Game Development Reference
In-Depth Information
method on the body property. Note that we also pass in the current angle of the body, since
SetTransform
requires that value to change its location.
This is all there is to customizing Asteroid to make it behave the way we want. In this example, we
don't remove any Actors from the scene, but that is something people very well might want to do.
The next section covers a little bit about cleaning up.
A Little Cleanup
Though we don't remove any Actors in this example, let's take a look at how we would do that.
Listing 13-9 shows how.
Listing 13-9. removeActor: (PhysicsViewController.m)
-(void)removeActor:(Actor *)actor{
if ([actor isKindOfClass:[PhysicsActor class]]){
PhysicsActor* physicsActor = (PhysicsActor*)actor;
b2Body* body = [physicsActor body];
world->DestroyBody(body);
}
[super removeActor:actor];
}
In Listing 13-9, we see the task
removeActor
:, which is called anytime we want to remove an Actor
from the scene. As with the task
addActor
: from Listing 13-3, we first check to see if the Actor is a
PhysicsActor
. If so, we get a reference to the body property and call
DestroyBody
to remove it from
our Box2D world object. In this way, we keep the Actors in our scene synchronized with the bodies
in the simulation.
Summary
In this chapter we explored some of the core features of Box2D, a game physics engine. We learned
how to create a world and simulate the motion of bodies in that world. We also looked at how to
extend our
GameController
class to enable physics within our existing game framework. We looked
at the class
PhysicsActor
and a new version of Asteroid to see what changes are required to Actors
that participate in the physics simulation.