[solved] triggering custom force callback function

Arcanor

07-04-2007 11:28:57

I've set my custom force callback function, and it seems to be working when gravity is applied, but then it never gets called again. I have no motion in my application yet except gravity. What I'm trying to do is apply a force to my player body. The way I'm guessing this is supposed to be done is that I'm setting a class variable indicating that the force is being applied, then in my callback function, applying that force. But my callback function isn't being called except for gravity (only upon the beginning of the application, when items are all dropping to the floor).

Since addForce() can only be called from INSIDE my callback function, how can I cause that callback function to be called when a user presses a key (i.e. to have the player start walking)? i.e. - what function should I be calling in my keyboard handler to trigger the force being applied, so that my callback will be called back?

Acid_Gambit

07-04-2007 13:00:27

I am not sure if this is helpful, but this is how I use my callback.

First I assign the callback to my ogrenewt body:


mPlayer->setCustomForceAndTorqueCallback((OgreNewt::Body::ForceCallback)forceCallback);


When I press a key I simply call mWorld->update(10); in order to step through the callback method.

Something like this...


if(input->isKeyDown(KC_W))
mWorld->update(10);



void PlayerCharacter::forceCallback(OgreNewt::Body* body)
{
...
body->addForce(dir);
}

Arcanor

07-04-2007 13:43:43

Thanks Acid_Gambit, but I don't see how that can work as I'm already doing the following, every single frame:

...
mWorld->update((Ogre::Real)(MicroSecElapsed * 0.000001f));
mWindow->update();


My understanding is that mWorld->update() needs to be processed constantly so that any resultant forces from object interactions can be calculated and rendered. Please correct me if I'm wrong...

One suggestion for you, if you don't mind. I'd recommend using a timer to determine update speed, rather than hard-coding your update() value. I'm not sure how your function is working at all since you're telling your world to update by 10 SECONDS each time you call your function.

To implement a timer like I've suggested, you would put something like this in your main application class's header file:
unsigned long mMicroSecElapsed;
Ogre::Timer* mTimer;

... and then this at the top of your main program loop:
// first let's figure out how long it's been since our last loop:
mMicroSecElapsed = mTimer->getMicroseconds();
// now we reset the timer before anything else happens, so we'll know how much time was used this frame:
mTimer->reset();
...

... then pass mMicroSecElapsed around to any of your other classes that need it during their respective updates.

Anyway, this still leaves me with my original problem. I still don't know how to trigger my callback function so my "walking" force can be applied to the player body. Any other thoughts anyone?

Arcanor

07-04-2007 14:18:29

Aha, I think I've found the problem. I don't have it quite working yet, but I believe I need to unFreeze() the body before the callback will be triggered.

Acid_Gambit

07-04-2007 15:28:13

Did you mean:


body->setAutoFreeze(0);


?

This way you don't have to unfreeze the body every time you want to apply forces.

Arcanor

07-04-2007 16:38:22

Yes, that's possibly a better way of doing it. Thanks. :)

By the way, I've got my callback function being called now. Problem solved.

Langhole

07-04-2007 16:44:53

Taken right out of my most favorite OgreNewt Demo... a cookie for anyone who can tell me what number it is :O haha..

// okay, we have the main chassis all setup. let's do a few things to it:
m_chassis->setStandardForceCallback();
// we don't want the vehicle to freeze, because we'll be unable to control it.
m_chassis->setAutoFreeze(0);


But seriously.. Custom Callback functions.. i've only written one myself, for friction settings on my old project with OgreNewt... I'm not sure how similar it is to this type of callback, but I'm thinking that if it IS, you really just need to make your own class with the actual function IN it.. and set the callback on init, and you won't have to call the callback at all, it'll do it automatically correct?

Arcanor

07-04-2007 16:51:46

Taken right out of my most favorite OgreNewt Demo... a cookie for anyone who can tell me what number it is :O haha..

// okay, we have the main chassis all setup. let's do a few things to it:
m_chassis->setStandardForceCallback();
// we don't want the vehicle to freeze, because we'll be unable to control it.
m_chassis->setAutoFreeze(0);


Langhole: I saw that line in the sample code, but there was very little explanation of what it does so I didn't "get" it. Now, of course, I realize its importance.

But seriously.. Custom Callback functions.. i've only written one myself, for friction settings on my old project with OgreNewt... I'm not sure how similar it is to this type of callback, but I'm thinking that if it IS, you really just need to make your own class with the actual function IN it.. and set the callback on init, and you won't have to call the callback at all, it'll do it automatically correct?


As for this, yes, that's the idea of a callback. You don't call it, it's event driven, so when the event happens, your callback function is called automatically.

Langhole

07-04-2007 16:55:30

sweet, yeh I'm still kinda new to most of c++, learning fast, but getting info like this only comes with a little back and forth on the forums :)

Well.. sorry If I bugged ya with my n00bishness.. I imagine I'll be using a customForceCallback soon as well...

good luck to yeh :)

Arcanor

07-04-2007 16:59:51

These forums are a great place for us all to learn. And there's the added benefit of being able to search through our questions and answers for anyone coming later.

PS - you didn't bug me. :) Thanks for contributing ideas to my thread.

kain

24-04-2007 16:22:46

I am trying to do the same on my object .. can you tell me how you did to trigger the pressed key with your forceCallback method ?

Arcanor

24-04-2007 16:30:43

I don't understand the question.