diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index b6546a2..f4c52dd 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -187,7 +187,7 @@ m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 7 + m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &128843414 GameObject: @@ -248,7 +248,7 @@ m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 4 + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &267250012 GameObject: @@ -494,8 +494,10 @@ m_Children: - {fileID: 267250013} - {fileID: 1331646752} + - {fileID: 705507995} + - {fileID: 978069800} m_Father: {fileID: 0} - m_RootOrder: 5 + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &705507993 GameObject: @@ -587,8 +589,8 @@ m_LocalPosition: {x: 0, y: 13.65, z: -21.6} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 0 + m_Father: {fileID: 581008542} + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} --- !u!1 &887232955 GameObject: @@ -838,12 +840,12 @@ m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 978069796} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: -0.01, z: 0} m_LocalScale: {x: 100, y: 1, z: 100} m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 2 + m_Father: {fileID: 581008542} + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!65 &978069801 BoxCollider: @@ -1091,7 +1093,7 @@ - {fileID: 555903563} - {fileID: 1812881468} m_Father: {fileID: 0} - m_RootOrder: 6 + m_RootOrder: 4 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0} @@ -1128,7 +1130,7 @@ m_Children: - {fileID: 963194228} m_Father: {fileID: 0} - m_RootOrder: 1 + m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 10, y: 0, z: 0} --- !u!114 &1256630291 MonoBehaviour: @@ -1392,7 +1394,7 @@ m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 3 + m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &1714391594 MonoBehaviour: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index b6546a2..f4c52dd 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -187,7 +187,7 @@ m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 7 + m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &128843414 GameObject: @@ -248,7 +248,7 @@ m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 4 + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &267250012 GameObject: @@ -494,8 +494,10 @@ m_Children: - {fileID: 267250013} - {fileID: 1331646752} + - {fileID: 705507995} + - {fileID: 978069800} m_Father: {fileID: 0} - m_RootOrder: 5 + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &705507993 GameObject: @@ -587,8 +589,8 @@ m_LocalPosition: {x: 0, y: 13.65, z: -21.6} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 0 + m_Father: {fileID: 581008542} + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} --- !u!1 &887232955 GameObject: @@ -838,12 +840,12 @@ m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 978069796} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: -0.01, z: 0} m_LocalScale: {x: 100, y: 1, z: 100} m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 2 + m_Father: {fileID: 581008542} + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!65 &978069801 BoxCollider: @@ -1091,7 +1093,7 @@ - {fileID: 555903563} - {fileID: 1812881468} m_Father: {fileID: 0} - m_RootOrder: 6 + m_RootOrder: 4 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0} @@ -1128,7 +1130,7 @@ m_Children: - {fileID: 963194228} m_Father: {fileID: 0} - m_RootOrder: 1 + m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 10, y: 0, z: 0} --- !u!114 &1256630291 MonoBehaviour: @@ -1392,7 +1394,7 @@ m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 3 + m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &1714391594 MonoBehaviour: diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index dc4f85c..3076e12 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -5,14 +5,16 @@ public class Car { private Route route; public Road road; - public float roadPositon = 0f, speed = 0f, brakingAcceleration, acceleration, airResistance, t; private int roadIndex = 0; + public float roadPositon = 0f, speed = 0f, airResistance; private GameObject gameObject; public Config config; public Vector3 position, direction; public bool isAlive = true; private CarData carData; private float brakingTime, brakingDistance; + private static float g = - Physics.gravity.y; + private static float breakawayAcceleration = 0.9f * g, rollingAcceleration = 0.02f * g; public Car(Route route, Transform parent, Config config) { this.route = route; @@ -21,7 +23,12 @@ foreach (Road currentRoad in route.roads) { currentRoad.carsOnRoute.Add(this); } + airResistance = config.carAirResistanceModifier * config.carFrontalArea * config.airDensity * 0.5f; + setupGameObject(parent); + position = road.path.getPosition(0f); + } + private void setupGameObject(Transform parent) { gameObject = new GameObject(); FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32, 0.01f); MeshRenderer meshRenderer = gameObject.AddComponent(); @@ -35,22 +42,17 @@ carMesh.transform.parent = gameObject.transform; carData = gameObject.AddComponent(); carData.car = this; - position = road.path.getPosition(0f); - carData.Update(); - brakingAcceleration = 0.2f * -Physics.gravity.y; - acceleration = config.carTorque / (config.carWheelRadius * config.carMass); - airResistance = config.carAirResistanceModifier * config.carFrontalArea * config.airDensity * 0.5f; - updateBrakingParameters(); } private void updateBrakingParameters() { float B = - airResistance; - float A = - 0.02f * (-Physics.gravity.y) - 0.2f * (-Physics.gravity.y); + float A = - rollingAcceleration - breakawayAcceleration; float sqrtAB = Mathf.Sqrt(A * B); float sqrtAbyB = Mathf.Sqrt(A / B); float offset = Mathf.Atan(1f / sqrtAbyB * speed); brakingTime = offset; - brakingDistance = ((float)(Mathf.Log((float)System.Math.Cosh(sqrtAB * brakingTime - offset)) - Mathf.Log((float)System.Math.Cosh(offset)))) / B; + brakingDistance = ((float)(Mathf.Log((float)System.Math.Cosh(sqrtAB * brakingTime - offset)) - + Mathf.Log((float)System.Math.Cosh(offset)))) / B; } private void incrementRoad() { @@ -67,20 +69,17 @@ } private float getMaxSpeed(Road road, float distance) { - return Mathf.Sqrt((float)(-Mathf.Abs(road.path.getRadius(road.path.getT(distance))) / - 0.2 * Physics.gravity.y)); + return Mathf.Sqrt((float)(Mathf.Abs(road.path.getRadius(road.path.getT(distance)))) * breakawayAcceleration); } private bool needsBraking(float totalDistance, float maxSpeed) { - if (totalDistance < 0) { - return true; - } float B = - airResistance; - float A = - 0.02f * (-Physics.gravity.y) - 0.2f * (-Physics.gravity.y); + float A = - rollingAcceleration - breakawayAcceleration; float sqrtAB = Mathf.Sqrt(A * B); float sqrtAbyB = Mathf.Sqrt(A / B); - float offset = Mathf.Atan(1f / sqrtAbyB * speed); - float time = - (offset + Mathf.Atan(1f / sqrtAbyB * maxSpeed)) / sqrtAB; + float sqrtBbyA = 1 / sqrtAbyB; + float offset = Mathf.Atan(sqrtBbyA * speed); + float time = (offset - Mathf.Atan(sqrtBbyA * maxSpeed)) / sqrtAB; float distance = (-Mathf.Log(Mathf.Cos(sqrtAB * time - offset)) + Mathf.Log(Mathf.Cos(offset))) / B; return totalDistance <= distance; } @@ -124,7 +123,7 @@ } private bool isBraking() { - float stoppingDistance = Mathf.Min(brakingDistance, 1f); + float stoppingDistance = Mathf.Max(brakingDistance, 1f); Road currentRoad = road; float currentRoadPosition = roadPositon; int currentRoadIndex = roadIndex; @@ -141,7 +140,6 @@ currentRoad = route.roads[currentRoadIndex]; } if (needsBraking(totalDistance, getMaxSpeed(currentRoad, currentRoadPosition))) { - // avoid escaping in corners return true; } } @@ -168,7 +166,6 @@ private void applyAcceleration(float A, float B, float deltaT) { if (A * B > 0) { - // currently braking probably if (speed == 0) { return; } @@ -190,16 +187,16 @@ airResistance = config.carAirResistanceModifier * config.carFrontalArea * config.airDensity * 0.5f; updateBrakingParameters(); float B = - airResistance; - float A = - 0.02f * (-Physics.gravity.y); + float A = - rollingAcceleration; if (isBraking()) { gameObject.GetComponent().material = config.carBrakingMaterial; - A -= 0.2f * (-Physics.gravity.y); + A -= breakawayAcceleration; } else { gameObject.GetComponent().material = config.carAccelerationMaterial; if (speed > 0) { - A += Mathf.Min(config.power / speed, 10); + A += Mathf.Min(config.power / speed, breakawayAcceleration); } else { - A += 10f; + A += 1f; } } applyAcceleration(A, B, deltaTime); @@ -207,7 +204,7 @@ while (roadPositon > road.path.length) { incrementRoad(); } - t = road.path.getT(roadPositon); + float t = road.path.getT(roadPositon); position = road.path.getPosition(t); direction = road.path.getDirection(t); direction.Normalize(); diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index b6546a2..f4c52dd 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -187,7 +187,7 @@ m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 7 + m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &128843414 GameObject: @@ -248,7 +248,7 @@ m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 4 + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &267250012 GameObject: @@ -494,8 +494,10 @@ m_Children: - {fileID: 267250013} - {fileID: 1331646752} + - {fileID: 705507995} + - {fileID: 978069800} m_Father: {fileID: 0} - m_RootOrder: 5 + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &705507993 GameObject: @@ -587,8 +589,8 @@ m_LocalPosition: {x: 0, y: 13.65, z: -21.6} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 0 + m_Father: {fileID: 581008542} + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} --- !u!1 &887232955 GameObject: @@ -838,12 +840,12 @@ m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 978069796} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: -0.01, z: 0} m_LocalScale: {x: 100, y: 1, z: 100} m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 2 + m_Father: {fileID: 581008542} + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!65 &978069801 BoxCollider: @@ -1091,7 +1093,7 @@ - {fileID: 555903563} - {fileID: 1812881468} m_Father: {fileID: 0} - m_RootOrder: 6 + m_RootOrder: 4 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0} @@ -1128,7 +1130,7 @@ m_Children: - {fileID: 963194228} m_Father: {fileID: 0} - m_RootOrder: 1 + m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 10, y: 0, z: 0} --- !u!114 &1256630291 MonoBehaviour: @@ -1392,7 +1394,7 @@ m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 3 + m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &1714391594 MonoBehaviour: diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index dc4f85c..3076e12 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -5,14 +5,16 @@ public class Car { private Route route; public Road road; - public float roadPositon = 0f, speed = 0f, brakingAcceleration, acceleration, airResistance, t; private int roadIndex = 0; + public float roadPositon = 0f, speed = 0f, airResistance; private GameObject gameObject; public Config config; public Vector3 position, direction; public bool isAlive = true; private CarData carData; private float brakingTime, brakingDistance; + private static float g = - Physics.gravity.y; + private static float breakawayAcceleration = 0.9f * g, rollingAcceleration = 0.02f * g; public Car(Route route, Transform parent, Config config) { this.route = route; @@ -21,7 +23,12 @@ foreach (Road currentRoad in route.roads) { currentRoad.carsOnRoute.Add(this); } + airResistance = config.carAirResistanceModifier * config.carFrontalArea * config.airDensity * 0.5f; + setupGameObject(parent); + position = road.path.getPosition(0f); + } + private void setupGameObject(Transform parent) { gameObject = new GameObject(); FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32, 0.01f); MeshRenderer meshRenderer = gameObject.AddComponent(); @@ -35,22 +42,17 @@ carMesh.transform.parent = gameObject.transform; carData = gameObject.AddComponent(); carData.car = this; - position = road.path.getPosition(0f); - carData.Update(); - brakingAcceleration = 0.2f * -Physics.gravity.y; - acceleration = config.carTorque / (config.carWheelRadius * config.carMass); - airResistance = config.carAirResistanceModifier * config.carFrontalArea * config.airDensity * 0.5f; - updateBrakingParameters(); } private void updateBrakingParameters() { float B = - airResistance; - float A = - 0.02f * (-Physics.gravity.y) - 0.2f * (-Physics.gravity.y); + float A = - rollingAcceleration - breakawayAcceleration; float sqrtAB = Mathf.Sqrt(A * B); float sqrtAbyB = Mathf.Sqrt(A / B); float offset = Mathf.Atan(1f / sqrtAbyB * speed); brakingTime = offset; - brakingDistance = ((float)(Mathf.Log((float)System.Math.Cosh(sqrtAB * brakingTime - offset)) - Mathf.Log((float)System.Math.Cosh(offset)))) / B; + brakingDistance = ((float)(Mathf.Log((float)System.Math.Cosh(sqrtAB * brakingTime - offset)) - + Mathf.Log((float)System.Math.Cosh(offset)))) / B; } private void incrementRoad() { @@ -67,20 +69,17 @@ } private float getMaxSpeed(Road road, float distance) { - return Mathf.Sqrt((float)(-Mathf.Abs(road.path.getRadius(road.path.getT(distance))) / - 0.2 * Physics.gravity.y)); + return Mathf.Sqrt((float)(Mathf.Abs(road.path.getRadius(road.path.getT(distance)))) * breakawayAcceleration); } private bool needsBraking(float totalDistance, float maxSpeed) { - if (totalDistance < 0) { - return true; - } float B = - airResistance; - float A = - 0.02f * (-Physics.gravity.y) - 0.2f * (-Physics.gravity.y); + float A = - rollingAcceleration - breakawayAcceleration; float sqrtAB = Mathf.Sqrt(A * B); float sqrtAbyB = Mathf.Sqrt(A / B); - float offset = Mathf.Atan(1f / sqrtAbyB * speed); - float time = - (offset + Mathf.Atan(1f / sqrtAbyB * maxSpeed)) / sqrtAB; + float sqrtBbyA = 1 / sqrtAbyB; + float offset = Mathf.Atan(sqrtBbyA * speed); + float time = (offset - Mathf.Atan(sqrtBbyA * maxSpeed)) / sqrtAB; float distance = (-Mathf.Log(Mathf.Cos(sqrtAB * time - offset)) + Mathf.Log(Mathf.Cos(offset))) / B; return totalDistance <= distance; } @@ -124,7 +123,7 @@ } private bool isBraking() { - float stoppingDistance = Mathf.Min(brakingDistance, 1f); + float stoppingDistance = Mathf.Max(brakingDistance, 1f); Road currentRoad = road; float currentRoadPosition = roadPositon; int currentRoadIndex = roadIndex; @@ -141,7 +140,6 @@ currentRoad = route.roads[currentRoadIndex]; } if (needsBraking(totalDistance, getMaxSpeed(currentRoad, currentRoadPosition))) { - // avoid escaping in corners return true; } } @@ -168,7 +166,6 @@ private void applyAcceleration(float A, float B, float deltaT) { if (A * B > 0) { - // currently braking probably if (speed == 0) { return; } @@ -190,16 +187,16 @@ airResistance = config.carAirResistanceModifier * config.carFrontalArea * config.airDensity * 0.5f; updateBrakingParameters(); float B = - airResistance; - float A = - 0.02f * (-Physics.gravity.y); + float A = - rollingAcceleration; if (isBraking()) { gameObject.GetComponent().material = config.carBrakingMaterial; - A -= 0.2f * (-Physics.gravity.y); + A -= breakawayAcceleration; } else { gameObject.GetComponent().material = config.carAccelerationMaterial; if (speed > 0) { - A += Mathf.Min(config.power / speed, 10); + A += Mathf.Min(config.power / speed, breakawayAcceleration); } else { - A += 10f; + A += 1f; } } applyAcceleration(A, B, deltaTime); @@ -207,7 +204,7 @@ while (roadPositon > road.path.length) { incrementRoad(); } - t = road.path.getT(roadPositon); + float t = road.path.getT(roadPositon); position = road.path.getPosition(t); direction = road.path.getDirection(t); direction.Normalize(); diff --git a/Assets/Scripts/Config.cs b/Assets/Scripts/Config.cs index 2867e64..2ec9709 100644 --- a/Assets/Scripts/Config.cs +++ b/Assets/Scripts/Config.cs @@ -8,10 +8,9 @@ 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 Mesh carMesh; public Material carMaterial; - public float power = 1000f; + public float power = 100000f; public float totalTravelTime = 0f; public int totalCars = 0; public RoadNetwork roadNetwork = new RoadNetwork(); diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index b6546a2..f4c52dd 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -187,7 +187,7 @@ m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 7 + m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &128843414 GameObject: @@ -248,7 +248,7 @@ m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 4 + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &267250012 GameObject: @@ -494,8 +494,10 @@ m_Children: - {fileID: 267250013} - {fileID: 1331646752} + - {fileID: 705507995} + - {fileID: 978069800} m_Father: {fileID: 0} - m_RootOrder: 5 + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &705507993 GameObject: @@ -587,8 +589,8 @@ m_LocalPosition: {x: 0, y: 13.65, z: -21.6} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 0 + m_Father: {fileID: 581008542} + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} --- !u!1 &887232955 GameObject: @@ -838,12 +840,12 @@ m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 978069796} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: -0.01, z: 0} m_LocalScale: {x: 100, y: 1, z: 100} m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 2 + m_Father: {fileID: 581008542} + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!65 &978069801 BoxCollider: @@ -1091,7 +1093,7 @@ - {fileID: 555903563} - {fileID: 1812881468} m_Father: {fileID: 0} - m_RootOrder: 6 + m_RootOrder: 4 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0} @@ -1128,7 +1130,7 @@ m_Children: - {fileID: 963194228} m_Father: {fileID: 0} - m_RootOrder: 1 + m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 10, y: 0, z: 0} --- !u!114 &1256630291 MonoBehaviour: @@ -1392,7 +1394,7 @@ m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 3 + m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &1714391594 MonoBehaviour: diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index dc4f85c..3076e12 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -5,14 +5,16 @@ public class Car { private Route route; public Road road; - public float roadPositon = 0f, speed = 0f, brakingAcceleration, acceleration, airResistance, t; private int roadIndex = 0; + public float roadPositon = 0f, speed = 0f, airResistance; private GameObject gameObject; public Config config; public Vector3 position, direction; public bool isAlive = true; private CarData carData; private float brakingTime, brakingDistance; + private static float g = - Physics.gravity.y; + private static float breakawayAcceleration = 0.9f * g, rollingAcceleration = 0.02f * g; public Car(Route route, Transform parent, Config config) { this.route = route; @@ -21,7 +23,12 @@ foreach (Road currentRoad in route.roads) { currentRoad.carsOnRoute.Add(this); } + airResistance = config.carAirResistanceModifier * config.carFrontalArea * config.airDensity * 0.5f; + setupGameObject(parent); + position = road.path.getPosition(0f); + } + private void setupGameObject(Transform parent) { gameObject = new GameObject(); FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32, 0.01f); MeshRenderer meshRenderer = gameObject.AddComponent(); @@ -35,22 +42,17 @@ carMesh.transform.parent = gameObject.transform; carData = gameObject.AddComponent(); carData.car = this; - position = road.path.getPosition(0f); - carData.Update(); - brakingAcceleration = 0.2f * -Physics.gravity.y; - acceleration = config.carTorque / (config.carWheelRadius * config.carMass); - airResistance = config.carAirResistanceModifier * config.carFrontalArea * config.airDensity * 0.5f; - updateBrakingParameters(); } private void updateBrakingParameters() { float B = - airResistance; - float A = - 0.02f * (-Physics.gravity.y) - 0.2f * (-Physics.gravity.y); + float A = - rollingAcceleration - breakawayAcceleration; float sqrtAB = Mathf.Sqrt(A * B); float sqrtAbyB = Mathf.Sqrt(A / B); float offset = Mathf.Atan(1f / sqrtAbyB * speed); brakingTime = offset; - brakingDistance = ((float)(Mathf.Log((float)System.Math.Cosh(sqrtAB * brakingTime - offset)) - Mathf.Log((float)System.Math.Cosh(offset)))) / B; + brakingDistance = ((float)(Mathf.Log((float)System.Math.Cosh(sqrtAB * brakingTime - offset)) - + Mathf.Log((float)System.Math.Cosh(offset)))) / B; } private void incrementRoad() { @@ -67,20 +69,17 @@ } private float getMaxSpeed(Road road, float distance) { - return Mathf.Sqrt((float)(-Mathf.Abs(road.path.getRadius(road.path.getT(distance))) / - 0.2 * Physics.gravity.y)); + return Mathf.Sqrt((float)(Mathf.Abs(road.path.getRadius(road.path.getT(distance)))) * breakawayAcceleration); } private bool needsBraking(float totalDistance, float maxSpeed) { - if (totalDistance < 0) { - return true; - } float B = - airResistance; - float A = - 0.02f * (-Physics.gravity.y) - 0.2f * (-Physics.gravity.y); + float A = - rollingAcceleration - breakawayAcceleration; float sqrtAB = Mathf.Sqrt(A * B); float sqrtAbyB = Mathf.Sqrt(A / B); - float offset = Mathf.Atan(1f / sqrtAbyB * speed); - float time = - (offset + Mathf.Atan(1f / sqrtAbyB * maxSpeed)) / sqrtAB; + float sqrtBbyA = 1 / sqrtAbyB; + float offset = Mathf.Atan(sqrtBbyA * speed); + float time = (offset - Mathf.Atan(sqrtBbyA * maxSpeed)) / sqrtAB; float distance = (-Mathf.Log(Mathf.Cos(sqrtAB * time - offset)) + Mathf.Log(Mathf.Cos(offset))) / B; return totalDistance <= distance; } @@ -124,7 +123,7 @@ } private bool isBraking() { - float stoppingDistance = Mathf.Min(brakingDistance, 1f); + float stoppingDistance = Mathf.Max(brakingDistance, 1f); Road currentRoad = road; float currentRoadPosition = roadPositon; int currentRoadIndex = roadIndex; @@ -141,7 +140,6 @@ currentRoad = route.roads[currentRoadIndex]; } if (needsBraking(totalDistance, getMaxSpeed(currentRoad, currentRoadPosition))) { - // avoid escaping in corners return true; } } @@ -168,7 +166,6 @@ private void applyAcceleration(float A, float B, float deltaT) { if (A * B > 0) { - // currently braking probably if (speed == 0) { return; } @@ -190,16 +187,16 @@ airResistance = config.carAirResistanceModifier * config.carFrontalArea * config.airDensity * 0.5f; updateBrakingParameters(); float B = - airResistance; - float A = - 0.02f * (-Physics.gravity.y); + float A = - rollingAcceleration; if (isBraking()) { gameObject.GetComponent().material = config.carBrakingMaterial; - A -= 0.2f * (-Physics.gravity.y); + A -= breakawayAcceleration; } else { gameObject.GetComponent().material = config.carAccelerationMaterial; if (speed > 0) { - A += Mathf.Min(config.power / speed, 10); + A += Mathf.Min(config.power / speed, breakawayAcceleration); } else { - A += 10f; + A += 1f; } } applyAcceleration(A, B, deltaTime); @@ -207,7 +204,7 @@ while (roadPositon > road.path.length) { incrementRoad(); } - t = road.path.getT(roadPositon); + float t = road.path.getT(roadPositon); position = road.path.getPosition(t); direction = road.path.getDirection(t); direction.Normalize(); diff --git a/Assets/Scripts/Config.cs b/Assets/Scripts/Config.cs index 2867e64..2ec9709 100644 --- a/Assets/Scripts/Config.cs +++ b/Assets/Scripts/Config.cs @@ -8,10 +8,9 @@ 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 Mesh carMesh; public Material carMaterial; - public float power = 1000f; + public float power = 100000f; public float totalTravelTime = 0f; public int totalCars = 0; public RoadNetwork roadNetwork = new RoadNetwork(); diff --git a/Assets/Scripts/Roads/RoadEdit.cs b/Assets/Scripts/Roads/RoadEdit.cs deleted file mode 100644 index f9841b4..0000000 --- a/Assets/Scripts/Roads/RoadEdit.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - } - } -}