# Raycast Wheels: How to go by distributing force between wheel and movable surface?

I am making scripts to simulate a car, and perhaps the most difficult component to simulate has to be the tires, for which the default wheelColliders may not be enough.

And so I decided to try to simulate my wheels through the raycast method.
There’s quite a bit of logic behind its workings but here is the gist of it.

The wheel is simulated by a vertical raycast, and this raycast will apply an “AddForce” to the car’s body from the point where the ray and the ground intersects. The force’s direction is parallel with the ground.
Aside from issues with realism, calculating the force upon the car isn’t difficult since there is no other object to apply the force from the wheel to.

However, what if the wheel is on a movable physics-enabled surface/object?
The trouble now is that the “AddForce” has to be distributed between the surface/object and the car.

Though the solution sounds simple, there are the following cases that have to be accounted for:

1. The physics object’s movement is blocked by an obstacle.

2. The physics object lying on a surface with different friction values.

3. The physics object lying on another physics object (and another and another…)

4. A combination of the above cases.

The following are ideas I considered but dumped:

• Following Newton’s 3rd Law by implementing a simple 50/50 split of force between the object and the car definitely won’t work as it can’t properly account for case 3. Unity’s “AddForce” function purely adds force and doesn’t really consider Newton’s 3rd law. For instance, if the object is light, the car should receive the least force while the object should receive the most force. But AddForce will simply accelerate the car at the same rate, regardless of the object’s mass.

• I’ve also considered calculating acceleration instead of force upon the physics object and car by comparing their masses and distributing force or acceleration accordingly. The greater the mass of one object, the less the acceleration they receive. Simple F=m*a stuff. But this again is not a good solution as it fails to calculate a reasonable outcome within the three above scenarios.

So… does anyone have ideas that may possibly work?
This feels like something that I can easily simulate through existing formulas or by cheating some simpler solution, but I can’t seem to figure out what to do that accurately simulates the above three cases without being too overly complex.

Any ideas will be greatly appreciated!

That’s a good question. I’ve faced the same problem and the solution is applying the force to the car normally, and also apply the same force in the opposite direction to the object under the wheel (Newton’s 3rd Law). The logic is simple:

• If the object is blocked (your case 1) then there’s no difference with the ground itself, so behavior of the car is the same.
• If the object has enough friction with the ground to stay static, then again there’s no difference with the ground itself. Same if there are one, two or more objects between the wheel and the ground, as long as the frictions between them are strong enough to keep them static. Applying the tire force to the object under the wheel won’t move it.
• If the object has not enough friction then the force will make it slide under the wheel, causing a velocity that will affect the tire force calculation in the next step. The very first simulation step the car will behave as if it were over the ground. But the next step the object under the wheel will be moving at some velocity, so the tire force calculation will be different and the car will react naturally to the new situation.

This is the correct solution and it works perfectly as long as the rigidbody dynamics and friction are simulated in a physically realistic way by the physics engine (PhysX in this case). Unfortunately, since PhysX 3 (Unity 5) both the rigidbody dynamics and the friction are severely simplified to say the least. Implementing this solution as-is may throw away the objects under the wheel at ridiculous velocities even when they have more than enough friction to remain static.

Recent Unity versions have exposed new friction models that are supposed to (finally) simulate friction realistically. I haven’t tested them yet so I can’t tell. Currently my implementation uses a compromise solution: instead of applying the force to the object as-is, I reduce the force based on the mass ratio between the car and the object. For example: if the car’s mass is 1000 and the mass of the object under the wheel is 100, then a tire force of 1000 N would apply a counter-force of 100 N to the object. The force applied to the car is always the calculated tire force.

1 Like

That’s right that’s actually right!
It never occured to me that Newton’s 3rd Law of Motion can be simulated. I’ve been caught up in trying to split force between the two objects, forgetting that I’ve already been doing that: a force toward the car, and a force toward an object of infinite mass (the ground).

And, yeah looking at your explanations for the cases, it will work well with my implementation of the physics!

I simulate the wheel RPM and calculate the forces based on the linear velocity of the wheel’s surface and the relative velocity of the ground; the greater the difference, the more force applied. And considering all the three cases, it all works for the same reasons you mention!

Thanks a bunch for the explanation! I knew the explanation was somewhere really close!

Hopefully with the current friction model though, the behavior isn’t too erratic. I don’t wanna end up bug testing for something on Unity’s end

1 Like