Car Suspension (Scripted)

That’s the point I’m stuck at.

1 Like

That’s exactly what I wrote.

I know, however that isn’t applied to the example made by roblox. I want to know how said example makes this particular part work.

Surely, if you move the wheel up using the weld cframe, the thruster would be pushing down and so it wouldn’t allow the wheel to move back down.

1 Like

Don’t use the one made by Roblox. You identified the purpose of the project as not using spring constraints and creating the spring simulation via a script.


Edit:

You’re not supposed to be CFraming anything. You’re going to use 4 attachments and 4 force constraints for the chasis. You’re going to use an attachment and force constraint for each wheel and connect the chasis and wheels using something like prismatic constraint.

You’re supposed to simulate the force of suspension on script and apply it via force objects.

3 Likes

The roblox example doesn’t use spring constraints. It’s purely code & body thrusters.

2 Likes

Ok… So what is it that you exactly want? Roblox’s example has a different approach to the equations than I do. UpdateThruster function in the LocalCarScript is the place where it does that.

--If we're on the ground, apply some forces to push the wheel up
bodyThrust.force = Vector3.new(0, ((stats.Height.Value - thrusterHeight)^2) * (force / stats.Height.Value^2), 0)
local thrusterDamping = thruster.CFrame:toObjectSpace(CFrame.new(thruster.Velocity + thruster.Position)).p * damping
bodyThrust.force = bodyThrust.force - Vector3.new(0, thrusterDamping.Y, 0)

but the one I suggested would have more realistic physics.

2 Likes

The awesome cars in the labs were made by @NWSpacek and kindly given to me, although I have since developed my own versions to use.

As far as I can remember the code works like this:

  1. Raycast from each thruster in the corner of the chassis
  2. Calculate the upwards force in each thruster from the length of the raycast, use either the code already in it or Oseday’s. Probably use Oseday’s, there’s a few problems in Roblox’s code if the car isn’t level.
  3. Change the wheel weld (the wheels are concollide off) using the raycast length to appear to be touching the surface of the ground.
  4. I use BodyThrust for propulsion and turning.
10 Likes

AH! So that’s how it works! I didn’t realise the wheels were cancollide off! That makes so much sense now!

With the wheels being cancollide off, it’s all based on thruster work which makes the thruster value easily able to act as if the wheels are cancollide on.

I will probably experiment with @Oseday’s Spring Physics as I think it looks cool too. I just wanted to understand exactly the process to make it work so that the rest is easier to understand.

I appreciate the help you’ve given me @madattak @Oseday. I think my next goal is to try and somehow have a go at making those wonderful hover cars that @NWSpacek made.

6 Likes

Good lord what is happening in there! Why not just use the PrismaticConstraint?

1 Like

Using scripts to simulate your springs

  • Uses far less resources
  • Done right, is far less prone to bugging out
  • Provides greater control of behaviour to the creator

Edit: Woops, first accidental necro bump I’ve done for a while. Teaches me to touch that ‘suggested topics’ list!

2020 Edit: For future reference, improvements to Roblox’s physics mean constraint vehicles are now likely to be a lot more stable and less likely to bug out and than code springs.

12 Likes

Hi, I know this is old Thread, but Looking at your code, I’m having trouble to understand How applying same force to wheel and car should work.

I opened new thread about same topic here. I’m having trouble figuring out, exactly how should I handle constraints and Forces. I’ve made hovercar and it works great, but making normal vehicles I find confusing. Too many constraints .

3 Likes

It’s exactly the same as a hover car, but you create cancollide off wheels, weld them to the thrusters, and change the C0 of the weld every frame so that they appear to be touching the ground.

3 Likes

How exactly would I change C0? I mean based on what? distance between the ground and car?

1 Like

Here’s what I’ve achieved so far:
Car.rbxm (14.7 KB)
With local script it doesn’t work as it would with server script and I don’t like it, but here it is.
But it’s still awful with serverScript.
Also I’m wondering how are you going to account for friction while driving the car.

You’ve got to code the friction mechanics yourself. If you’re not experienced with scripting in Roblox I would recommend searching for a constraint chassis instead (For example, doing a quick search of the devforum gave this: [Update] Constraint Chassis) as while I do firmly believe that scripted suspension vehicles are better overall, they take a lot of time and experience to make them work to a high quality.

1 Like

How can I prevent this from happening?

it sort of stutters and doesn’t tilt enough

local X = 6 --height
local K = 2000    
local B = 300


local y = i.Part.Position.Y     
local V = i.Part.Velocity.Y

local x0 = (y - hit_position.Y)

ST.d2x = ST.dx 
ST.dx = X-x0

local c = 2*math.sqrt(Part.Parent.Parent.Engine:GetMass() * K)

f = K*(X-x0)- ((X-x0)-ST.d2x)*c - B*V

this is how I calculate force. The result often is not enough to push car up so I multiply it by 4 and I believe this causes stuttering, but otherwise I don’t know how to calculate the force. I’ve tried many things. Changing x0, changing ST.dx to y position instead of X-x0 and other things, but haven’t solved it. My hovercar with same formula works great, So I assume it has something to do with the wheels.

Note:Part is edge thruster

Part.Weld.C1 = CFrame.new(0, math.abs((y - hit_position.Y) - i.Wheel.Size.Y/2), 0)
2 Likes

wouldn’t it be better to raycast down from the thrusters perspective?

so instead you would do

ray.new(thruster.position, - thruster.CFrame.upVector * height)

(I accidently pushed Shift+Enter and posted early.)

1 Like

This simply doesn’t work because suspension height is an integer, not a vec3.

1 Like

it would work, It would multiply the vector3 by the integer. I’m bad at explaining so just test it in studio for a more clear result.

For a good example of how this sort of stuff works, I have a file that features a custom physics engine and all the necessary bits and pieces to create a fully working vehicle (Including suspension)

You can look more into how suspension is handled through the function ‘bk’ in game.ReplicatedStorage.Module.Chassis (Line 526)