Game Development Reference
In-Depth Information
Once we have our circle, we attach it to our body by creating a b2FixtureDef called fixtureDef .
Fixtures follow the same pattern as bodies in Box2D; they are created with a struct that defines their
starting values and then actually creates them by calling an appropriate factory method—in this
case, CreateFixture on body.
On fixtureDef , we also specify the density, friction, and restitution. In our example, these values
were set by trial and error to see what looked good. Notice, for example, that the density is based on
the radius of the asteroid. This is obviously not very realistic; one would expect a bunch of asteroids
to have, if not identical, similar densities if they are made out of the same stuff. But when playing
with the simulation, I liked how changing the densities gave the little asteroids more bounce. Feel
free to play with these values and see how it changes the simulation.
Once we have created a body and a fixture for it, we store that object in the body property of
. In this way, the physicsActor has reference to its representation in the Box2D world
addActor : is call the super-implementation so that the actor is attached to the
scene, as in previous chapters. The following section will explore our new type of
PhysicsActor —and see how we link its Box2D body with our scene.
PhysicsActor is intended to be a super-class for any Actor that wishes to participate in
we look at the class Asteroid, we will see that we do break this encapsulation, but I thought the
code was easier to understand as an example. Anyway, let's start by looking at the header of
PhysicsActor , shown in Listing 13-4.
Listing 13-4. PhysicsActor.h
#import <Foundation/Foundation.h>
#import <Box2D/Box2D.h>
#import "Actor.h"
#import "PhysicsViewController.h"
@interface PhysicsActor : Actor
@property (nonatomic) b2Body* body;
-(b2BodyDef)createBodyDef;
@end
In Listing 13-4, we see that PhysicsActor extends Actor. It also adds a new property called body that
is used to store b2Body that represents it in the Box2D world. You can also see the declaration of
the task createBodyDef that was used in Listing 13-3 when adding the Actor to the scene. Let's take
a quick look at the implementation of createBodyDef , shown in Listing 13-5.
 
Search WWH ::




Custom Search