setStandardForceCallback() inside a frameListener

nikhil

02-03-2007 07:42:04

I've a setStandardForceCallback() inside of a framelistener.. i was just wondering.

How are the forces applied/work?

Do I need to add a force every frame or I just add it once and it stays there forever untill I change it???

If we have to do a addForce just once then having the gravity added to the body every frame is a VERY WRONG thing to do, right?


Regards
Nik

walaber

02-03-2007 15:39:38

setStandardForceCallback() should only be called once during setup, to specify the callback function you want to call EVERY FRAME to apply forces to the body.

and in that callback function (where you actually use addForce etc to aply forces), you need to apply them EVERY FRAME, they are not remembered between frames.

nikhil

03-03-2007 10:08:17

setStandardForceCallback() should only be called once during setup, to specify the callback function you want to call EVERY FRAME to apply forces to the body.

I got it.. :)

and in that callback function (where you actually use addForce etc to aply forces), you need to apply them EVERY FRAME, they are not remembered between frames.

:shock: Confused again :shock:

Do you mean that something like
robotBody->setCustomForceAndTorqueCallback(&myCallbackFunction)
should be called just once. Whereas for the force to be applied I need to call newtonWorld->update(timestep) every frame??

Let's assume, on press of 'Y' I want to accelerate my physics body forward. How do I achieve that correctly without doing setVelocity?

CagedFury

15-03-2007 16:15:08

yes these callbacks can be hard to get your head around at first specially the custom ones :)

Something like this (just gonna copy and paste some of my code)

Called Once -> sets a function to be teh forcecallback

mCollision_Body->setCustomForceAndTorqueCallback(boost::bind(&ForceCallback_CollisionBody,this,_1));


Then this will be called every time the physics updates

void ForceCallback_CollisionBody(OgreNewt::Body * me)
{
//Reset to 0;
mForce = Vector3(0,0,0);

//Add x and z then * by rotation
mForce.x += adjForce->x;
mForce.y += adjForce->y;
mForce.z += adjForce->z;

//mForce = mDirection * mForce;

me->addForce(mForce);
}


Now you also need a function that does something like this


void RagCharacter::addForce(Vector3 adj)
{
adjForce = new Vector3(adj);
}


Oh and something like this too in your main App


bool processUnbufferedKeyInput(const Ogre::FrameEvent &evt)
{
addForce = Vector3(0,0,0);

if(mInputDevice->isKeyDown( KC_UP))
{
addForce.z -= amount;
}
if(mInputDevice->isKeyDown( KC_DOWN))
{
addForce.z += amount;
}
if(mInputDevice->isKeyDown( KC_LEFT))
{
addForce.x -= amountStrafe;
}
if(mInputDevice->isKeyDown( KC_RIGHT))
{
addForce.x += amountStrafe;
}

//RagChar->addRotation(rotation);
RagChar->addForce(addForce);

if( mInputDevice->isKeyDown( KC_ESCAPE) )
{
return false;
}else
{
return true;
}
}


So what you then have is your callback function which is assigned once, which runs every physics step which will if no keys are pressed does nothing (thats why i set the forces back to 0 each time but feel free to experiment) but when the user presses up down left or right it adds an adjusting force to the object.

Oh and although i left the traces of code in there mDirection and rotation are processed by my processUnbufferedMouse, basically they work the same as adjForce and mForce except that it is a Quaternion based on getRealOrientation() function for the mouse event, thats gets my movement acting in the direction the mouse is facing and may or may not be the way you want things to work.

Hopefully that is a good enough start at any rate :)

nikhil

17-03-2007 08:17:59

Thanks dude.. I actually ended making a whole demo game without completely understanding the working of these callbacks.. and when the time came to refine it I was clueless.. earlier it was all hit and try..

there has to be some kind of wiki page which helps a fresher breeze through this important and fairly comprehensible concepts..

only if I was nt lazy, i would've made one.. :D ..