how to add force

nikhil

15-10-2006 11:26:19

.. hi guyz..

i finally got the ball hitting the ground.. and just to mess up with stuff.. i called setVelocity on the ball to change its velocity.. but the thing is, once it stops.. it wont move again.. I think i've to apply force.. right??

and to add force.. i need a custom call back.. right??

if so, where do i define my Callback.. do i've to do it in OgreNewt_Body.cpp

and can i define a non-static Callback?
"standardForceCallback( OgreNewt::Body* me )" is static in OgreNewt_body.h

Lastly does the "me" in "standardForceCallback( OgreNewt::Body* me )" refer to the calling object??

regards

nikhil

alberts

15-10-2006 15:19:54

You don't need to modify OgreNewt.

You only have to define a function and set it as the function callback of a body.

Example: In my Airplane class I have defined a function like this:


void Airplane::AerodynamicForceCallback( OgreNewt::Body* me )
{
Ogre::Vector3 inertia;
Ogre::Real mass;
Ogre::Vector3 position;
Ogre::Quaternion orient;
me->getMassMatrix( mass, inertia );
me->getPositionOrientation(position,orient);

Ogre::Vector3 netForce(0, 0, 0);
Ogre::Vector3 netTorque(0, 0, 0);


Ogre::Vector3 veloc = me->getVelocity();
Ogre::Vector3 relWindVeloc (Meteo::getSingleton().getWindAt(position) - veloc);

AerodynamicProperties* ap = (AerodynamicProperties*)me->getUserData();
netForce = ap->CalculateAirfoilForce(relWindVeloc);

//add gravity
netForce += Ogre::Vector3(0,-9.8,0) * mass;
if(netForce.length() > 15000)
{
netForce = netForce.normalisedCopy() * 15000;
}
me->addForce(netForce);
}


Then I can assign this callback function to any newton body:


OgreNewt::Body* mLeftWing->setCustomForceAndTorqueCallback<Airplane>( &Airplane::AerodynamicForceCallback, this );
OgreNewt::Body* mRightWing->setCustomForceAndTorqueCallback<Airplane>( &Airplane::AerodynamicForceCallback, this );


I hope this help you :wink:

nikhil

16-10-2006 03:58:52

hi alberts..

umm..

I made this test code:-



void ballJumpCallback (OgreNewt::Body* me)
{
Ogre::Real mass;
Vector3 jumpForce(0, 15,-5), inertia;
me->getMassMatrix(mass,inertia);
jumpForce += Vector3(0,-9.8,0);
jumpForce *= mass;
me->addForce(jumpForce);
}


And then:-


if (mInputDevice->isKeyDown(KC_SPACE))
{
_ballBody->setCustomForceAndTorqueCallback(&ballJumpCallback);
}


the problem here is.. that my ball falls from a height at the beginning and before it comes to rest, if i press space, it flys of according to "jumpForce" .. but if it comes to a rest, then, on pressing space nothing happens..

It stays still and wouldn't budge.. Whts happening here..

regards

nikhil

alberts

16-10-2006 12:17:25

You only need to set the callback for a body once. The callback function is automatically fired every frame, so you need to check if the key is pressed inside the callback function or set another callback function for the body when the user press a key.

nikhil

17-10-2006 11:36:54

Guyz

Plz tell me wht i am doing wrong.. Once the ball i've falling on the floor stops its bounce i cann't get it move again..

I'm applying a force to it.. But that also only works when its in some motion, i.e has a velocity in any direction..?????

i've this inside the frameStarted of my physics listener:-


//isKeyPressed = false initially
if (mInputDevice->isKeyDown(KC_SPACE))
{
if (!_isKeyPressed)
{
_ballBody->setCustomForceAndTorqueCallback(&ballJumpCallback);
_isKeyPressed = true;
}
else
{
_ballBody->setStandardForceCallback();
}
}
else
{
_isKeyPressed = false;
}



Heres the body for ballJumpCallback (its in the global scope)

void ballJumpCallback (OgreNewt::Body* me)
{
Ogre::Real mass;
Vector3 jumpForce(0, 15,-5), inertia;
me->getMassMatrix(mass,inertia);
jumpForce += Vector3(0,-9.8,0);
jumpForce *= mass;
me->addForce(jumpForce);
}


why is the force not applied once the ball is stationary??????????/

regards

nik

nikhil

17-10-2006 11:39:45

You only need to set the callback for a body once. The callback function is automatically fired every frame, so you need to check if the key is pressed inside the callback function or set another callback function for the body when the user press a key.

i do set the callback once and set another callback on press of space and that too only for the first frame.. I use a isKeyPress flag to check that the callback is assigned only once.. Plz luk at the code above and correct me.. I'm sure the code is wrong somewhere

thanks
nik

alberts

17-10-2006 16:27:14

I'm at work now and I cannot see my code but I remember that there is something like setAutofreeze that should be put to false. I don't remember if it is a world or a body function.

nikhil

17-10-2006 21:44:26

I'm at work now and I cannot see my code but I remember that there is something like setAutofreeze that should be put to false. I don't remember if it is a world or a body function.

Hey thnks man..

it worked.. appreciate it

regards

nikhil