Problem with Raycast Car Bouncing

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

I followed a tutorial when making the Suspension Calculation since all my other attempts failed, this is the video with timestamp: Making Custom Car Physics in Unity (for Very Very Valet) - YouTube

This is a video showing the bouncing of the car.

For refernce, this is what I am trying to achieve: STUNTAGE devlog#5 - various spring solutions for vehicle suspension - YouTube

If you need more info about the car/code then please say so.
How would I go about fixing this?

6 Likes

if that is happening thay means your stiffness is too strong or your damping is too strong or the raycast is hitting the car itself making it shoot up

1 Like

As shown in the video, the stiffness is set to the lowest possible, but I don’t know about the damping.

EDIT: I made so that it sets the Damping automatically but it still bounces

1 Like

damping could make it do that too. try setting damping manually to a very low value go into 0.001 if you must

1 Like

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?

1 Like

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

1 Like

The same problem occurs when I use :ApplyImpulseAtPosition()

1 Like

it could be how you get the spring’s velocity

try using this function
BasePart:GetVelocityAtPosition (roblox.com)

I already tried that :confused:

This text will be blurred

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
image

-- 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)

edit: video showing it

4 Likes

This should work just fine. You do need to update it every render step though.

i know that… i am showing him the code for the suspension not the entire script

1 Like

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?

you can do the same thing with vector forces

vecotr forces are more stable than impulses in my opinion

Would I use the same output force that you calculated for VectorForces?

yeah, you may want to zero out the X and Z though

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.

image

10 Likes

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.