Inherits from NSObject
Declared in CCPhysicsBody.h

Overview

Physics bodies are attached to CCNode objects and contain their physical properties such as mass, shape, friction, etc.

There are two main kinds of bodies, static and dynamic. Static bodies cannot be moved by normal forces, collisions or gravity. This is the type of body you would use for the ground or wall in a game. Dynamic bodies are just the opposite. They are affected by collisions, friction, gravity, forces, etc. You can change the type of a body using the CCPhysicsBody.type property.

There are two basic categories of shapes that physics bodies can have. The simplest category are convex shapes such as circles, pills and polygons (in order of efficiency). These are considered to be solid (not hollow) objects by the physics engine. Composite bodies, such as those created with the polyline or bodyWithShapes: methods are actually composed of multiple shapes. Regardless of the type, all shapes can be given a radius value that adds some thickness to them and rounds out their sharp corners.

Many rigid body properties such as the moment of inertia or center of gravity are calculated for you automatically based on the shapes the body is created from. The exception is the mass of the body which defaults to 1.0. It is the developer’s responsibility to set the mass (or density) for the objects. What units you use for mass is not important as long as they are consistent.

Collision Filtering:

By default physics bodies collide with all other physics bodies. Since 2D games use a lot of faked perspectives and layering CCPhysics provides you with many options to filter out unwanted collisions.

  • collisionGroup: Bodies can be assigned an object pointer that defines a group. Two bodies in the same group will not collide.
  • collisionCategory, collisionMask: Bodies can be assigned a list of categories and masks (rules) that define which kinds of bodies they will collide with.
  • CCPhysicsJoint.collideBodies: Joints have a flag that allows you to reject a collisions between the bodies they connect.
  • CCPhysicsCollisionDelegate methods: You can set up a delegate that responds to collision events between bodies progmatically.

Tips:

  • Use the simplest shape you can. You don’t need to be pixel perfect. Collisions with complex shapes are very computationally expensive and will ultimately feel more random and unfair to your players.
  • Add radii to your shapes. It will help smooth out collisions between corners of objects and it will allow the collision detection to run more efficiently.
  • Avoid very small, thin, or fast moving shapes when possible. The underlying physics engine does not perform continuous collision detection and can miss collisions if they move too much in a single step.
  • When possible, try to use groups, categories or joints to filter collisions since they are tried before running the expensive collision detection code.

Properties

affectedByGravity

Affected by gravity flag. Defaults to Yes.

@property (nonatomic, assign) BOOL affectedByGravity

Declared In

CCPhysicsBody.h

allowsRotation

Allow body rotation flag. Defaults to Yes.

@property (nonatomic, assign) BOOL allowsRotation

Declared In

CCPhysicsBody.h

angularVelocity

Angular velocity of the physics body in radians per second.

@property (nonatomic, assign) CGFloat angularVelocity

Declared In

CCPhysicsBody.h

area

Area of the body in points2. Please note that this is relative to the CCPhysicsNode, changing the node or a parent can change the area.

@property (nonatomic, readonly) CGFloat area

Declared In

CCPhysicsBody.h

collisionCategories

An array of NSString category names of which this physics body is a member. Up to 32 unique categories can be used in a single physics node. A value of nil means that a body exists in all possible collision categories. The deefault is nil.

@property (nonatomic, copy) NSArray *collisionCategories

Declared In

CCPhysicsBody.h

collisionGroup

The body’s collisionGroup, if two physics bodies share the same group id, they don’t collide. Defaults to nil.

@property (nonatomic, assign) id collisionGroup

Declared In

CCPhysicsBody.h

collisionMask

An array of NSString category names that this physics body wants to collide with. The categories/masks of both bodies must agree for a collision to occur. A value of nil means that this body will collide with a body in any category. The default is nil.

@property (nonatomic, copy) NSArray *collisionMask

Declared In

CCPhysicsBody.h

collisionType

A string that identifies the collision pair delegate method that should be called. Default value is @“default”.

@property (nonatomic, copy) NSString *collisionType

Declared In

CCPhysicsBody.h

density

Density of the body in 1/1000 units of mass per point2. The co-efficent is used to keep the mass of an object a reasonably small value. If the body has multiple shapes, you cannot change the density directly. Note that mass and not density will remain constant if an object is rescaled.

@property (nonatomic, assign) CGFloat density

Declared In

CCPhysicsBody.h

elasticity

Elasticity of the physics body. When two objects collide, their elasticity is multiplied together. The calculated value can be ovrriden in a CCCollisionPairDelegate pre-solve method. Defaults to 0.2.

@property (nonatomic, assign) CGFloat elasticity

Declared In

CCPhysicsBody.h

force

Linear force applied to the physics body this fixed timestep.

@property (nonatomic, assign) CGPoint force

Declared In

CCPhysicsBody.h

friction

Surface friction of the physics body, when two objects collide, their friction is multiplied together. The calculated value can be overridden in a CCCollisionPairDelegate pre-solve method. Defaults to 0.7

@property (nonatomic, assign) CGFloat friction

Declared In

CCPhysicsBody.h

joints

Joints connected to this body.

@property (nonatomic, readonly) NSArray *joints

Declared In

CCPhysicsBody.h

mass

Mass of the physics body. The mass of a composite body cannod be changed. Defaults to 1.0.

@property (nonatomic, assign) CGFloat mass

Declared In

CCPhysicsBody.h

node

The CCNode to which this physics body is attached.

@property (nonatomic, readonly, weak) CCNode *node

Declared In

CCPhysicsBody.h

sensor

Is this body a sensor? A sensor will call a collision delegate but does not physically cause collisions between bodies. Defaults to NO.

@property (nonatomic, assign) BOOL sensor

Declared In

CCPhysicsBody.h

sleeping

Sleeping bodies use minimal CPU resources as they are removed from the simulation until something collides with them. Normally a body will fall alsleep on it’s own, but you can manually force a body to fall a sleep at any time if you desire.

@property (nonatomic, assign) BOOL sleeping

Declared In

CCPhysicsBody.h

surfaceVelocity

Velocity of the surface of a physics body relative to it’s normal velocity. This is useful for modelling conveyor belts or the feet of a player avatar. The calculated surface velocity of two colliding shapes by default only affects their friction. The calculated value can be overriden in a CCCollisionPairDelegate pre-solve method. Defaults to CGPointZero.

@property (nonatomic, assign) CGPoint surfaceVelocity

Declared In

CCPhysicsBody.h

torque

Torque applied to this physics body this fixed timestep.

@property (nonatomic, assign) CGFloat torque

Declared In

CCPhysicsBody.h

type

Physics body type. Defaults to CCPhysicsBodyTypeDynamic

@property (nonatomic, assign) CCPhysicsBodyType type

Declared In

CCPhysicsBody.h

velocity

The velocity of the physics body in absolute coordinates.

@property (nonatomic, assign) CGPoint velocity

Declared In

CCPhysicsBody.h

Class Methods

bodyWithCircleOfRadius:andCenter:

Creates and retuns a circular physics body using the circle radius and center values specified.

+ (CCPhysicsBody *)bodyWithCircleOfRadius:(CGFloat)radius andCenter:(CGPoint)center

Parameters

radius

Circle radius.

center

Circle center point.

Return Value

The CCPhysicsBody Object.

Declared In

CCPhysicsBody.h

bodyWithPillFrom:to:cornerRadius:

Creates and returns a pill shaped physics body with rounded corners that stretches from ‘start’ to ‘end’.

+ (CCPhysicsBody *)bodyWithPillFrom:(CGPoint)from to:(CGPoint)to cornerRadius:(CGFloat)cornerRadius

Parameters

from

Start point.

to

End point.

cornerRadius

Corner radius.

Return Value

The CCPhysicsBody Object.

Declared In

CCPhysicsBody.h

bodyWithPolygonFromPoints:count:cornerRadius:

Creates and returns a convex polygon shaped physics body with rounded corners. If the points do not form a convex polygon then a convex hull will be created from them automatically.

+ (CCPhysicsBody *)bodyWithPolygonFromPoints:(CGPoint *)points count:(NSUInteger)count cornerRadius:(CGFloat)cornerRadius

Parameters

points

Points array pointer.

count

Points count.

cornerRadius

Corner radius.

Return Value

The CCPhysicsBody Object.

Declared In

CCPhysicsBody.h

bodyWithPolylineFromPoints:count:cornerRadius:looped:

Creates and returns a physics body with multiple pill shapes attached, one for each segment in the polyline. Polyline based bodies default to the CCPhysicsBodyTypeStatic body type.

+ (CCPhysicsBody *)bodyWithPolylineFromPoints:(CGPoint *)points count:(NSUInteger)count cornerRadius:(CGFloat)cornerRadius looped:(bool)looped

Parameters

points

Points array pointer.

count

Points count.

cornerRadius

Corner radius.

looped

Should there be a pill shape that goes from the first to last point or not.

Return Value

The CCPhysicsBody Object.

Declared In

CCPhysicsBody.h

bodyWithPolylineFromRect:cornerRadius:

Creates and returns a physics body with four pill shapes around the rectangle’s perimeter. Polyline based bodies default to the CCPhysicsBodyTypeStatic body type.

+ (CCPhysicsBody *)bodyWithPolylineFromRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius

Parameters

rect

Rectangle perimeter.

cornerRadius

Corner radius.

Return Value

The CCPhysicsBody Object.

Declared In

CCPhysicsBody.h

bodyWithRect:cornerRadius:

Creates and returns a box shaped physics body with rounded corners.

+ (CCPhysicsBody *)bodyWithRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius

Parameters

rect

Box dimensions.

cornerRadius

Corner radius.

Return Value

The CCPhysicsBody Object.

Declared In

CCPhysicsBody.h

bodyWithShapes:

Creates and returns a physics body with multiple shapes.

+ (CCPhysicsBody *)bodyWithShapes:(NSArray *)shapes

Parameters

shapes

Array of shapes to attach to the body.

Return Value

The CCPhysicsBody Object.

Declared In

CCPhysicsBody.h

Instance Methods

applyAngularImpulse:

Apply an angular impulse.

- (void)applyAngularImpulse:(CGFloat)impulse

Parameters

impulse

Angular impulse.

Declared In

CCPhysicsBody.h

applyForce:

Apply a force to the physics body.

- (void)applyForce:(CGPoint)force

Parameters

force

Force vector.

Declared In

CCPhysicsBody.h

applyForce:atLocalPoint:

Apply force and torque on the physics body from a force applied at the given point in the parent CCNode’s coordinates. The force will be rotated by, but not scaled by the CCNode’s transform.

- (void)applyForce:(CGPoint)force atLocalPoint:(CGPoint)point

Parameters

force

Force vector.

point

Point to apply force.

Declared In

CCPhysicsBody.h

applyForce:atWorldPoint:

Apply an force and angular torque on the physics body at the given point in absolute coordinates.

- (void)applyForce:(CGPoint)force atWorldPoint:(CGPoint)point

Parameters

force

Force vector.

point

Point to apply force.

Declared In

CCPhysicsBody.h

applyImpulse:

Apply an impulse on the physics body.

- (void)applyImpulse:(CGPoint)impulse

Parameters

impulse

Impulse vector.

Declared In

CCPhysicsBody.h

applyImpulse:atLocalPoint:

Apply an impulse and angular impulse on the physics body at the given point in the parent CCNode’s coordinates. The impulse will be rotated by, but not scaled by the CCNode’s transform.

- (void)applyImpulse:(CGPoint)impulse atLocalPoint:(CGPoint)point

Parameters

impulse

Impulse vector.

point

Point to apply impulse.

Declared In

CCPhysicsBody.h

applyImpulse:atWorldPoint:

Apply an impulse and angular impulse on the physics body at the given point in absolute coordinates.

- (void)applyImpulse:(CGPoint)impulse atWorldPoint:(CGPoint)point

Parameters

impulse

Impulse vector.

point

Point to apply impulse.

Declared In

CCPhysicsBody.h

applyTorque:

Apply a torque on the physics body.

- (void)applyTorque:(CGFloat)torque

Parameters

torque

Torque.

Declared In

CCPhysicsBody.h

eachCollisionPair:

Iterate over all of the CCPhysicsCollisionPairs this body is currently in contact with.

- (void)eachCollisionPair:(void ( ^ ) ( CCPhysicsCollisionPair *pair ))block

Parameters

block

Collision block.

Discussion

Note: The CCPhysicsCollisionPair object is shared so you should not store a reference to it.

Declared In

CCPhysicsBody.h