How to move using forces?????

Buckcherry

23-06-2006 13:54:13

I am trying to move my character. At the moment, it is only affected by the gravity force.

I have test to move my character in different ways without success:

mBody->addForce(Vector3(0,0,10));mBody->addTorque(Vector3(0,0,10));

I have been taking a look into the OgreNewt_Body.h file and it says:

//! add force to the body.
/*!
this function is only valid inside a ForceCallback function!
*/
void addForce( const Ogre::Vector3& force ) { NewtonBodyAddForce( m_body, &force.x ); }


What does it mean with "only valid inside a ForceCallback" ??

How can I move my character???.

HexiDave

23-06-2006 19:03:57

That's for when you create your own Custom callback, but it's pretty much a must-do when you get into character physics, etc. Here's a sample of what I use - both of these functions are part of the same class, so it takes advantage of Boost functions added to OgreNewt:

From class PhysicsManager
void PhysicsManager::forceCallback( OgreNewt::Body* me )
{
//apply a simple gravity force.
Ogre::Real mass;
Ogre::Vector3 inertia;

me->getMassMatrix(mass, inertia);
Ogre::Vector3 force(0,-9.8,0);
force *= mass;

me->addForce( force );

}


void PhysicsManager::createPhysEntity(const String& objName, const String& entName, const Vector3& pos){

SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
static int objCount = 0;
Entity* ent = mSceneMgr->createEntity( entName + StringConverter::toString(objCount++), objName );

node->attachObject(ent);
node->setScale(10.0,10.0,10.0);

OgreNewt::Collision* col = new OgreNewt::CollisionPrimitives::Ellipsoid(mWorld,Vector3(10.0,10.0,10.0));

OgreNewt::Body* body = new OgreNewt::Body(mWorld, col);
body->attachToNode(node);
body->setPositionOrientation(pos,Quaternion::IDENTITY);
Ogre::Real mass = 500.0;

Ogre::Vector3 inertia = OgreNewt::MomentOfInertia::CalcSphereSolid(mass,10.0f);
body->setMassMatrix( mass, inertia );
body->setCustomForceAndTorqueCallback(boost::bind(&PhysicsManager::forceCallback,this,_1));

}


The last line there is the important part. Looks really really weird, but that's how it's done with non-static functions.

Hope that helps.