Making a car follow a track

I have a track and a car, and I would like this car to follow the tracks contours.
I have already gotten basic gravity to work, by writting this:


import direct.directbase.DirectStart 

from pandac.PandaModules import * # import the panda tools
from myEventHandler1 import myEventHandler

car = loader.loadModel("ambu/ambulance")
car.setPos(0,0,0)
car.setHpr(90,0,0)
car.reparentTo(render)
car.setScale(0.9)

track = loader.loadModel("models2/DIKUtrack5")
track.reparentTo(render)
track.setScale(0.05)

base.disableMouse()
camera = base.camera
camera.reparentTo(car)
camera.setPos(0,55,30)
camera.setHpr(180,-25,0)

road = track.find("**/baseTrack")
road.node().setIntoCollideMask(BitMask32.bit(1))
road.setCollideMask(BitMask32.bit(0))

body = car.attachNewNode(CollisionNode('colNode'))
body.node().addSolid(CollisionRay(0, 0, 0, 0, 0, -1))
body.node().setIntoCollideMask( BitMask32.allOff() )
body.node().setFromCollideMask( BitMask32.bit(0))

cTrav = CollisionTraverser()
cHandler = CollisionHandlerGravity()
cHandler.setGravity(50)
cHandler.setReach(10)
cHandler.setOffset(1)
cHandler.addCollider(body,car)
cTrav.addCollider(body, cHandler)

myEventHandler(car)

base.cTrav = cTrav

run()

The problem is that the car is stiff like a board in the air, and does not realy
behave like a car.

I know this is a very open question, but it would be wonderfull if you could give
me some pointers as to what is needed for this car to move nicely.

Thank you very much

In fact the answer is quite simple, even for an open question. What you need for a natural “feeling” is (more) physics. Not only gravity, but also lifting forces, inertia, lag, drag and things like that.

I cannot really offer any code, but I bet that e.g. the Airblade code (under Downloads/Software) offers some basic things like that. Airblade “feels” real for what I remember, so it will probably have some physics - or at least randomised bounces and wobbles.

Thank you very much Marc … I have already been dabling around with physics.

To begin with, I just want the 4 wheels to touch the ground, even if the track is uneven. The car model I am working with, the four wheels of the car have their own groups in the egg file, meaning that I can retrieve their node. I have tried, while the car iself had gravity to put a downward force on those wheels. But nothing happens.

Unfortunatly where I am sitting, I do not have any code handy to show. :frowning:

I don’t know, but judging from your code posted up there I cannot imagine that there’s real physics in there already. I admit that I haven’t worked with any physics engine yet (including Panda’s), but I know some physics, and I cannot imagine that just setting gravity is enough.

For full realism, you need to solve a 6DOF differential equation in real time - not a good choice. I’d guess that 4DOF will be enough, though - rotational “wobbling” (pitch and roll), rotational steering (yaw) and one translation (the actual driving :-)). The four wheels receive a translatory excitation and affect the car’s body indirectly through 4 springs. This leads to the wobbling (pitch and roll), which needs to be damped not to go on infinitely. Yaw should be determined through the steering wheel, even though you’ll also need some spring-damper-model here for the steering not to feel too “direct”. The same goes for acceleration.

That would be my approach for some realistic “feeling” of the car. Does that get you farther or did you already try to do it that way somehow?

No problem, since I find it easier to think in terms of physics anyway. I’m an engineer, and not really a programmer, so concepts suit me better than code. :wink:

the bad news is that panda has a limited physics engine. Most of it was designed with particles in mind. You can integrate force vectors, sources, sinks, and jitters, but there won’t be any accounting for the handling of your vehicle.

On a good note, there are some very interesting resources out there for doing car physics

home.planet.nl/~monstrous/tutcar.html

This tutorial is far more in depth than you would ever want (he even goes into the torque on the transmission), but it lays out ways to attack many of the problems you might be interested. He’s even got a source code demo in c++.

Hey zpavlov,

Thanks for the link! That’s excessive indeed, but for serious driving sims you have to get into that kind of detail, at least most automotive engineers would. IMO the games people need to find the equilibrium for more of a semi-realism - fun and credibility are often more important than “real realism”.

But that reminds me of something else - there was a thread about the (Python-wrapped) Open Source Dynamics Engine (http://www.ode.org/) and Panda3D:

https://discourse.panda3d.org/viewtopic.php?t=437

Again, I don’t know what physics engines can really do, so I guess I’d rather program the physics myself. Call it re-inventing te wheel, if you will, but at least like this I’d make sure of a good Panda3D/Python integration. :slight_smile:

EDIT: More optimism about about PyODE can be found here:
https://discourse.panda3d.org/viewtopic.php?t=188

Everybody … thank you very much for the help, and the links. :slight_smile: I really appreciate it.

But if understand this correctly, Panda is not really suited for making car racing games?

I do think it is suitable for any kind of game - just that you’ll have to do some more physics by hand. But since it is one of the most difficult things to do well anyway, this little extra work won’t matter too much, I guess.

Otherwise go for (Py)ODE, that’s about the best physics engine open source has to offer (for all I know).