OgreNewt callbacks question

bharling

20-04-2007 01:20:45

Can't figure out how this works (from the newton vehicle demo):

## This is the important callback, which is the meat of controlling the vehicle.
def userCallback(self):
## foop through wheels, adding torque and steering, and updating their positions.
## problem here as the original source casts the tire returned as a simpletire
## however getFirstTire returns and OgreNewt tire so we lose the 'steering'
for tire in self.tires:
if (tire.SteeringTire):
tire.setSteeringAngle( self.Steering )
else:
tire.setTorque( self.Torque )
## finally, this command updates the location of the visual mesh.
tire.updateNode()


I'm trying to implement a callback to make a character walk (or more specifically, apply torque to the sphere representing the feet) when a key is pressed. How and where could I assign a callback to do that?

I understand the vehicle example where torque and steering are stored and applied on the callback, just not how the engine knows which callback to use. Also, is it possible to assign multiple callbacks, ie: walkforward, turn etc. to different keys (or events)? or is it best to store these values and just apply them in one callback.

many thanks for any responses
:D

Game_Ender

20-04-2007 02:48:30

It appears the newton automatically calls that method of the class, so all you have to do is sublcass the OgreNewt.Vehicle class and fill you own code in there.

So in order to have the effect you want you could setup an input system which maps ois key events to your internal event system. Then in the handler for that internal event you can set a variable on you vehicle class to indicate the vehicle should be going sideways. Then when the callback is called by newton it just checks the state of these status variables.

In my project I have an abstract input system (it can take evetns from OIS or wxPython) that maps unique key events like "CTRL + ALT + A" to my internal event system. My internal event system just maps event types (simple strings) to handler functions. Currently you have to set the mappings up by hand, but the idea is that you can just query the system and then auto generate a gui like you see in most games for reconfiguring the keys.

bharling

20-04-2007 09:22:11

Hmm, but I'd rather not use the vehicle class at all, what I'm making is not really related to it. However, am I right in thinking that:

body.setCustomForceCallback( string: CallbackFunctionName )

will do the trick?

Does that mean that when the engine comes to apply force to the body, it will use that callback instead?

Game_Ender

20-04-2007 13:49:17

Yes, only inside that callback will applying forces to that body be valid. I get around this by having my own Body class which stores information about all the forces you apply to it, then when the callback is called it just applies these forces and resets them to zero. Generally you set your python object as the user data on the object, and then retrieve inside the callback.

Last I tried that callback can only be a "free" function, ie only a standalone function or static member function.

bharling

20-04-2007 14:43:07

Cool, after reading that 5 times I think I actually get it! I'll try that out tonight. Thanks very much for explaining it to me!

So, I have an actor class, which holds the Body object. The actor tracks torque and other motion applied to it. If I read you right, I would set the body's user data property to point to the actor class, and retrieve the torque during the callback via getUserData() or something similar. So the callback does not inherit any namespaces? (suppose that would kind of make sense).

As you may have guessed, this is for the techdemo project. I'm hoping to have some methods for automagically setting up physics objects for nodes as they are spawned into the game. I'm starting with a walking character template, which I think I've nailed, just need to link callbacks to the actor's internal events and we should be laughing.

Game_Ender

20-04-2007 18:45:09

Yes you got it. Here is a little example of what I was talking about:

class Actor(object):
def __init__(self, ...)
... Code ...
self._body.setUserData(self)

def move(self):
""" An event handler for the move key """
self._force += (0,10,0)

def turn(self):
""" An event handler for the turn key """
self._torque += (0, -5, 0)

... rest of the class ...

def applyForceAndTorques(self):
self._body.addForce(self._force)
self._body.addTorque(self._torque)

... Apply other forces here ...

... Lots of Code ...

def force_torque_callback(me):
actor = me.getUserData()
actor.applyForceAndTorques()

saladin

26-06-2007 23:10:39

I can't help but notice that newton doesn't seem to allow multiple force callbacks. Is this true? So gravity and the non-damping kind of air drag etc. all need to go into the custom force callback function call?