CCPhysicsBody Class Reference
| 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.
Tasks
Creating a CCPhysicsBody Object
-
+ bodyWithCircleOfRadius:andCenter: -
+ bodyWithRect:cornerRadius: -
+ bodyWithPillFrom:to:cornerRadius: -
+ bodyWithPolygonFromPoints:count:cornerRadius: -
+ bodyWithPolylineFromRect:cornerRadius: -
+ bodyWithPolylineFromPoints:count:cornerRadius:looped: -
+ bodyWithShapes:
Accessing Basic Body Attributes
-
massproperty -
areaproperty -
densityproperty -
frictionproperty -
elasticityproperty -
surfaceVelocityproperty
Accessing Simulation Attributes
-
affectedByGravityproperty -
allowsRotationproperty -
typeproperty
Accessing Collision and Contact Attributes
-
sensorproperty -
collisionGroupproperty -
collisionTypeproperty -
collisionCategoriesproperty -
collisionMaskproperty -
– eachCollisionPair:
Accessing Velocity Attributes
-
velocityproperty -
angularVelocityproperty
Accessing Forces and Torques Attributes
Applying Force and Impulses Methods
-
– applyTorque: -
– applyAngularImpulse: -
– applyForce: -
– applyImpulse: -
– applyForce:atLocalPoint: -
– applyImpulse:atLocalPoint: -
– applyForce:atWorldPoint: -
– applyImpulse:atWorldPoint:
Accessing Misc Attributes
Properties
affectedByGravity
Affected by gravity flag. Defaults to Yes.
@property (nonatomic, assign) BOOL affectedByGravityDeclared In
CCPhysicsBody.hallowsRotation
Allow body rotation flag. Defaults to Yes.
@property (nonatomic, assign) BOOL allowsRotationDeclared In
CCPhysicsBody.hangularVelocity
Angular velocity of the physics body in radians per second.
@property (nonatomic, assign) CGFloat angularVelocityDeclared In
CCPhysicsBody.harea
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 areaDeclared In
CCPhysicsBody.hcollisionCategories
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 *collisionCategoriesDeclared In
CCPhysicsBody.hcollisionGroup
The body’s collisionGroup, if two physics bodies share the same group id, they don’t collide. Defaults to nil.
@property (nonatomic, assign) id collisionGroupDeclared In
CCPhysicsBody.hcollisionMask
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 *collisionMaskDeclared In
CCPhysicsBody.hcollisionType
A string that identifies the collision pair delegate method that should be called. Default value is @“default”.
@property (nonatomic, copy) NSString *collisionTypeDeclared In
CCPhysicsBody.hdensity
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 densityDeclared In
CCPhysicsBody.helasticity
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 elasticityDeclared In
CCPhysicsBody.hforce
Linear force applied to the physics body this fixed timestep.
@property (nonatomic, assign) CGPoint forceDeclared In
CCPhysicsBody.hfriction
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 frictionDeclared In
CCPhysicsBody.hjoints
Joints connected to this body.
@property (nonatomic, readonly) NSArray *jointsDeclared In
CCPhysicsBody.hmass
Mass of the physics body. The mass of a composite body cannod be changed. Defaults to 1.0.
@property (nonatomic, assign) CGFloat massDeclared In
CCPhysicsBody.hnode
The CCNode to which this physics body is attached.
@property (nonatomic, readonly, weak) CCNode *nodeDeclared In
CCPhysicsBody.hsensor
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 sensorDeclared In
CCPhysicsBody.hsleeping
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 sleepingDeclared In
CCPhysicsBody.hsurfaceVelocity
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 surfaceVelocityDeclared In
CCPhysicsBody.htorque
Torque applied to this physics body this fixed timestep.
@property (nonatomic, assign) CGFloat torqueDeclared In
CCPhysicsBody.hClass Methods
bodyWithCircleOfRadius:andCenter:
Creates and retuns a circular physics body using the circle radius and center values specified.
+ (CCPhysicsBody *)bodyWithCircleOfRadius:(CGFloat)radius andCenter:(CGPoint)centerParameters
- radius
Circle radius.
- center
Circle center point.
Return Value
The CCPhysicsBody Object.
Declared In
CCPhysicsBody.hbodyWithPillFrom: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)cornerRadiusParameters
- from
Start point.
- to
End point.
- cornerRadius
Corner radius.
Return Value
The CCPhysicsBody Object.
Declared In
CCPhysicsBody.hbodyWithPolygonFromPoints: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)cornerRadiusParameters
- points
Points array pointer.
- count
Points count.
- cornerRadius
Corner radius.
Return Value
The CCPhysicsBody Object.
Declared In
CCPhysicsBody.hbodyWithPolylineFromPoints: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)loopedParameters
- 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.hbodyWithPolylineFromRect: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)cornerRadiusParameters
- rect
Rectangle perimeter.
- cornerRadius
Corner radius.
Return Value
The CCPhysicsBody Object.
Declared In
CCPhysicsBody.hbodyWithRect:cornerRadius:
Creates and returns a box shaped physics body with rounded corners.
+ (CCPhysicsBody *)bodyWithRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadiusParameters
- rect
Box dimensions.
- cornerRadius
Corner radius.
Return Value
The CCPhysicsBody Object.
Declared In
CCPhysicsBody.hInstance Methods
applyAngularImpulse:
Apply an angular impulse.
- (void)applyAngularImpulse:(CGFloat)impulseParameters
- impulse
Angular impulse.
Declared In
CCPhysicsBody.happlyForce:
Apply a force to the physics body.
- (void)applyForce:(CGPoint)forceParameters
- force
Force vector.
Declared In
CCPhysicsBody.happlyForce: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)pointDeclared In
CCPhysicsBody.happlyForce:atWorldPoint:
- (void)applyForce:(CGPoint)force atWorldPoint:(CGPoint)pointDeclared In
CCPhysicsBody.happlyImpulse:
Apply an impulse on the physics body.
- (void)applyImpulse:(CGPoint)impulseParameters
- impulse
Impulse vector.
Declared In
CCPhysicsBody.happlyImpulse: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)pointParameters
- impulse
Impulse vector.
- point
Point to apply impulse.
Declared In
CCPhysicsBody.happlyImpulse:atWorldPoint:
Apply an impulse and angular impulse on the physics body at the given point in absolute coordinates.
- (void)applyImpulse:(CGPoint)impulse atWorldPoint:(CGPoint)pointParameters
- impulse
Impulse vector.
- point
Point to apply impulse.
Declared In
CCPhysicsBody.happlyTorque:
Apply a torque on the physics body.
- (void)applyTorque:(CGFloat)torqueParameters
- torque
Torque.
Declared In
CCPhysicsBody.heachCollisionPair:
Iterate over all of the CCPhysicsCollisionPairs this body is currently in contact with.
- (void)eachCollisionPair:(void ( ^ ) ( CCPhysicsCollisionPair *pair ))blockParameters
- block
Collision block.
Discussion
Note: The CCPhysicsCollisionPair object is shared so you should not store a reference to it.
Declared In
CCPhysicsBody.h