Ch12B-Box2DBodies - Capitol College Faculty Pages

barbarousmonthΚινητά – Ασύρματες Τεχνολογίες

10 Δεκ 2013 (πριν από 4 χρόνια και 7 μήνες)

144 εμφανίσεις


Written in C++, developed by Eric Catto,
released in September 2007

Popular, comes with Cocos2D

Need to rename .m files as .mm files so that
Xcode knows they can be mix of Objective
C and C++


Documentation at



Box2D is distributed with Cocos2D

Start a New Project in Xcode

Choose the Cocos2D Box2D template


Box2D folder is included in your project

Blocks.png is included too

.mm implementation files are already set up

Large skeleton code already there

Using Box2D

#import “Box2D.h”

#import “GLES

Render is for debugging; more later

b2World is a class that encapsulates the
physics world (of the game); it stores and
updates all the physics bodies

b2World *world; // inside .h file

Using Box2D

b2World constructor takes 2 parameters:

A b2Vec2 object (similar to a CGPoint), for

A bool variable

Using Box2D

b2Vec2 gravity = b2Vect2( 0.0f,
10.0f );

Vertical earth gravity (could use

This is an object, not a pointer


1.622f for the moon?

Using Box2D


parameter (bool)


allows bodies to sleep, i.e. when
forces applied to it are below a certain
threshold for a certain amount of time

time to stop its animation

saves cpu time


bodies do not sleep

Using Box2D

bool allowBodiesToSleep = true;

world = new b2World( gravity,
allowBodiesToSleep );

Note that world is a pointer (*, pointer
notation when we declared it earlier)
pointing to a dynamic variable (use of new

Using Box2D

new operator

dynamic variable

to destroy it later

(void) dealloc


delete world; // C++ syntax

[super dealloc]; // Objective C syntax


Creating bodies

Bodies can be dynamic or static depending
on their mass property

Static bodies do not move under the
influence of other bodies

Static body: density = 0

You may manually move a static body (be

Creating bodies

Bodies are created using the CreateBody
factory (i.e. static) method from the
b2World class

b2Body* CreateBody( b2BodyRef & );

Need a b2BodyRef (body definition)
object first; the body definition holds the
data needed to create and define the body

Creating bodies

A body copies the data from the body

It does not keep a pointer to it

can recycle the body definition to create
other bodies

Creating bodies

A body definition has many members: type,
position, angle, damping, gravity scale,
sleep parameters, fixed rotation, bullet
denomination, activation, user data

b2BodyDef bodyDef;

bodyDef.type = b2_dynamicBody;

Creating bodies

bodyDef.type = b2_dynamicBody;

bodyDef.allowSleep = true;

bodyDef.fixedRotation = true; // cannot rotate

bodyDef.bullet = true; /* flags body as fast

CCD (Continuous Collision
Detection) will be performed to avoid
tunneling issues */

Bodies and sprites

Box2D Version 0.99

User data is a sprite, the one the body is
associated with

CCSprite *sprite = …

bodyDef.userData = sprite;

Bodies and sprites

Box2D Version 2

Use PhysicsSprite class for sprite (similar to
CCSprite, but automatically follows its body)

Associate the body to the sprite not the other way

PhysicsSprite *sprite = [PhysicsSprite
spriteWithTexture: someSpriteTexture rect:

// define body ..

[sprite setPhysicsBody: body];

PhysicsSprite class

Can search and find it in you skeleton project (no
documentation found on web)

In Cocos2D since 1/4/2012

Inherits from CCSprite

One instance variable:

b2Body *body_;

One method:

(void) setPhysicsBody: (b2Body * ) body;

Creating bodies

Now that we have defined a body
definition, we can create a body with it

b2Body *body = world
> CreateBody(
&bodyRef );

Creating bodies

b2Body *body = world
> CreateBody(
&bodyRef );

world is a pointer

need to use
> to call

CreateBody returns a pointer to a b2Body

bodyRef is an object; we need to pass a

use &