diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 0bd7901..5785c07 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -220,6 +220,7 @@ m_EditorClassIdentifier: roadMaterial: {fileID: 2100000, guid: 1b0a930ba0e3dba46a226bb15ae71b80, type: 2} roadEditMaterial: {fileID: 2100000, guid: e2c5b6e79b6cdb545863f245ced42125, type: 2} + carMaterial: {fileID: 2100000, guid: bb482d1941dca4649842d0da3f1120ab, type: 2} mode: 2 futureMode: 0 highlight: {fileID: 887232956} diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 0bd7901..5785c07 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -220,6 +220,7 @@ m_EditorClassIdentifier: roadMaterial: {fileID: 2100000, guid: 1b0a930ba0e3dba46a226bb15ae71b80, type: 2} roadEditMaterial: {fileID: 2100000, guid: e2c5b6e79b6cdb545863f245ced42125, type: 2} + carMaterial: {fileID: 2100000, guid: bb482d1941dca4649842d0da3f1120ab, type: 2} mode: 2 futureMode: 0 highlight: {fileID: 887232956} diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index f9c539a..e2abe70 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -5,13 +5,17 @@ public class Car { private Route route; private Road currentRoad; - private float currentPosition = 0f, speed = 0.25f; + public float roadPositon = 0f, speed = 0f, currentDistance; private int roadIndex = 0; - GameObject gameObject; + private GameObject gameObject; + private Config config; + public Vector3 position; public Car(Route route, Transform parent, Config config) { this.route = route; + this.config = config; currentRoad = route.roads[0]; + currentRoad.cars.Add(this); gameObject = new GameObject(); FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); @@ -20,23 +24,42 @@ gameObject.transform.parent = parent; CarData carData = gameObject.AddComponent(); carData.car = this; + position = currentRoad.path.getPosition(0f); carData.Update(); } - - public Vector3 getPosition() { - return currentRoad.path.getPosition(currentPosition); - } - public void step(float deltaTime) { - currentPosition += deltaTime * speed; - if (currentPosition > 1f) { - currentPosition -= 1f; - roadIndex++; - if (roadIndex > route.roads.Count-1) { - GameObject.Destroy(gameObject); - return; - } - currentRoad = route.roads[roadIndex]; + private void incrementRoad() { + roadPositon -= 1f; + roadIndex++; + if (roadIndex == route.roads.Count) { + GameObject.Destroy(gameObject); + return; } + currentRoad.cars.Remove(this); + currentRoad = route.roads[roadIndex]; + currentRoad.cars.Add(this); + } + + public void step(float deltaTime) { + speed += + -(deltaTime*config.carAirResistanceModifier*config.carFrontalArea*config.airDensity*speed*speed) / + (2*config.carMass) + + (config.carTorque*deltaTime) / (config.carWheelRadius * config.carMass) + ; + float distance = deltaTime * speed; + float currentDeltaT = distance / currentRoad.nodeDistance; + 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; + while (roadPositon + currentDeltaT > 1f) { + incrementRoad(); + } + currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; + } + roadPositon += currentDeltaT; + while (roadPositon > 1f) { + incrementRoad(); + } + position = currentRoad.path.getPosition(roadPositon); } } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 0bd7901..5785c07 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -220,6 +220,7 @@ m_EditorClassIdentifier: roadMaterial: {fileID: 2100000, guid: 1b0a930ba0e3dba46a226bb15ae71b80, type: 2} roadEditMaterial: {fileID: 2100000, guid: e2c5b6e79b6cdb545863f245ced42125, type: 2} + carMaterial: {fileID: 2100000, guid: bb482d1941dca4649842d0da3f1120ab, type: 2} mode: 2 futureMode: 0 highlight: {fileID: 887232956} diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index f9c539a..e2abe70 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -5,13 +5,17 @@ public class Car { private Route route; private Road currentRoad; - private float currentPosition = 0f, speed = 0.25f; + public float roadPositon = 0f, speed = 0f, currentDistance; private int roadIndex = 0; - GameObject gameObject; + private GameObject gameObject; + private Config config; + public Vector3 position; public Car(Route route, Transform parent, Config config) { this.route = route; + this.config = config; currentRoad = route.roads[0]; + currentRoad.cars.Add(this); gameObject = new GameObject(); FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); @@ -20,23 +24,42 @@ gameObject.transform.parent = parent; CarData carData = gameObject.AddComponent(); carData.car = this; + position = currentRoad.path.getPosition(0f); carData.Update(); } - - public Vector3 getPosition() { - return currentRoad.path.getPosition(currentPosition); - } - public void step(float deltaTime) { - currentPosition += deltaTime * speed; - if (currentPosition > 1f) { - currentPosition -= 1f; - roadIndex++; - if (roadIndex > route.roads.Count-1) { - GameObject.Destroy(gameObject); - return; - } - currentRoad = route.roads[roadIndex]; + private void incrementRoad() { + roadPositon -= 1f; + roadIndex++; + if (roadIndex == route.roads.Count) { + GameObject.Destroy(gameObject); + return; } + currentRoad.cars.Remove(this); + currentRoad = route.roads[roadIndex]; + currentRoad.cars.Add(this); + } + + public void step(float deltaTime) { + speed += + -(deltaTime*config.carAirResistanceModifier*config.carFrontalArea*config.airDensity*speed*speed) / + (2*config.carMass) + + (config.carTorque*deltaTime) / (config.carWheelRadius * config.carMass) + ; + float distance = deltaTime * speed; + float currentDeltaT = distance / currentRoad.nodeDistance; + 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; + while (roadPositon + currentDeltaT > 1f) { + incrementRoad(); + } + currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; + } + roadPositon += currentDeltaT; + while (roadPositon > 1f) { + incrementRoad(); + } + position = currentRoad.path.getPosition(roadPositon); } } diff --git a/Assets/Scripts/Car/CarData.cs b/Assets/Scripts/Car/CarData.cs index db481e2..d26ce78 100644 --- a/Assets/Scripts/Car/CarData.cs +++ b/Assets/Scripts/Car/CarData.cs @@ -10,6 +10,6 @@ public void Update() { car.step(Time.deltaTime); - transform.position = car.getPosition(); + transform.position = car.position; } } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 0bd7901..5785c07 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -220,6 +220,7 @@ m_EditorClassIdentifier: roadMaterial: {fileID: 2100000, guid: 1b0a930ba0e3dba46a226bb15ae71b80, type: 2} roadEditMaterial: {fileID: 2100000, guid: e2c5b6e79b6cdb545863f245ced42125, type: 2} + carMaterial: {fileID: 2100000, guid: bb482d1941dca4649842d0da3f1120ab, type: 2} mode: 2 futureMode: 0 highlight: {fileID: 887232956} diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index f9c539a..e2abe70 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -5,13 +5,17 @@ public class Car { private Route route; private Road currentRoad; - private float currentPosition = 0f, speed = 0.25f; + public float roadPositon = 0f, speed = 0f, currentDistance; private int roadIndex = 0; - GameObject gameObject; + private GameObject gameObject; + private Config config; + public Vector3 position; public Car(Route route, Transform parent, Config config) { this.route = route; + this.config = config; currentRoad = route.roads[0]; + currentRoad.cars.Add(this); gameObject = new GameObject(); FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); @@ -20,23 +24,42 @@ gameObject.transform.parent = parent; CarData carData = gameObject.AddComponent(); carData.car = this; + position = currentRoad.path.getPosition(0f); carData.Update(); } - - public Vector3 getPosition() { - return currentRoad.path.getPosition(currentPosition); - } - public void step(float deltaTime) { - currentPosition += deltaTime * speed; - if (currentPosition > 1f) { - currentPosition -= 1f; - roadIndex++; - if (roadIndex > route.roads.Count-1) { - GameObject.Destroy(gameObject); - return; - } - currentRoad = route.roads[roadIndex]; + private void incrementRoad() { + roadPositon -= 1f; + roadIndex++; + if (roadIndex == route.roads.Count) { + GameObject.Destroy(gameObject); + return; } + currentRoad.cars.Remove(this); + currentRoad = route.roads[roadIndex]; + currentRoad.cars.Add(this); + } + + public void step(float deltaTime) { + speed += + -(deltaTime*config.carAirResistanceModifier*config.carFrontalArea*config.airDensity*speed*speed) / + (2*config.carMass) + + (config.carTorque*deltaTime) / (config.carWheelRadius * config.carMass) + ; + float distance = deltaTime * speed; + float currentDeltaT = distance / currentRoad.nodeDistance; + 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; + while (roadPositon + currentDeltaT > 1f) { + incrementRoad(); + } + currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; + } + roadPositon += currentDeltaT; + while (roadPositon > 1f) { + incrementRoad(); + } + position = currentRoad.path.getPosition(roadPositon); } } diff --git a/Assets/Scripts/Car/CarData.cs b/Assets/Scripts/Car/CarData.cs index db481e2..d26ce78 100644 --- a/Assets/Scripts/Car/CarData.cs +++ b/Assets/Scripts/Car/CarData.cs @@ -10,6 +10,6 @@ public void Update() { car.step(Time.deltaTime); - transform.position = car.getPosition(); + transform.position = car.position; } } diff --git a/Assets/Scripts/Config.cs b/Assets/Scripts/Config.cs index 7d002db..83d3aa8 100644 --- a/Assets/Scripts/Config.cs +++ b/Assets/Scripts/Config.cs @@ -7,6 +7,8 @@ public Mode mode = Mode.DeleteRoad; public Mode futureMode; public RectTransform highlight; + public float carAirResistanceModifier = 0.3f, carFrontalArea = 4f, airDensity = 1.225f, carMass = 1000f; + public float carTorque = 300f, carWheelRadius = 0.25f; public void click(Mode mode, int index) { this.mode = Mode.ClickButton; diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 0bd7901..5785c07 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -220,6 +220,7 @@ m_EditorClassIdentifier: roadMaterial: {fileID: 2100000, guid: 1b0a930ba0e3dba46a226bb15ae71b80, type: 2} roadEditMaterial: {fileID: 2100000, guid: e2c5b6e79b6cdb545863f245ced42125, type: 2} + carMaterial: {fileID: 2100000, guid: bb482d1941dca4649842d0da3f1120ab, type: 2} mode: 2 futureMode: 0 highlight: {fileID: 887232956} diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index f9c539a..e2abe70 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -5,13 +5,17 @@ public class Car { private Route route; private Road currentRoad; - private float currentPosition = 0f, speed = 0.25f; + public float roadPositon = 0f, speed = 0f, currentDistance; private int roadIndex = 0; - GameObject gameObject; + private GameObject gameObject; + private Config config; + public Vector3 position; public Car(Route route, Transform parent, Config config) { this.route = route; + this.config = config; currentRoad = route.roads[0]; + currentRoad.cars.Add(this); gameObject = new GameObject(); FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); @@ -20,23 +24,42 @@ gameObject.transform.parent = parent; CarData carData = gameObject.AddComponent(); carData.car = this; + position = currentRoad.path.getPosition(0f); carData.Update(); } - - public Vector3 getPosition() { - return currentRoad.path.getPosition(currentPosition); - } - public void step(float deltaTime) { - currentPosition += deltaTime * speed; - if (currentPosition > 1f) { - currentPosition -= 1f; - roadIndex++; - if (roadIndex > route.roads.Count-1) { - GameObject.Destroy(gameObject); - return; - } - currentRoad = route.roads[roadIndex]; + private void incrementRoad() { + roadPositon -= 1f; + roadIndex++; + if (roadIndex == route.roads.Count) { + GameObject.Destroy(gameObject); + return; } + currentRoad.cars.Remove(this); + currentRoad = route.roads[roadIndex]; + currentRoad.cars.Add(this); + } + + public void step(float deltaTime) { + speed += + -(deltaTime*config.carAirResistanceModifier*config.carFrontalArea*config.airDensity*speed*speed) / + (2*config.carMass) + + (config.carTorque*deltaTime) / (config.carWheelRadius * config.carMass) + ; + float distance = deltaTime * speed; + float currentDeltaT = distance / currentRoad.nodeDistance; + 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; + while (roadPositon + currentDeltaT > 1f) { + incrementRoad(); + } + currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; + } + roadPositon += currentDeltaT; + while (roadPositon > 1f) { + incrementRoad(); + } + position = currentRoad.path.getPosition(roadPositon); } } diff --git a/Assets/Scripts/Car/CarData.cs b/Assets/Scripts/Car/CarData.cs index db481e2..d26ce78 100644 --- a/Assets/Scripts/Car/CarData.cs +++ b/Assets/Scripts/Car/CarData.cs @@ -10,6 +10,6 @@ public void Update() { car.step(Time.deltaTime); - transform.position = car.getPosition(); + transform.position = car.position; } } diff --git a/Assets/Scripts/Config.cs b/Assets/Scripts/Config.cs index 7d002db..83d3aa8 100644 --- a/Assets/Scripts/Config.cs +++ b/Assets/Scripts/Config.cs @@ -7,6 +7,8 @@ public Mode mode = Mode.DeleteRoad; public Mode futureMode; public RectTransform highlight; + public float carAirResistanceModifier = 0.3f, carFrontalArea = 4f, airDensity = 1.225f, carMass = 1000f; + public float carTorque = 300f, carWheelRadius = 0.25f; public void click(Mode mode, int index) { this.mode = Mode.ClickButton; diff --git a/Assets/Scripts/Roads/Road.cs b/Assets/Scripts/Roads/Road.cs index 48ebf03..66b27af 100644 --- a/Assets/Scripts/Roads/Road.cs +++ b/Assets/Scripts/Roads/Road.cs @@ -10,6 +10,8 @@ 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) { nodes.Add(start); @@ -99,6 +101,10 @@ arrow2.D = arrow2.B; arrow1Renderer.update(); arrow2Renderer.update(); + nodeDistance = (nodes[0].position - nodes[1].position).magnitude; + foreach (Car car in cars) { + car.position = path.getPosition(car.roadPositon); + } } override public bool Equals(object other) {