I'll have physics controlling the monster entities. I tried to move entities around using impulses but this was erratic, sent things spinning and often only roughly the right direction.
So I'm modifying the entity velocities directly, pre-tick, via the tickCallback. Here is the code I've written. This is a simple test, it's designed so that when I press the "K" key the entities get a boost to fly up in the air.
I set up the callback like this:
Code: Select all
void PhysicsTickCallback(const btDynamicsWorld *world, btScalar timeStep) ;
static void _tickCallback(btDynamicsWorld* world, btScalar timeStep) ;
And the code is like this:
Code: Select all
// this is done in earlier setup code
m_dynamicsWorld->setInternalTickCallback((btInternalTickCallback)_tickCallback, 0, true) ;
// callbacks
void OgreFramework::PhysicsTickCallback(const btDynamicsWorld *world, btScalar timeStep)
{
btVector3 Velocity ;
btScalar Speed ;
btScalar MaxSpeed=16.0f ;
for(int nEnt=m_nFirstTestEntity ; nEnt<m_nLastTestEntity ; nEnt++)
{
// make sure the entity isn't sleeping
m_pEntityInfo[nEnt].RigidBody->activate() ;
// get its current velocity
Velocity=m_pEntityInfo[nEnt].RigidBody->getLinearVelocity() ;
// add our upward velocity nudge if "K" is pressed
if(m_nKeyToggle[OIS::KC_K]==1)
Velocity.setY(Velocity.y()+1.0f) ;
// cap velocities to prevent tunnelling
Speed = Velocity.length();
if(Speed>MaxSpeed)
Velocity *= MaxSpeed/Speed ;
// set the entity's updated velocity
m_pEntityInfo[nEnt].RigidBody->setLinearVelocity(Velocity) ;
}
}
void OgreFramework::_tickCallback(btDynamicsWorld* world, btScalar timeStep)
{
getSingletonPtr()->PhysicsTickCallback(world, timeStep);
}