diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index e2abe70..ae26c58 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -10,6 +10,7 @@ private GameObject gameObject; private Config config; public Vector3 position; + public bool isAlive = true; public Car(Route route, Transform parent, Config config) { this.route = route; @@ -33,10 +34,11 @@ roadIndex++; if (roadIndex == route.roads.Count) { GameObject.Destroy(gameObject); + isAlive = false; return; } currentRoad.cars.Remove(this); - currentRoad = route.roads[roadIndex]; + currentRoad = route.roads[roadIndex]; currentRoad.cars.Add(this); } @@ -47,10 +49,10 @@ (config.carTorque*deltaTime) / (config.carWheelRadius * config.carMass) ; float distance = deltaTime * speed; - float currentDeltaT = distance / currentRoad.nodeDistance; + float currentDeltaT = distance / currentRoad.path.length; currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; - for (int i = 0; i < 10; i++) { //while (Mathf.Abs(currentDistance - distance) > 0.001f) { - currentDeltaT += (distance - currentDistance) / currentRoad.nodeDistance; + for (int i = 0; i < 10; i++) { + currentDeltaT += (distance - currentDistance) / currentRoad.path.length; while (roadPositon + currentDeltaT > 1f) { incrementRoad(); } diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index e2abe70..ae26c58 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -10,6 +10,7 @@ private GameObject gameObject; private Config config; public Vector3 position; + public bool isAlive = true; public Car(Route route, Transform parent, Config config) { this.route = route; @@ -33,10 +34,11 @@ roadIndex++; if (roadIndex == route.roads.Count) { GameObject.Destroy(gameObject); + isAlive = false; return; } currentRoad.cars.Remove(this); - currentRoad = route.roads[roadIndex]; + currentRoad = route.roads[roadIndex]; currentRoad.cars.Add(this); } @@ -47,10 +49,10 @@ (config.carTorque*deltaTime) / (config.carWheelRadius * config.carMass) ; float distance = deltaTime * speed; - float currentDeltaT = distance / currentRoad.nodeDistance; + float currentDeltaT = distance / currentRoad.path.length; currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; - for (int i = 0; i < 10; i++) { //while (Mathf.Abs(currentDistance - distance) > 0.001f) { - currentDeltaT += (distance - currentDistance) / currentRoad.nodeDistance; + for (int i = 0; i < 10; i++) { + currentDeltaT += (distance - currentDistance) / currentRoad.path.length; while (roadPositon + currentDeltaT > 1f) { incrementRoad(); } diff --git a/Assets/Scripts/Car/CarData.cs b/Assets/Scripts/Car/CarData.cs index d26ce78..50f93c3 100644 --- a/Assets/Scripts/Car/CarData.cs +++ b/Assets/Scripts/Car/CarData.cs @@ -10,6 +10,8 @@ public void Update() { car.step(Time.deltaTime); - transform.position = car.position; + if (car.isAlive) { + transform.position = car.position; + } } } diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index e2abe70..ae26c58 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -10,6 +10,7 @@ private GameObject gameObject; private Config config; public Vector3 position; + public bool isAlive = true; public Car(Route route, Transform parent, Config config) { this.route = route; @@ -33,10 +34,11 @@ roadIndex++; if (roadIndex == route.roads.Count) { GameObject.Destroy(gameObject); + isAlive = false; return; } currentRoad.cars.Remove(this); - currentRoad = route.roads[roadIndex]; + currentRoad = route.roads[roadIndex]; currentRoad.cars.Add(this); } @@ -47,10 +49,10 @@ (config.carTorque*deltaTime) / (config.carWheelRadius * config.carMass) ; float distance = deltaTime * speed; - float currentDeltaT = distance / currentRoad.nodeDistance; + float currentDeltaT = distance / currentRoad.path.length; currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; - for (int i = 0; i < 10; i++) { //while (Mathf.Abs(currentDistance - distance) > 0.001f) { - currentDeltaT += (distance - currentDistance) / currentRoad.nodeDistance; + for (int i = 0; i < 10; i++) { + currentDeltaT += (distance - currentDistance) / currentRoad.path.length; while (roadPositon + currentDeltaT > 1f) { incrementRoad(); } diff --git a/Assets/Scripts/Car/CarData.cs b/Assets/Scripts/Car/CarData.cs index d26ce78..50f93c3 100644 --- a/Assets/Scripts/Car/CarData.cs +++ b/Assets/Scripts/Car/CarData.cs @@ -10,6 +10,8 @@ public void Update() { car.step(Time.deltaTime); - transform.position = car.position; + if (car.isAlive) { + transform.position = car.position; + } } } diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs index ea19006..b0e9531 100644 --- a/Assets/Scripts/Primitives/Bezier.cs +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -4,12 +4,14 @@ public class Bezier { public Vector3 A, B, C, D; + public float length; public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { this.A = A; this.B = B; this.C = C; this.D = D; + updateLength(); } public Bezier() { @@ -17,6 +19,7 @@ this.B = Vector3.zero; this.C = Vector3.zero; this.D = Vector3.zero; + updateLength(); } public Vector3 getPosition(float t) { @@ -28,6 +31,17 @@ D * ( t*t*t); } + public void updateLength() { + length = 0f; + float steps = 50f; + Vector3 previous = A; + for (int i = 1; i <= steps; i++) { + Vector3 current = getPosition(i / steps); + length += (current - previous).magnitude; + previous = current; + } + } + public Vector3 getDirection(float t) { float T = 1.0f - t; return diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index e2abe70..ae26c58 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -10,6 +10,7 @@ private GameObject gameObject; private Config config; public Vector3 position; + public bool isAlive = true; public Car(Route route, Transform parent, Config config) { this.route = route; @@ -33,10 +34,11 @@ roadIndex++; if (roadIndex == route.roads.Count) { GameObject.Destroy(gameObject); + isAlive = false; return; } currentRoad.cars.Remove(this); - currentRoad = route.roads[roadIndex]; + currentRoad = route.roads[roadIndex]; currentRoad.cars.Add(this); } @@ -47,10 +49,10 @@ (config.carTorque*deltaTime) / (config.carWheelRadius * config.carMass) ; float distance = deltaTime * speed; - float currentDeltaT = distance / currentRoad.nodeDistance; + float currentDeltaT = distance / currentRoad.path.length; currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; - for (int i = 0; i < 10; i++) { //while (Mathf.Abs(currentDistance - distance) > 0.001f) { - currentDeltaT += (distance - currentDistance) / currentRoad.nodeDistance; + for (int i = 0; i < 10; i++) { + currentDeltaT += (distance - currentDistance) / currentRoad.path.length; while (roadPositon + currentDeltaT > 1f) { incrementRoad(); } diff --git a/Assets/Scripts/Car/CarData.cs b/Assets/Scripts/Car/CarData.cs index d26ce78..50f93c3 100644 --- a/Assets/Scripts/Car/CarData.cs +++ b/Assets/Scripts/Car/CarData.cs @@ -10,6 +10,8 @@ public void Update() { car.step(Time.deltaTime); - transform.position = car.position; + if (car.isAlive) { + transform.position = car.position; + } } } diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs index ea19006..b0e9531 100644 --- a/Assets/Scripts/Primitives/Bezier.cs +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -4,12 +4,14 @@ public class Bezier { public Vector3 A, B, C, D; + public float length; public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { this.A = A; this.B = B; this.C = C; this.D = D; + updateLength(); } public Bezier() { @@ -17,6 +19,7 @@ this.B = Vector3.zero; this.C = Vector3.zero; this.D = Vector3.zero; + updateLength(); } public Vector3 getPosition(float t) { @@ -28,6 +31,17 @@ D * ( t*t*t); } + public void updateLength() { + length = 0f; + float steps = 50f; + Vector3 previous = A; + for (int i = 1; i <= steps; i++) { + Vector3 current = getPosition(i / steps); + length += (current - previous).magnitude; + previous = current; + } + } + public Vector3 getDirection(float t) { float T = 1.0f - t; return diff --git a/Assets/Scripts/Roads/Road.cs b/Assets/Scripts/Roads/Road.cs index 66b27af..c6d5c5e 100644 --- a/Assets/Scripts/Roads/Road.cs +++ b/Assets/Scripts/Roads/Road.cs @@ -10,7 +10,6 @@ private Config config; private MeshCollider collider; public GameObject gameObject; - public float nodeDistance; public List cars = new List(); public Road(Node start, Node end, Config config) { @@ -101,7 +100,7 @@ arrow2.D = arrow2.B; arrow1Renderer.update(); arrow2Renderer.update(); - nodeDistance = (nodes[0].position - nodes[1].position).magnitude; + path.updateLength(); foreach (Car car in cars) { car.position = path.getPosition(car.roadPositon); }