Raycast suspension force, need help with correcting calculations

I notice that my raycast suspension doesnt add the wheelRadius, which means the ray is only goes to the length of the suspension length, so if the suspension length is 0, the car falls flat on the ground instead of it floating at the wheel Radius height

I’m not sure if that made since, here is some images

here are the car settings
CarScreenShot3


as your see the spring length is 3 but the car is clearly not at the height of 3 plus the wheel Radius


in this image, as you see the red mark is how high the suspension should be, which is springLength + wheelRadius


heres is what I want, as you see the black ball represents where the springLength ends
and then the rest of the ray is the wheelRadius

This is what im trying to acheive. (a image from a youtube vidoe)
image_2022-10-20_160819354


heres my code so far


		local rayOrigin = attachment.WorldPosition
		local rayDirection = -mainBody.CFrame.UpVector * (springLength + wheelRadius)
		local raycast = workspace:Raycast(rayOrigin, rayDirection, rayParams)

		local rayPosition = raycast and raycast.Position or rayOrigin + rayDirection
		local rayDistance = raycast and (rayOrigin - rayPosition).Magnitude or springLength

		-- [ spring force ]
		if raycast then	
			local springDir : Vector3 = mainBody.CFrame.UpVector
			
			local springOffset : number = (springLength - rayDistance)
			local springVelocity : number = springDir:Dot(originVelocity)
			
			local force : number = (springOffset * stiffness) - (springVelocity * damping)
			springForce.Force = springDir * force
		end

I don’t have much time left so ill be back in 6-7 hours, if someone can help me fix this, it’ll mean a lot to me.

feel free to take the code and test it on a simple part and make some visualizers for it and try to fix it

1 Like

ok I pretty much fixed the problem, lol I changed my code to this like countless times but then the car kept bouncing and I now realized that I need to up the damping and lower the force being applied

as you see in this image, the springlength is 2 and wheelradius is 2 and the black ball is pretty much in the center so ill just keep it like this for now

here’s how fixed it, this is the calculation for getting the spring offset

local springMaxLength = springLength + wheelRadius
local springOffset : number = (springMaxLength - rayDistance) + wheelRadius

but as you see, I pretty much got the same result in this image
Screenshot 2022-10-20 160728

2 Likes