Skip to main content
Controlling car on twisted track         Controlling car on crazy, twisted tracks using physics engine

Problem

Controlling car on crazy, twisted tracks using a physics engine.

Solution

1. Stick the car to the track:

Check your physics engine for something that will give you normal vector of surface car contacts (i.e. contact callback). Save this normal to a variable, and use it's invertion as the gravity direction. Remember to define what happens if the car is not contacting with the track.




2. Controlling the car

In case of simple, flat tracks, you can just set the torque .Y value in the force callback. But twisted tracks require a little more work. Look at the How to drive a car, and create right and left nodes, but move them forward along with the car to it's front. Create also one more child node exactly on front of the car. You should have something like this




CarControllNodes.jpg

Now, when applying torque and forces, when turning left, obtain Ogre::Quaternion

Copy to clipboard
Ogre::Quaternion torqueBase = centerNode.getWorldPosition().getRotationTo(leftNode.getWorldPosition())

And when turning right:

Copy to clipboard
Ogre::Quaternion torqueBase = centerNode.getWorldPosition().getRotationTo(rightNode.getWorldPosition())

Pay attention on position you are getting from nodes. It should be world position, not the relative one. Now just apply torque using function coming with a physics engine, i.e:

Copy to clipboard
Car->ApplyTorque(torqueBase.getPitch().valueRadians() * f, torqueBase.getYaw().valueRadians() * f, torqueBase.getRoll().valueRadians() * f);

Where "f" is a factor (like mass * torqueSpeed).


Alias: Controlling_car_on_twisted_track