Moving dynamic object via tickCallback

mkultra333
Posts: 3
Joined: Mon Nov 15, 2010 2:48 pm

Moving dynamic object via tickCallback

Post by mkultra333 »

Hi, new to Bullet, it's my second day so I'm still working out how to use it. I'm adding it to my Ogre game project, I've got it compiling and running, and entities colliding with my map geometry and each other.

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);
}
I just wanted to ask if this is ok as a method. Also, if I change the physics to multi-threading will this kind of method still work?
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Moving dynamic object via tickCallback

Post by Erwin Coumans »

It should be ok, and multi-threading has no impact on the (pre) tick callbacks.

Thanks,
Erwin