diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index ae26c58..49a7a90 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -30,7 +30,7 @@ } private void incrementRoad() { - roadPositon -= 1f; + roadPositon -= currentRoad.path.length; roadIndex++; if (roadIndex == route.roads.Count) { GameObject.Destroy(gameObject); @@ -49,19 +49,10 @@ (config.carTorque*deltaTime) / (config.carWheelRadius * config.carMass) ; float distance = deltaTime * speed; - float currentDeltaT = distance / currentRoad.path.length; - currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; - for (int i = 0; i < 10; i++) { - currentDeltaT += (distance - currentDistance) / currentRoad.path.length; - while (roadPositon + currentDeltaT > 1f) { - incrementRoad(); - } - currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; - } - roadPositon += currentDeltaT; - while (roadPositon > 1f) { + roadPositon += distance; + while (roadPositon > currentRoad.path.length) { incrementRoad(); } - position = currentRoad.path.getPosition(roadPositon); + position = currentRoad.path.getPosition(currentRoad.path.getT(roadPositon)); } } diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index ae26c58..49a7a90 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -30,7 +30,7 @@ } private void incrementRoad() { - roadPositon -= 1f; + roadPositon -= currentRoad.path.length; roadIndex++; if (roadIndex == route.roads.Count) { GameObject.Destroy(gameObject); @@ -49,19 +49,10 @@ (config.carTorque*deltaTime) / (config.carWheelRadius * config.carMass) ; float distance = deltaTime * speed; - float currentDeltaT = distance / currentRoad.path.length; - currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; - for (int i = 0; i < 10; i++) { - currentDeltaT += (distance - currentDistance) / currentRoad.path.length; - while (roadPositon + currentDeltaT > 1f) { - incrementRoad(); - } - currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; - } - roadPositon += currentDeltaT; - while (roadPositon > 1f) { + roadPositon += distance; + while (roadPositon > currentRoad.path.length) { incrementRoad(); } - position = currentRoad.path.getPosition(roadPositon); + position = currentRoad.path.getPosition(currentRoad.path.getT(roadPositon)); } } diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs index b0e9531..521b864 100644 --- a/Assets/Scripts/Primitives/Bezier.cs +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -5,6 +5,8 @@ public class Bezier { public Vector3 A, B, C, D; public float length; + // t(distance) = ??? + public NewtonPolynom TOfDistance = new NewtonPolynom(); public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { this.A = A; @@ -33,15 +35,22 @@ public void updateLength() { length = 0f; - float steps = 50f; + float steps = 5f; Vector3 previous = A; + TOfDistance = new NewtonPolynom(); + TOfDistance.add(0f, 0f); for (int i = 1; i <= steps; i++) { Vector3 current = getPosition(i / steps); length += (current - previous).magnitude; previous = current; + TOfDistance.add(length, i/steps); } } + public float getT(float distance) { + return TOfDistance.evaluate(distance); + } + 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 ae26c58..49a7a90 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -30,7 +30,7 @@ } private void incrementRoad() { - roadPositon -= 1f; + roadPositon -= currentRoad.path.length; roadIndex++; if (roadIndex == route.roads.Count) { GameObject.Destroy(gameObject); @@ -49,19 +49,10 @@ (config.carTorque*deltaTime) / (config.carWheelRadius * config.carMass) ; float distance = deltaTime * speed; - float currentDeltaT = distance / currentRoad.path.length; - currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; - for (int i = 0; i < 10; i++) { - currentDeltaT += (distance - currentDistance) / currentRoad.path.length; - while (roadPositon + currentDeltaT > 1f) { - incrementRoad(); - } - currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; - } - roadPositon += currentDeltaT; - while (roadPositon > 1f) { + roadPositon += distance; + while (roadPositon > currentRoad.path.length) { incrementRoad(); } - position = currentRoad.path.getPosition(roadPositon); + position = currentRoad.path.getPosition(currentRoad.path.getT(roadPositon)); } } diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs index b0e9531..521b864 100644 --- a/Assets/Scripts/Primitives/Bezier.cs +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -5,6 +5,8 @@ public class Bezier { public Vector3 A, B, C, D; public float length; + // t(distance) = ??? + public NewtonPolynom TOfDistance = new NewtonPolynom(); public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { this.A = A; @@ -33,15 +35,22 @@ public void updateLength() { length = 0f; - float steps = 50f; + float steps = 5f; Vector3 previous = A; + TOfDistance = new NewtonPolynom(); + TOfDistance.add(0f, 0f); for (int i = 1; i <= steps; i++) { Vector3 current = getPosition(i / steps); length += (current - previous).magnitude; previous = current; + TOfDistance.add(length, i/steps); } } + public float getT(float distance) { + return TOfDistance.evaluate(distance); + } + public Vector3 getDirection(float t) { float T = 1.0f - t; return diff --git a/Assets/Scripts/Primitives/NewtonPolynom.cs b/Assets/Scripts/Primitives/NewtonPolynom.cs new file mode 100644 index 0000000..1a0e2be --- /dev/null +++ b/Assets/Scripts/Primitives/NewtonPolynom.cs @@ -0,0 +1,34 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + + +public class NewtonPolynom { + private List coefficients = new List(); + private List xValues = new List(), yValues = new List(); + + public NewtonPolynom() { + } + + public void add(float x, float y) { + xValues.Add(x); + yValues.Add(y); + coefficients.Add(getCoefficient(coefficients.Count, 0)); + } + + private float getCoefficient(int x, int y) { + if (x == 0) { + return yValues[y]; + } + return (getCoefficient(x-1, y+1) - getCoefficient(x-1, y)) / (xValues[x+y] - xValues[y]); + } + + public float evaluate(float x) { + float current = 0; + for (int i = 0; i < coefficients.Count; i++) { + int index = coefficients.Count-1-i; + current = coefficients[index] + (x - xValues[index]) * current; + } + return current; + } +} diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index ae26c58..49a7a90 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -30,7 +30,7 @@ } private void incrementRoad() { - roadPositon -= 1f; + roadPositon -= currentRoad.path.length; roadIndex++; if (roadIndex == route.roads.Count) { GameObject.Destroy(gameObject); @@ -49,19 +49,10 @@ (config.carTorque*deltaTime) / (config.carWheelRadius * config.carMass) ; float distance = deltaTime * speed; - float currentDeltaT = distance / currentRoad.path.length; - currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; - for (int i = 0; i < 10; i++) { - currentDeltaT += (distance - currentDistance) / currentRoad.path.length; - while (roadPositon + currentDeltaT > 1f) { - incrementRoad(); - } - currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; - } - roadPositon += currentDeltaT; - while (roadPositon > 1f) { + roadPositon += distance; + while (roadPositon > currentRoad.path.length) { incrementRoad(); } - position = currentRoad.path.getPosition(roadPositon); + position = currentRoad.path.getPosition(currentRoad.path.getT(roadPositon)); } } diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs index b0e9531..521b864 100644 --- a/Assets/Scripts/Primitives/Bezier.cs +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -5,6 +5,8 @@ public class Bezier { public Vector3 A, B, C, D; public float length; + // t(distance) = ??? + public NewtonPolynom TOfDistance = new NewtonPolynom(); public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { this.A = A; @@ -33,15 +35,22 @@ public void updateLength() { length = 0f; - float steps = 50f; + float steps = 5f; Vector3 previous = A; + TOfDistance = new NewtonPolynom(); + TOfDistance.add(0f, 0f); for (int i = 1; i <= steps; i++) { Vector3 current = getPosition(i / steps); length += (current - previous).magnitude; previous = current; + TOfDistance.add(length, i/steps); } } + public float getT(float distance) { + return TOfDistance.evaluate(distance); + } + public Vector3 getDirection(float t) { float T = 1.0f - t; return diff --git a/Assets/Scripts/Primitives/NewtonPolynom.cs b/Assets/Scripts/Primitives/NewtonPolynom.cs new file mode 100644 index 0000000..1a0e2be --- /dev/null +++ b/Assets/Scripts/Primitives/NewtonPolynom.cs @@ -0,0 +1,34 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + + +public class NewtonPolynom { + private List coefficients = new List(); + private List xValues = new List(), yValues = new List(); + + public NewtonPolynom() { + } + + public void add(float x, float y) { + xValues.Add(x); + yValues.Add(y); + coefficients.Add(getCoefficient(coefficients.Count, 0)); + } + + private float getCoefficient(int x, int y) { + if (x == 0) { + return yValues[y]; + } + return (getCoefficient(x-1, y+1) - getCoefficient(x-1, y)) / (xValues[x+y] - xValues[y]); + } + + public float evaluate(float x) { + float current = 0; + for (int i = 0; i < coefficients.Count; i++) { + int index = coefficients.Count-1-i; + current = coefficients[index] + (x - xValues[index]) * current; + } + return current; + } +} diff --git a/Assets/Scripts/Primitives/NewtonPolynom.cs.meta b/Assets/Scripts/Primitives/NewtonPolynom.cs.meta new file mode 100644 index 0000000..7a13d48 --- /dev/null +++ b/Assets/Scripts/Primitives/NewtonPolynom.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 91cc699c03ef4f947a332c54b936f2fc +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Car/Car.cs b/Assets/Scripts/Car/Car.cs index ae26c58..49a7a90 100644 --- a/Assets/Scripts/Car/Car.cs +++ b/Assets/Scripts/Car/Car.cs @@ -30,7 +30,7 @@ } private void incrementRoad() { - roadPositon -= 1f; + roadPositon -= currentRoad.path.length; roadIndex++; if (roadIndex == route.roads.Count) { GameObject.Destroy(gameObject); @@ -49,19 +49,10 @@ (config.carTorque*deltaTime) / (config.carWheelRadius * config.carMass) ; float distance = deltaTime * speed; - float currentDeltaT = distance / currentRoad.path.length; - currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; - for (int i = 0; i < 10; i++) { - currentDeltaT += (distance - currentDistance) / currentRoad.path.length; - while (roadPositon + currentDeltaT > 1f) { - incrementRoad(); - } - currentDistance = (position - currentRoad.path.getPosition(roadPositon+currentDeltaT)).magnitude; - } - roadPositon += currentDeltaT; - while (roadPositon > 1f) { + roadPositon += distance; + while (roadPositon > currentRoad.path.length) { incrementRoad(); } - position = currentRoad.path.getPosition(roadPositon); + position = currentRoad.path.getPosition(currentRoad.path.getT(roadPositon)); } } diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs index b0e9531..521b864 100644 --- a/Assets/Scripts/Primitives/Bezier.cs +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -5,6 +5,8 @@ public class Bezier { public Vector3 A, B, C, D; public float length; + // t(distance) = ??? + public NewtonPolynom TOfDistance = new NewtonPolynom(); public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { this.A = A; @@ -33,15 +35,22 @@ public void updateLength() { length = 0f; - float steps = 50f; + float steps = 5f; Vector3 previous = A; + TOfDistance = new NewtonPolynom(); + TOfDistance.add(0f, 0f); for (int i = 1; i <= steps; i++) { Vector3 current = getPosition(i / steps); length += (current - previous).magnitude; previous = current; + TOfDistance.add(length, i/steps); } } + public float getT(float distance) { + return TOfDistance.evaluate(distance); + } + public Vector3 getDirection(float t) { float T = 1.0f - t; return diff --git a/Assets/Scripts/Primitives/NewtonPolynom.cs b/Assets/Scripts/Primitives/NewtonPolynom.cs new file mode 100644 index 0000000..1a0e2be --- /dev/null +++ b/Assets/Scripts/Primitives/NewtonPolynom.cs @@ -0,0 +1,34 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + + +public class NewtonPolynom { + private List coefficients = new List(); + private List xValues = new List(), yValues = new List(); + + public NewtonPolynom() { + } + + public void add(float x, float y) { + xValues.Add(x); + yValues.Add(y); + coefficients.Add(getCoefficient(coefficients.Count, 0)); + } + + private float getCoefficient(int x, int y) { + if (x == 0) { + return yValues[y]; + } + return (getCoefficient(x-1, y+1) - getCoefficient(x-1, y)) / (xValues[x+y] - xValues[y]); + } + + public float evaluate(float x) { + float current = 0; + for (int i = 0; i < coefficients.Count; i++) { + int index = coefficients.Count-1-i; + current = coefficients[index] + (x - xValues[index]) * current; + } + return current; + } +} diff --git a/Assets/Scripts/Primitives/NewtonPolynom.cs.meta b/Assets/Scripts/Primitives/NewtonPolynom.cs.meta new file mode 100644 index 0000000..7a13d48 --- /dev/null +++ b/Assets/Scripts/Primitives/NewtonPolynom.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 91cc699c03ef4f947a332c54b936f2fc +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Road.cs b/Assets/Scripts/Roads/Road.cs index c6d5c5e..08be76d 100644 --- a/Assets/Scripts/Roads/Road.cs +++ b/Assets/Scripts/Roads/Road.cs @@ -101,9 +101,6 @@ arrow1Renderer.update(); arrow2Renderer.update(); path.updateLength(); - foreach (Car car in cars) { - car.position = path.getPosition(car.roadPositon); - } } override public bool Equals(object other) {