diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 5ca7c5f..ee8949e 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -232,7 +232,7 @@ carMass: 1000 carMesh: {fileID: -2432090755550338912, guid: 6358fc72c8f1a5645b372c8756ff9871, type: 3} carMaterial: {fileID: 2100000, guid: 55dd6bb7e89b75f4b9958b0b7bb7c5d2, type: 2} - power: 1000 + power: 10000 travelTimes: [] trafficLights: {fileID: 128843417} cameraControl: {fileID: 1256630291} diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 5ca7c5f..ee8949e 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -232,7 +232,7 @@ carMass: 1000 carMesh: {fileID: -2432090755550338912, guid: 6358fc72c8f1a5645b372c8756ff9871, type: 3} carMaterial: {fileID: 2100000, guid: 55dd6bb7e89b75f4b9958b0b7bb7c5d2, type: 2} - power: 1000 + power: 10000 travelTimes: [] trafficLights: {fileID: 128843417} cameraControl: {fileID: 1256630291} diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index 32d7522..cd451a7 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -14,7 +14,7 @@ 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; + private static float breakawayAcceleration = 0.9f * g * (float) Mathf.Sqrt(2) / 2f, rollingAcceleration = 0.02f * g; public Car(Route route, Transform parent, Config config) { this.route = route; @@ -88,6 +88,9 @@ if (roadIndex == route.roads.Count - 1) { return false; } + if (Vector3.Distance(position, car.position) > 10f) { + return false; + } Road conflict = car.road; float otherDistance = conflict.path.length - car.roadPositon; for (int i = car.roadIndex; !this.route.roads.Contains(conflict) && i < car.route.roads.Count;) { @@ -104,18 +107,16 @@ float otherTraveledDistance = car.speed * time; float currentCarDistance = thisDistance - otherDistance; float projectedDistance = otherTraveledDistance - otherDistance; - if (currentCarDistance < 0 || currentCarDistance > car.brakingDistance + 3f) { + float savetyDistance = 3f + speed * 1.8f; + if (currentCarDistance < 0 || currentCarDistance > car.brakingDistance + savetyDistance) { return false; } - if (currentCarDistance > 0 && currentCarDistance < 3) { + if (currentCarDistance > 0 && currentCarDistance < savetyDistance || + projectedDistance > 0 && projectedDistance < savetyDistance) { Debug.DrawLine(position + 1.5f * Vector3.up, car.position + 2 * Vector3.up, Color.blue, 0f, false); return true; } - if (projectedDistance > 0 && projectedDistance < 4) { - Debug.DrawLine(position + 1.5f * Vector3.up, car.position + 2 * Vector3.up, Color.yellow, 0f, false); - return true; - } - if (needsBraking(Mathf.Min(projectedDistance, currentCarDistance) - 3f, car.speed)) { + if (needsBraking(Mathf.Min(projectedDistance, currentCarDistance) - savetyDistance, car.speed)) { Debug.DrawLine(position + 1.5f * Vector3.up, car.position + 2 * Vector3.up, Color.red, 0f, false); return true; } @@ -191,7 +192,7 @@ } public void step(float deltaTime) { - airResistance = config.carAirResistanceModifier * config.carFrontalArea * config.airDensity * 0.5f; + airResistance = config.carAirResistanceModifier * config.carFrontalArea * config.airDensity * 0.5f / config.carMass; updateBrakingParameters(); float B = - airResistance; float A = - rollingAcceleration; @@ -201,7 +202,7 @@ } else { gameObject.GetComponent().material = config.carAccelerationMaterial; if (speed > 0) { - A += Mathf.Min(config.power / speed, breakawayAcceleration); + A += Mathf.Min(config.power / speed / config.carMass, breakawayAcceleration); } else { A += 1f; } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 5ca7c5f..ee8949e 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -232,7 +232,7 @@ carMass: 1000 carMesh: {fileID: -2432090755550338912, guid: 6358fc72c8f1a5645b372c8756ff9871, type: 3} carMaterial: {fileID: 2100000, guid: 55dd6bb7e89b75f4b9958b0b7bb7c5d2, type: 2} - power: 1000 + power: 10000 travelTimes: [] trafficLights: {fileID: 128843417} cameraControl: {fileID: 1256630291} diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index 32d7522..cd451a7 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -14,7 +14,7 @@ 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; + private static float breakawayAcceleration = 0.9f * g * (float) Mathf.Sqrt(2) / 2f, rollingAcceleration = 0.02f * g; public Car(Route route, Transform parent, Config config) { this.route = route; @@ -88,6 +88,9 @@ if (roadIndex == route.roads.Count - 1) { return false; } + if (Vector3.Distance(position, car.position) > 10f) { + return false; + } Road conflict = car.road; float otherDistance = conflict.path.length - car.roadPositon; for (int i = car.roadIndex; !this.route.roads.Contains(conflict) && i < car.route.roads.Count;) { @@ -104,18 +107,16 @@ float otherTraveledDistance = car.speed * time; float currentCarDistance = thisDistance - otherDistance; float projectedDistance = otherTraveledDistance - otherDistance; - if (currentCarDistance < 0 || currentCarDistance > car.brakingDistance + 3f) { + float savetyDistance = 3f + speed * 1.8f; + if (currentCarDistance < 0 || currentCarDistance > car.brakingDistance + savetyDistance) { return false; } - if (currentCarDistance > 0 && currentCarDistance < 3) { + if (currentCarDistance > 0 && currentCarDistance < savetyDistance || + projectedDistance > 0 && projectedDistance < savetyDistance) { Debug.DrawLine(position + 1.5f * Vector3.up, car.position + 2 * Vector3.up, Color.blue, 0f, false); return true; } - if (projectedDistance > 0 && projectedDistance < 4) { - Debug.DrawLine(position + 1.5f * Vector3.up, car.position + 2 * Vector3.up, Color.yellow, 0f, false); - return true; - } - if (needsBraking(Mathf.Min(projectedDistance, currentCarDistance) - 3f, car.speed)) { + if (needsBraking(Mathf.Min(projectedDistance, currentCarDistance) - savetyDistance, car.speed)) { Debug.DrawLine(position + 1.5f * Vector3.up, car.position + 2 * Vector3.up, Color.red, 0f, false); return true; } @@ -191,7 +192,7 @@ } public void step(float deltaTime) { - airResistance = config.carAirResistanceModifier * config.carFrontalArea * config.airDensity * 0.5f; + airResistance = config.carAirResistanceModifier * config.carFrontalArea * config.airDensity * 0.5f / config.carMass; updateBrakingParameters(); float B = - airResistance; float A = - rollingAcceleration; @@ -201,7 +202,7 @@ } else { gameObject.GetComponent().material = config.carAccelerationMaterial; if (speed > 0) { - A += Mathf.Min(config.power / speed, breakawayAcceleration); + A += Mathf.Min(config.power / speed / config.carMass, breakawayAcceleration); } else { A += 1f; } diff --git a/Assets/Scripts/Config.cs b/Assets/Scripts/Config.cs index 6be79a8..ae87792 100644 --- a/Assets/Scripts/Config.cs +++ b/Assets/Scripts/Config.cs @@ -10,7 +10,7 @@ public float carAirResistanceModifier = 0.3f, carFrontalArea = 4f, airDensity = 1.225f, carMass = 1000f; public Mesh carMesh; public Material carMaterial; - public float power = 100000f; + public float power = 2500f; public List travelTimes = new List(); public RoadNetwork roadNetwork = new RoadNetwork(); public TrafficLights trafficLights;