I'm trying to create a raycast suspension car, but I have ran into a very strange issue; when the car moves/accelerates it seemingly gets pulled down to the ground, and when it stops it's fine again. Here is the car in action:
(Console is printing current spring velocity)
Here is the spring code:
Vector3 springdir = wheel.GlobalTransform.Basis.Y;
float length = wheel.GlobalPosition.DistanceTo(wheel.GetCollisionPoint());
float springvel = springdir.Dot(getpointvel(wheel.GlobalPosition));
float offset = (restlength-length);
float force = (offset*stiffness)-(springvel*damping);
ApplyForce(force*springdir,wheel.Position);
This is the "getpointvel" function:
private Vector3 getpointvel(Vector3 point)
{
return LinearVelocity+AngularVelocity.Cross(point-GlobalTransform.Origin);
}
I tried replacing the point velocity with the object space linear velocity's Y, but the problem still remained, so I'm guessing the problem is caused by either the way I calculate the distance from raycast origin to ground or I'm doing the dot product wrong. Any help is appreciated