Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chipmunk Physics Remember that we talked about this a bit when we did collision handlers for the space ship integration task (SpritesActionsPhysicsSound).

Similar presentations


Presentation on theme: "Chipmunk Physics Remember that we talked about this a bit when we did collision handlers for the space ship integration task (SpritesActionsPhysicsSound)."— Presentation transcript:

1 Chipmunk Physics Remember that we talked about this a bit when we did collision handlers for the space ship integration task (SpritesActionsPhysicsSound). Also available code.google.com http://code.google.com/p/chipmunk-physics/ It's a 2D rigid body physics library written in “C”.

2 2D Rigid Body Physics The bodies are 2D only, and can overlap (i.e., look carefully at some of the asteroids). The bodies do not deform when they hit other objects. e.g., Couldn't simulate collision damage on a car. The bodies obey some “simulated physical” laws.

3 Overview There are 4 basic objects: Space holds the other 3 objects. Body holds the properties (mass, position, rotation, velocity, angular momentum, etc) associated with a physical object. Shapes attached to a body; defines the surface boundaries of a body for collision purposes; also the friction and elasticity. Constraints define the type of interactions that bodies can have.

4 To Start to Use From our test code you know that you need to: Initialize the system with 'cpInitChipmunk()' AsteroidsLayer init method. Schedule a method to be fired at intervals (usually 1/60 sec) AsteroidsLayer step method. Invoke the cpSpaceStep function in the scheduled method.

5 Basic Types we have used The library defines some basic C types other than the main ones (space, shape, body, constraint). We have used: cpFloat which is a double cpVect which is a 2D vector (i.e., ). e.g., used to define the boundries of a shape. cpCollisionType allows you to categorize things that can collide with other things. e.g., asteroid, space ship, missile, alien ship.

6 Defines Operations on Vectors In addition to the trigonometry functions found in math.h (see AsteroidLayer.m): Addition, subtraction, multiplication with float, negative, dot product, cross product, rotate, length, normalize, distance between, vector-angle, angle-vector operations.

7 cpBody Defined using vectors: position, velocity, current force applied to. Defined using floats: mass, moment (a measure of an object's resistance to changes to its change in rotation), angle, rotational velocity, torque applied to. Defines a place for the user to store a data pointer (e.g., game object associated with the body).

8 cpBody Functions Memory management (alloc & free) Moment of inertia assuming certain shapes (circle, segment, polygon, box) Getters & Setters maintained cached data Incremental (velocity update, position update) Coordinate conversion Application of forces and torque

9 cpShape Currently three types: (circle, line, convex polygon) Defined by: elasticty (bouncability), friction, collision_type, associated body, layers, & groups. Layers allow shapes to collide only if they are in the same “plane” Shapes in the same “group” never collide. Defines a place for the user to store a data pointer (e.g., game object associated with the body).

10 Collisions Collisions are defined as talking place between two shapes Handlers can be defined to be called when: Begin: two shapes just started touching Are: can get back collision force End: two shapes just stopped touching Callbacks are used in removing objects during a collision.

11 cpSpace Groups bodies, and shapes together for simulation. Simulation iterations define how much computation is done to make something “solid”. Defined by: iterations, gravity, viscous dampening. An “infinite mass, static body” can be attached to a space. Gives it a “immovable body” to attach others to via constraints (e.g., rotate about a point or slide to within limits)

12 cpSpace Functions Same memory management functions as the others (alloc, free) Ability to add/remove: bodies, shapes, constraints Ability to simulate (cpSpaceStep)

13 cpConstraint Describes how two bodies interact or constrain each other's movements (e.g., joints act like joints in your body) Defined by: two bodies, max force used to act on them, a data pointer. Some examples: http://www.youtube.com/watch?v=ZgJJZTS0aMM &feature=player_embedded

14 Our Tie in with physics engine We subclassed CCSprite so that: The body and collision shape are created at init time The 'setPosition' method also tells the physics engine Delete the body and shape with the sprite is deallocated http://www.cocos2d- iphone.org/wiki/doku.php/prog_guide:sprites#how _to_subclass_sprites


Download ppt "Chipmunk Physics Remember that we talked about this a bit when we did collision handlers for the space ship integration task (SpritesActionsPhysicsSound)."

Similar presentations


Ads by Google