diff --git a/Assets/Materials/accelerating.mat b/Assets/Materials/accelerating.mat new file mode 100644 index 0000000..340722e --- /dev/null +++ b/Assets/Materials/accelerating.mat @@ -0,0 +1,79 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 6 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: accelerating + m_Shader: {fileID: 4800000, guid: f29180dc31569f94f92bb39aa3f95f71, type: 3} + m_ShaderKeywords: + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: [] + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Floats: + - _BumpScale: 1 + - _Cutoff: 0.5 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _GlossMapScale: 1 + - _Glossiness: 0.5 + - _GlossyReflections: 1 + - _Metallic: 0 + - _Mode: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.02 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _UVSec: 0 + - _ZWrite: 1 + m_Colors: + - _Color: {r: 1, g: 1, b: 1, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + - _MainColor: {r: 0.19406372, g: 0.8396226, b: 0.24201539, a: 1} + m_BuildTextureStacks: [] diff --git a/Assets/Materials/accelerating.mat b/Assets/Materials/accelerating.mat new file mode 100644 index 0000000..340722e --- /dev/null +++ b/Assets/Materials/accelerating.mat @@ -0,0 +1,79 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 6 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: accelerating + m_Shader: {fileID: 4800000, guid: f29180dc31569f94f92bb39aa3f95f71, type: 3} + m_ShaderKeywords: + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: [] + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Floats: + - _BumpScale: 1 + - _Cutoff: 0.5 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _GlossMapScale: 1 + - _Glossiness: 0.5 + - _GlossyReflections: 1 + - _Metallic: 0 + - _Mode: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.02 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _UVSec: 0 + - _ZWrite: 1 + m_Colors: + - _Color: {r: 1, g: 1, b: 1, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + - _MainColor: {r: 0.19406372, g: 0.8396226, b: 0.24201539, a: 1} + m_BuildTextureStacks: [] diff --git a/Assets/Materials/accelerating.mat.meta b/Assets/Materials/accelerating.mat.meta new file mode 100644 index 0000000..5e8f100 --- /dev/null +++ b/Assets/Materials/accelerating.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e1e6607970c4d7c42b255d3dbedfe868 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Materials/accelerating.mat b/Assets/Materials/accelerating.mat new file mode 100644 index 0000000..340722e --- /dev/null +++ b/Assets/Materials/accelerating.mat @@ -0,0 +1,79 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 6 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: accelerating + m_Shader: {fileID: 4800000, guid: f29180dc31569f94f92bb39aa3f95f71, type: 3} + m_ShaderKeywords: + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: [] + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Floats: + - _BumpScale: 1 + - _Cutoff: 0.5 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _GlossMapScale: 1 + - _Glossiness: 0.5 + - _GlossyReflections: 1 + - _Metallic: 0 + - _Mode: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.02 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _UVSec: 0 + - _ZWrite: 1 + m_Colors: + - _Color: {r: 1, g: 1, b: 1, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + - _MainColor: {r: 0.19406372, g: 0.8396226, b: 0.24201539, a: 1} + m_BuildTextureStacks: [] diff --git a/Assets/Materials/accelerating.mat.meta b/Assets/Materials/accelerating.mat.meta new file mode 100644 index 0000000..5e8f100 --- /dev/null +++ b/Assets/Materials/accelerating.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e1e6607970c4d7c42b255d3dbedfe868 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 5785c07..c7874df 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -220,10 +220,17 @@ m_EditorClassIdentifier: roadMaterial: {fileID: 2100000, guid: 1b0a930ba0e3dba46a226bb15ae71b80, type: 2} roadEditMaterial: {fileID: 2100000, guid: e2c5b6e79b6cdb545863f245ced42125, type: 2} - carMaterial: {fileID: 2100000, guid: bb482d1941dca4649842d0da3f1120ab, type: 2} + carBrakingMaterial: {fileID: 2100000, guid: bb482d1941dca4649842d0da3f1120ab, type: 2} + carAccelerationMaterial: {fileID: 2100000, guid: e1e6607970c4d7c42b255d3dbedfe868, type: 2} mode: 2 futureMode: 0 highlight: {fileID: 887232956} + carAirResistanceModifier: 0.3 + carFrontalArea: 4 + airDensity: 1.225 + carMass: 1000 + carTorque: 300 + carWheelRadius: 0.25 --- !u!4 &128843416 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/Materials/accelerating.mat b/Assets/Materials/accelerating.mat new file mode 100644 index 0000000..340722e --- /dev/null +++ b/Assets/Materials/accelerating.mat @@ -0,0 +1,79 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 6 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: accelerating + m_Shader: {fileID: 4800000, guid: f29180dc31569f94f92bb39aa3f95f71, type: 3} + m_ShaderKeywords: + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: [] + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Floats: + - _BumpScale: 1 + - _Cutoff: 0.5 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _GlossMapScale: 1 + - _Glossiness: 0.5 + - _GlossyReflections: 1 + - _Metallic: 0 + - _Mode: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.02 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _UVSec: 0 + - _ZWrite: 1 + m_Colors: + - _Color: {r: 1, g: 1, b: 1, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + - _MainColor: {r: 0.19406372, g: 0.8396226, b: 0.24201539, a: 1} + m_BuildTextureStacks: [] diff --git a/Assets/Materials/accelerating.mat.meta b/Assets/Materials/accelerating.mat.meta new file mode 100644 index 0000000..5e8f100 --- /dev/null +++ b/Assets/Materials/accelerating.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e1e6607970c4d7c42b255d3dbedfe868 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 5785c07..c7874df 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -220,10 +220,17 @@ m_EditorClassIdentifier: roadMaterial: {fileID: 2100000, guid: 1b0a930ba0e3dba46a226bb15ae71b80, type: 2} roadEditMaterial: {fileID: 2100000, guid: e2c5b6e79b6cdb545863f245ced42125, type: 2} - carMaterial: {fileID: 2100000, guid: bb482d1941dca4649842d0da3f1120ab, type: 2} + carBrakingMaterial: {fileID: 2100000, guid: bb482d1941dca4649842d0da3f1120ab, type: 2} + carAccelerationMaterial: {fileID: 2100000, guid: e1e6607970c4d7c42b255d3dbedfe868, type: 2} mode: 2 futureMode: 0 highlight: {fileID: 887232956} + carAirResistanceModifier: 0.3 + carFrontalArea: 4 + airDensity: 1.225 + carMass: 1000 + carTorque: 300 + carWheelRadius: 0.25 --- !u!4 &128843416 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index 135a152..73d0a53 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -5,7 +5,7 @@ public class Car { private Route route; public Road currentRoad; - public float roadPositon = 0f, speed = 0f; + public float roadPositon = 0f, speed = 0f, brakingAcceleration, acceleration, airResistance; private int roadIndex = 0; private GameObject gameObject; private Config config; @@ -20,13 +20,17 @@ gameObject = new GameObject(); FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); - gameObject.AddComponent().material = config.carMaterial; + gameObject.AddComponent().material = config.carBrakingMaterial; gameObject.AddComponent().mesh = renderer.mesh; gameObject.transform.parent = parent; CarData carData = gameObject.AddComponent(); carData.car = this; position = currentRoad.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 / + (2*config.carMass); } private void incrementRoad() { @@ -41,18 +45,47 @@ currentRoad = route.roads[roadIndex]; currentRoad.cars.Add(this); } + + 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)); + } + + private bool isBraking() { + float stoppingDistance = 0.5f * speed * speed / brakingAcceleration; + Road road = currentRoad; + float position = roadPositon; + int currentRoadIndex = roadIndex; + for (int i = 1; i <= 10; i++) { + position += stoppingDistance / 10; + while (position >= road.path.length) { + currentRoadIndex ++; + if (currentRoadIndex == route.roads.Count) { + return false; + } + position -= road.path.length; + road = route.roads[currentRoadIndex]; + } + float deltaV = speed - getMaxSpeed(road, position); + float distanceNeededToBrake = speed * speed / brakingAcceleration + - 0.5f * deltaV * deltaV / brakingAcceleration; + if (stoppingDistance / 10 * i < distanceNeededToBrake) { + return true; + } + } + return false; + } 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 t = currentRoad.path.getT(roadPositon); - float maxSpeed = Mathf.Sqrt((float) (-Mathf.Abs(currentRoad.path.getRadius(t)) / 0.2 * Physics.gravity.y)); - speed = Mathf.Min(speed, maxSpeed); - float distance = deltaTime * speed; - roadPositon += distance; + speed -= airResistance * deltaTime * speed * speed; + if (isBraking()) { + speed -= brakingAcceleration * deltaTime; + gameObject.GetComponent().material = config.carBrakingMaterial; + } else { + speed += acceleration * deltaTime; + gameObject.GetComponent().material = config.carAccelerationMaterial; + } + roadPositon += deltaTime * speed; while (roadPositon > currentRoad.path.length) { incrementRoad(); } diff --git a/Assets/Materials/accelerating.mat b/Assets/Materials/accelerating.mat new file mode 100644 index 0000000..340722e --- /dev/null +++ b/Assets/Materials/accelerating.mat @@ -0,0 +1,79 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 6 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: accelerating + m_Shader: {fileID: 4800000, guid: f29180dc31569f94f92bb39aa3f95f71, type: 3} + m_ShaderKeywords: + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: [] + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Floats: + - _BumpScale: 1 + - _Cutoff: 0.5 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _GlossMapScale: 1 + - _Glossiness: 0.5 + - _GlossyReflections: 1 + - _Metallic: 0 + - _Mode: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.02 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _UVSec: 0 + - _ZWrite: 1 + m_Colors: + - _Color: {r: 1, g: 1, b: 1, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + - _MainColor: {r: 0.19406372, g: 0.8396226, b: 0.24201539, a: 1} + m_BuildTextureStacks: [] diff --git a/Assets/Materials/accelerating.mat.meta b/Assets/Materials/accelerating.mat.meta new file mode 100644 index 0000000..5e8f100 --- /dev/null +++ b/Assets/Materials/accelerating.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e1e6607970c4d7c42b255d3dbedfe868 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 5785c07..c7874df 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -220,10 +220,17 @@ m_EditorClassIdentifier: roadMaterial: {fileID: 2100000, guid: 1b0a930ba0e3dba46a226bb15ae71b80, type: 2} roadEditMaterial: {fileID: 2100000, guid: e2c5b6e79b6cdb545863f245ced42125, type: 2} - carMaterial: {fileID: 2100000, guid: bb482d1941dca4649842d0da3f1120ab, type: 2} + carBrakingMaterial: {fileID: 2100000, guid: bb482d1941dca4649842d0da3f1120ab, type: 2} + carAccelerationMaterial: {fileID: 2100000, guid: e1e6607970c4d7c42b255d3dbedfe868, type: 2} mode: 2 futureMode: 0 highlight: {fileID: 887232956} + carAirResistanceModifier: 0.3 + carFrontalArea: 4 + airDensity: 1.225 + carMass: 1000 + carTorque: 300 + carWheelRadius: 0.25 --- !u!4 &128843416 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index 135a152..73d0a53 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -5,7 +5,7 @@ public class Car { private Route route; public Road currentRoad; - public float roadPositon = 0f, speed = 0f; + public float roadPositon = 0f, speed = 0f, brakingAcceleration, acceleration, airResistance; private int roadIndex = 0; private GameObject gameObject; private Config config; @@ -20,13 +20,17 @@ gameObject = new GameObject(); FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); - gameObject.AddComponent().material = config.carMaterial; + gameObject.AddComponent().material = config.carBrakingMaterial; gameObject.AddComponent().mesh = renderer.mesh; gameObject.transform.parent = parent; CarData carData = gameObject.AddComponent(); carData.car = this; position = currentRoad.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 / + (2*config.carMass); } private void incrementRoad() { @@ -41,18 +45,47 @@ currentRoad = route.roads[roadIndex]; currentRoad.cars.Add(this); } + + 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)); + } + + private bool isBraking() { + float stoppingDistance = 0.5f * speed * speed / brakingAcceleration; + Road road = currentRoad; + float position = roadPositon; + int currentRoadIndex = roadIndex; + for (int i = 1; i <= 10; i++) { + position += stoppingDistance / 10; + while (position >= road.path.length) { + currentRoadIndex ++; + if (currentRoadIndex == route.roads.Count) { + return false; + } + position -= road.path.length; + road = route.roads[currentRoadIndex]; + } + float deltaV = speed - getMaxSpeed(road, position); + float distanceNeededToBrake = speed * speed / brakingAcceleration + - 0.5f * deltaV * deltaV / brakingAcceleration; + if (stoppingDistance / 10 * i < distanceNeededToBrake) { + return true; + } + } + return false; + } 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 t = currentRoad.path.getT(roadPositon); - float maxSpeed = Mathf.Sqrt((float) (-Mathf.Abs(currentRoad.path.getRadius(t)) / 0.2 * Physics.gravity.y)); - speed = Mathf.Min(speed, maxSpeed); - float distance = deltaTime * speed; - roadPositon += distance; + speed -= airResistance * deltaTime * speed * speed; + if (isBraking()) { + speed -= brakingAcceleration * deltaTime; + gameObject.GetComponent().material = config.carBrakingMaterial; + } else { + speed += acceleration * deltaTime; + gameObject.GetComponent().material = config.carAccelerationMaterial; + } + roadPositon += deltaTime * speed; while (roadPositon > currentRoad.path.length) { incrementRoad(); } diff --git a/Assets/Scripts/Config.cs b/Assets/Scripts/Config.cs index 83d3aa8..78721fb 100644 --- a/Assets/Scripts/Config.cs +++ b/Assets/Scripts/Config.cs @@ -3,7 +3,7 @@ using UnityEngine; public class Config : MonoBehaviour { - public Material roadMaterial, roadEditMaterial, carMaterial; + public Material roadMaterial, roadEditMaterial, carBrakingMaterial, carAccelerationMaterial; public Mode mode = Mode.DeleteRoad; public Mode futureMode; public RectTransform highlight; diff --git a/Assets/Materials/accelerating.mat b/Assets/Materials/accelerating.mat new file mode 100644 index 0000000..340722e --- /dev/null +++ b/Assets/Materials/accelerating.mat @@ -0,0 +1,79 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 6 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: accelerating + m_Shader: {fileID: 4800000, guid: f29180dc31569f94f92bb39aa3f95f71, type: 3} + m_ShaderKeywords: + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: [] + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Floats: + - _BumpScale: 1 + - _Cutoff: 0.5 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _GlossMapScale: 1 + - _Glossiness: 0.5 + - _GlossyReflections: 1 + - _Metallic: 0 + - _Mode: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.02 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _UVSec: 0 + - _ZWrite: 1 + m_Colors: + - _Color: {r: 1, g: 1, b: 1, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + - _MainColor: {r: 0.19406372, g: 0.8396226, b: 0.24201539, a: 1} + m_BuildTextureStacks: [] diff --git a/Assets/Materials/accelerating.mat.meta b/Assets/Materials/accelerating.mat.meta new file mode 100644 index 0000000..5e8f100 --- /dev/null +++ b/Assets/Materials/accelerating.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e1e6607970c4d7c42b255d3dbedfe868 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 5785c07..c7874df 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -220,10 +220,17 @@ m_EditorClassIdentifier: roadMaterial: {fileID: 2100000, guid: 1b0a930ba0e3dba46a226bb15ae71b80, type: 2} roadEditMaterial: {fileID: 2100000, guid: e2c5b6e79b6cdb545863f245ced42125, type: 2} - carMaterial: {fileID: 2100000, guid: bb482d1941dca4649842d0da3f1120ab, type: 2} + carBrakingMaterial: {fileID: 2100000, guid: bb482d1941dca4649842d0da3f1120ab, type: 2} + carAccelerationMaterial: {fileID: 2100000, guid: e1e6607970c4d7c42b255d3dbedfe868, type: 2} mode: 2 futureMode: 0 highlight: {fileID: 887232956} + carAirResistanceModifier: 0.3 + carFrontalArea: 4 + airDensity: 1.225 + carMass: 1000 + carTorque: 300 + carWheelRadius: 0.25 --- !u!4 &128843416 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index 135a152..73d0a53 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -5,7 +5,7 @@ public class Car { private Route route; public Road currentRoad; - public float roadPositon = 0f, speed = 0f; + public float roadPositon = 0f, speed = 0f, brakingAcceleration, acceleration, airResistance; private int roadIndex = 0; private GameObject gameObject; private Config config; @@ -20,13 +20,17 @@ gameObject = new GameObject(); FlatCircleRenderer renderer = new FlatCircleRenderer(0.8f, 0.2f, 32); - gameObject.AddComponent().material = config.carMaterial; + gameObject.AddComponent().material = config.carBrakingMaterial; gameObject.AddComponent().mesh = renderer.mesh; gameObject.transform.parent = parent; CarData carData = gameObject.AddComponent(); carData.car = this; position = currentRoad.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 / + (2*config.carMass); } private void incrementRoad() { @@ -41,18 +45,47 @@ currentRoad = route.roads[roadIndex]; currentRoad.cars.Add(this); } + + 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)); + } + + private bool isBraking() { + float stoppingDistance = 0.5f * speed * speed / brakingAcceleration; + Road road = currentRoad; + float position = roadPositon; + int currentRoadIndex = roadIndex; + for (int i = 1; i <= 10; i++) { + position += stoppingDistance / 10; + while (position >= road.path.length) { + currentRoadIndex ++; + if (currentRoadIndex == route.roads.Count) { + return false; + } + position -= road.path.length; + road = route.roads[currentRoadIndex]; + } + float deltaV = speed - getMaxSpeed(road, position); + float distanceNeededToBrake = speed * speed / brakingAcceleration + - 0.5f * deltaV * deltaV / brakingAcceleration; + if (stoppingDistance / 10 * i < distanceNeededToBrake) { + return true; + } + } + return false; + } 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 t = currentRoad.path.getT(roadPositon); - float maxSpeed = Mathf.Sqrt((float) (-Mathf.Abs(currentRoad.path.getRadius(t)) / 0.2 * Physics.gravity.y)); - speed = Mathf.Min(speed, maxSpeed); - float distance = deltaTime * speed; - roadPositon += distance; + speed -= airResistance * deltaTime * speed * speed; + if (isBraking()) { + speed -= brakingAcceleration * deltaTime; + gameObject.GetComponent().material = config.carBrakingMaterial; + } else { + speed += acceleration * deltaTime; + gameObject.GetComponent().material = config.carAccelerationMaterial; + } + roadPositon += deltaTime * speed; while (roadPositon > currentRoad.path.length) { incrementRoad(); } diff --git a/Assets/Scripts/Config.cs b/Assets/Scripts/Config.cs index 83d3aa8..78721fb 100644 --- a/Assets/Scripts/Config.cs +++ b/Assets/Scripts/Config.cs @@ -3,7 +3,7 @@ using UnityEngine; public class Config : MonoBehaviour { - public Material roadMaterial, roadEditMaterial, carMaterial; + public Material roadMaterial, roadEditMaterial, carBrakingMaterial, carAccelerationMaterial; public Mode mode = Mode.DeleteRoad; public Mode futureMode; public RectTransform highlight; diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs index 3f03e46..c8642bd 100644 --- a/Assets/Scripts/Primitives/Bezier.cs +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -39,7 +39,7 @@ public float getT(float distance) { int index = 0; - while (!(lengths[index] <= distance && lengths[index+1] > distance)) { + while (!(lengths[index] <= distance && lengths[index+1] > distance) && index < perscision-1) { index++; } float lengthBefore = lengths[index];