Hi, I am having a problem with my raycast car. It keeps bouncing and I don’t know how to fix it.
For context, this code loops through every wheel in the car and fires a ray downwards. There is a VectorForce for every wheel in the Root of the car, that’s the VectorForce variable.
This is the calculation for the spring
local SpringDirection = -Root.CFrame.UpVector
local SpringOffset = (Suspension.Rest - SpringLength)
local SpringVelocity = SpringDirection:Dot(WheelVelocity)
local SuspensionForce = (SpringOffset * Car:GetAttribute("Stiffness")) - (SpringVelocity * Car:GetAttribute("Damping"))
VectorForce.Force = Vector3.yAxis * -SuspensionForce
Even if I set it to 0, it still bounces though. It has to be some problem with my calculation, the video was made with unity and this is in roblox. Maybe I have to do some converting?
vector forces are way different than applying impulses in unity (if the video does that)
you can apply an impulse to the part to push it up instead of using vector forces. i believe impulses do not require you to times your force by the mass
BasePart:ApplyImpulseAtPosition() i believe is the function
i managed to recreate the suspension using the unity video you linked and i managed to get it to now bounce
i am using a diamondplate part with attachments at the positions for the springs
-- cframe = the diamondplate part's cframe
-- origin is attachment's worldposition
-- result is the raycast
-- get world space vectors
local springDir = cframe.UpVector
local worldVelocity = mainPart:GetVelocityAtPosition(origin)
-- calculate offset from the raycast
local dist = (origin - result.Position).Magnitude
local offset = height - dist
-- calculate velocity along spring direction
local vel = springDir:Dot(worldVelocity)
-- calculate the magnitude of the dampened spring force
local force = ((offset * stiffness) - (vel * damping)) * mainPart.AssemblyMass
print(force)
-- apply force at localtion, in the direction of springDir
mainPart:ApplyImpulseAtPosition(springDir * force, origin)
wwhat i think is happening is that your ray is short enough that the car jumps, because the BodyVelocity or whatever you’re using has set the Force set too much so then, the car jumps and then it jumps so high that the raycast isn’t hitting the ground anymore so its going back and then again, the same thing
Thank you this works really good! But there is still a problem, It’s still visibly shaking a tiny amount when colliding with walls and such, and I think this is because I am using :ApplyImpulseAtPosition(). As shown in this STUNTAGE devlog#5 - various spring solutions for vehicle suspension - YouTube video, he uses VectorForces for his custom solution to this problem. And I really want it to be similar to his since it is so well made. He has his custom solution and another solution called Simple Harmonic Oscillator applying vector force, and I don’t know if that is hard to make either, its just a little bit of math (not saying I am good at math, its just that the formulas are a little complex for me). Do you think that is doable?
It looks like he’s setting the force to be proportional to the constant. In control theory that’s known as a proportional controller. It isn’t that vector forces are more stable it’s that he is taking feedback into the real world system from the physics, which doesn’t cause the shaking behavior. He seems to be first calculating the force to apply then applying it at the next frame as a change in position. This is the reason why people use springs and prismatic constraints for cars, you don’t need to reinvent the wheel just see this
the stiffness/elasticity of the spring controls how shaky it is, aswell as the stuff that touches the ground (the wheels elasticity, it’s why we drive cars with rubber not concrete)
Aye lads, project has been collecting dust for a while and recently gave away an older version of the sauce to a few devs. So more power to the people.
It’s made using vector forces with attachments and is stable under 5fps.
The main stability trick here is Euler integration. Carrying over the last length of the spring to next frame.
Also the ideal damper to stiffness ratio is ~1:10 for a stable spring. Keep numbers low as higher numbers = higher error.
The rest should be obvious.
Thank you for showing this!
One question: Where is your attachment located? In the middle of the wheel or at the top?
Because currently my attachment is at the top and I had to change the RestLength to Diameter * 1.3 instead to get it to work.