Game Development Reference
In-Depth Information
'
systems generally don
t deal well with deep interpenetrations of objects, which hap-
pens a lot when objects move a large distance in between simulation steps.
The Incredible Bouncing Camera
Physics systems are horribly sensitive to frame rate. When I was working on
Thief: Deadly Shadows, I had to program a simple spring attached to the
camera system, which created a smooth movement of the camera under lots
of game situations, for example, when the main character jumped off a wall.
On my first attempt, I noticed that the camera could easily bounce out of
control, as if the spring were getting more and more energy until the camera
system crashed. After a little debugging, I noticed the system crashed more
easily in areas with a low frame rate. The problem was that my spring
system wasn
t being ticked at a high enough frame rate, say 60Hz, and the
spring calculation would accumulate energy. The solution was pretty easy. I
just called the spring calculation in a tight loop, with a delay of no more than
1/60th of a second, and everything was fine.
'
The trade-off is that ticking your physics simulation multiple times in one game loop
is expensive, so try your best to keep enough CPU budget around for everything: ren-
dering, AI, sound decompression, resource streaming, and physics.
Another important note is that Bullet automatically calls an
once every
internal time step. This callback is specified by the user. For our purposes, let
internal callback
'
ssetitas
BulletInternalTickCallback . This function handles dispatching collision events.
After the physics system has updated itself, you can grab the results and send them to
your game
s data structures. Any decent physics system lets you set a user data mem-
ber of its internal physics objects. Doing this step is critical to getting the new posi-
tion and orientation data to your game. First, take a look at a small helper class called
ActorMotionState :
'
struct ActorMotionState : public btMotionState
{
Mat4x4 m_worldToPositionTransform;
ActorMotionState( Mat4x4 const & startingTransform )
: m_worldToPositionTransform( startingTransform ) { }
// btMotionState interface: Bullet calls these
virtual void getWorldTransform( btTransform& worldTrans ) const
{ worldTrans = Mat4x4_to_btTransform( m_worldToPositionTransform ); }
virtual void setWorldTransform( const btTransform& worldTrans )
{ m_worldToPositionTransform = btTransform_to_Mat4x4( worldTrans ); }
};
 
Search WWH ::




Custom Search