How do you code friction for a raycast suspension vehicle?

I have been working on a car system lately, and I have been stuck for a long time on this one part, and that is coding friction for the vehicle. What happens is you enter the seat, then the vehicle starts sliding around. I could use constraint vehicles, but those are uncontrollable at slight speeds, not realistic, hard to get them working right, and less math.

Here is the if needed:

https://www.roblox.com/library/4297805436/I-need-help

Where to put the items

Put the VehicleReplicationClient in replicated first, Put the folder in replicated first as well, and put the remote event in replicated storage. Also, don’t forget to go to Car.EngineBlock.ConfigModule and set it’s value to the modulescript in the folder

Hi, the model is still private :stuck_out_tongue: Mind updating that and i can take a gander?

2 Likes

Fixed! Sorry about that. :confused:

30 CHars

I suggest watching this video:

It mentions, that for drifting, it tells to take the right/left velocity of the part, and apply a negative force to it. ex.

local frictionForce = part.Velocity.X * (0.5 * dt) -- replace 0.5 with a number you find is right
bodyMover.Velocity = -frictionForce
7 Likes

Using f = -mv is basically the standard for drag for most video games. Real life aerodynamic drag is a bit more nuanced (nonlinear), but a simple linear model is suitable for almost every video-game use case.

Just a little physics terminology though; friction is usually a relatively constant force while drag is ‘proportional’ to velocity.

2 Likes

So the example he is showing is simulated physics, and not the way car traction is in real life?

What he is coding is essentially drag, which is not friction. Drag is like when you are swimming; the faster you swim, the more resistance you feel from the water. Same thing happens in air, when faster you drive the harder your car has to work to fight against the air.

I have not looked at your specific case, but there are two general implementations you could do for ROBLOX (assuming your are using Physics based cars). One is that you can modulate (chagee) your car tires friction coefficient based on a realistic model. You would only need to vary it based on tire-temperature and other factors, as ROBLOX already handles tire torque-slip pretty well (f=ma with static friction).

If you are doing what some other cars do on ROBLOX (essentially provide your own BodyForce for each wheel), then you need to take a different approach. Basically, your car is probably modeled right now as just F=ma; which turns in a=F/m. The more force you put in, the faster your car accelerates. However, this means you can put a ton of force into the car and then it instantly accelerates! But in real life, the tires slip and the car doesn’t instantly speed up. So how can we fix this?

In physics, the reason a tire actually works is because there is friction. A cars acceleration is entirely based around this friction force. Imagine if you had a car on some ice (friction-less magic ice), when you drive forward there is no friction force generated by the tire. This means no acceleration, so your car stays in place while the tires just spin aimlessly. Simple static friction models in physics for wheels just use a piece-wise; if your F_tire (friction force from tire) is above a threshold, it just limits it to the max friction force (with F_tire being calculated through F = uN; Friction force = constant * normal force). This is fine for most vidya-games.

More advanced car simulators (iRacing, Project Cars, Forza) have more realistic friction models. This is because real life car tire friction is highly nonlinear and pretty advanced (as friction is dependent on actual tire speed, temperature, centrifugal force which expands the tire and thus more patch surface, etc). There is a fair amount of academia for this type of stuff. The most cutting-edge stuff I would use for ROBLOX is the lumped LuGre friction model, as most static friction models are already pretty heavy stuff for just ROBLOX (because most people don’t like transient systems :c). Lumped models just assume point-contact which is usually fine unless you are trying to do actual control systems.

Friction force using the approximated lumped-LuGre friction model can be expressed as via the equation: chrome_sXISOe5hh9

σ_0 is stiffness constant, σ_1 is damping constant, and σ_2 is viscous damping constant. These values can be found experimentally if you own a car and have a few thousand-dollars worth of test equipment. Or you could find them in some random dissertation. v_r and F_n are self explanatory. z is a bit more nuanced, as it is essentially the ‘deflection’ of the tire molecules against the ground. Math and physics is pretty boring anyways, so just use the DEQ chrome_JrJPc2hg4B

Integrate it in code to solve for z and call it a day, as you will then have a pretty alright friction model for ROBLOX, as the contact patch method with the exact ODE is even worse than this. This also model has real-world steady-state characteristics, so it’s alright.

10 Likes

God!!! Your math fried my brain becauze I haven’t learnt anyhting about this frictions formulas in my school yet i dont think i should focus on cars till then

1 Like