| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 2 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 3 |
<head> |
| 4 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 5 |
<title>Chipmunk Game Dynamics Documentation</title> |
| 6 |
<link rel="stylesheet" type="text/css" href="stylesheet.css" /> |
| 7 |
</head> |
| 8 |
<body> |
| 9 |
|
| 10 |
<h1>Example Code Snippets:</h1> |
| 11 |
|
| 12 |
|
| 13 |
<a name="Transform" /> |
| 14 |
<h2>Getting a Transformation from a Rigid Body:</h2> |
| 15 |
|
| 16 |
<p>You can quickly and easily build a transformation matrix from a Chipmunk body. The following code is for OpenGL, but it should be trivial to modify for DirectX or affine transforms. (Note that OpenGL matrices are column-major)</p> |
| 17 |
|
| 18 |
<pre style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">cpVect pos = body->p; |
| 19 |
cpVect rot = body->rot; |
| 20 |
|
| 21 |
GLFloat matrix[<span style="color:#0000ff;">16</span>] = { |
| 22 |
rot.x, rot.y, <span style="color:#0000ff;">0.0f</span>, <span style="color:#0000ff;">0.0f</span>, |
| 23 |
-rot.y, rot.x, <span style="color:#0000ff;">0.0f</span>, <span style="color:#0000ff;">0.0f</span>, |
| 24 |
<span style="color:#0000ff;">0.0f</span>, <span style="color:#0000ff;">0.0f</span>, <span style="color:#0000ff;">1.0f</span>, <span style="color:#0000ff;">0.0f</span>, |
| 25 |
pos.x, pos.y, <span style="color:#0000ff;">0.0f</span>, <span style="color:#0000ff;">1.0f</span>, |
| 26 |
}; |
| 27 |
|
| 28 |
<span style="color:#003369;">glMultMatrixf</span>(matrix.farr); |
| 29 |
</pre> |
| 30 |
|
| 31 |
|
| 32 |
<a name="CollisionCallbacks" /> |
| 33 |
<h2>Collision Callbacks:</h2> |
| 34 |
|
| 35 |
<p>This snippet demonstrates several Chipmunk collision callback features. It defines a collision handler that is called when collision shapes start touching and also a post-step callback to remove the collision shape and body.</p> |
| 36 |
|
| 37 |
<pre style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><strong><span style="color:#881350;">static</span></strong> <strong><span style="color:#881350;">void</span></strong> |
| 38 |
<span style="color:#003369;">postStepRemove</span>(cpSpace *space, cpShape *shape, <strong><span style="color:#881350;">void</span></strong> *unused) |
| 39 |
{ |
| 40 |
<span style="color:#003369;">cpSpaceRemoveBody</span>(space, shape->body); |
| 41 |
<span style="color:#003369;">cpSpaceRemoveShape</span>(space, shape); |
| 42 |
|
| 43 |
<span style="color:#003369;">cpShapeFree</span>(shape); |
| 44 |
<span style="color:#003369;">cpBodyFree</span>(shape->body); |
| 45 |
} |
| 46 |
|
| 47 |
<strong><span style="color:#881350;">static</span></strong> <strong><span style="color:#881350;">int</span></strong> |
| 48 |
<span style="color:#003369;">begin</span>(cpArbiter *arb, cpSpace *space, <strong><span style="color:#881350;">void</span></strong> *unused) |
| 49 |
{ |
| 50 |
<em><span style="color:#236e25;">// Get the cpShapes involved in the collision |
| 51 |
</span></em> <em><span style="color:#236e25;">// The order will be the same as you defined in the handler definition |
| 52 |
</span></em> <em><span style="color:#236e25;">// a->collision_type will be BULLET_TYPE and b->collision_type will be MONSTER_TYPE |
| 53 |
</span></em> cpShape *a, *b; <span style="color:#003369;">cpArbiterGetShapes</span>(arb, &a, &b); |
| 54 |
|
| 55 |
<em><span style="color:#236e25;">// Alternatively you can use the CP_ARBITER_GET_SHAPES() macro |
| 56 |
</span></em> <em><span style="color:#236e25;">// It defines and sets the variables for you. |
| 57 |
</span></em> <em><span style="color:#236e25;">//CP_ARBITER_GET_SHAPES(arb, a, b); |
| 58 |
</span></em> |
| 59 |
<em><span style="color:#236e25;">// Add a post step callback to safely remove the body and shape from the space. |
| 60 |
</span></em> <em><span style="color:#236e25;">// Calling cpSpaceRemove*() directly from a collision handler callback can cause crashes. |
| 61 |
</span></em> <span style="color:#003369;">cpSpaceAddPostStepCallback</span>(space, (cpPostStepFunc)postStepRemove, b, <strong><span style="color:#881350;">NULL</span></strong>); |
| 62 |
|
| 63 |
<em><span style="color:#236e25;">// The object is dead, don’t process the collision further |
| 64 |
</span></em> <strong><span style="color:#881350;">return</span></strong> <span style="color:#0000ff;">0</span>; |
| 65 |
} |
| 66 |
|
| 67 |
<span style="color:#683821;">#define BULLET_TYPE </span><span style="color:#0000ff;">1</span><span style="color:#683821;"> |
| 68 |
#define MONSTER_TYPE </span><span style="color:#0000ff;">2</span><span style="color:#683821;"> |
| 69 |
</span> |
| 70 |
<em><span style="color:#236e25;">// Define a collision handler for bullets and monsters |
| 71 |
// Kill the monster by removing it’s shape and body from the space as soon as it’s hit by a bullet |
| 72 |
</span></em><span style="color:#003369;">cpSpaceAddCollisionHandler</span>(space, BULLET_TYPE, MONSTER_TYPE, begin, <strong><span style="color:#881350;">NULL</span></strong>, <strong><span style="color:#881350;">NULL</span></strong>, <strong><span style="color:#881350;">NULL</span></strong>, <strong><span style="color:#881350;">NULL</span></strong>);</pre> |
| 73 |
|
| 74 |
<p>For more callback examples, see the <a href="http://code.google.com/p/chipmunk-physics/source/browse/trunk/Demo/OneWay.c">One Way Platform Demo</a>, <a href="http://code.google.com/p/chipmunk-physics/source/browse/trunk/Demo/Sensors.c">Sensors Demo</a>, or the <a href="http://code.google.com/p/chipmunk-physics/source/browse/trunk/Demo/Player.c">Player Demo</a>.</p> |
| 75 |
|
| 76 |
|
| 77 |
<a name="Query" /> |
| 78 |
<h2>Query Examples:</h2> |
| 79 |
|
| 80 |
<p>The following example is taken directly from <a href="http://code.google.com/p/chipmunk-physics/source/browse/trunk/Demo/ChipmunkDemo.c#319">ChipmunkDemo.c</a>. When the mouse is clicked, a point query is performed to see if there is a shape under the mouse. If there is, it adds a joint to the body that links it to the mouse's movement.</p> |
| 81 |
|
| 82 |
<pre style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><strong><span style="color:#881350;">static</span></strong> <strong><span style="color:#881350;">void</span></strong> |
| 83 |
<span style="color:#003369;">click</span>(<strong><span style="color:#881350;">int</span></strong> button, <strong><span style="color:#881350;">int</span></strong> state, <strong><span style="color:#881350;">int</span></strong> x, <strong><span style="color:#881350;">int</span></strong> y) |
| 84 |
{ |
| 85 |
<strong><span style="color:#881350;">if</span></strong>(button == GLUT_LEFT_BUTTON){ |
| 86 |
<strong><span style="color:#881350;">if</span></strong>(state == GLUT_DOWN){ |
| 87 |
cpVect point = <span style="color:#003369;">mouseToSpace</span>(x, y); |
| 88 |
|
| 89 |
cpShape *shape = <span style="color:#003369;">cpSpacePointQueryFirst</span>(space, point, GRABABLE_MASK_BIT, <span style="color:#0000ff;">0</span>); |
| 90 |
<strong><span style="color:#881350;">if</span></strong>(shape){ |
| 91 |
cpBody *body = shape->body; |
| 92 |
mouseJoint = <span style="color:#003369;">cpPivotJointNew2</span>(mouseBody, body, cpvzero, <span style="color:#003369;">cpBodyWorld2Local</span>(body, point)); |
| 93 |
mouseJoint->maxForce = <span style="color:#0000ff;">50000.0f</span>; |
| 94 |
mouseJoint->biasCoef = <span style="color:#0000ff;">0.15f</span>; |
| 95 |
<span style="color:#003369;">cpSpaceAddConstraint</span>(space, mouseJoint); |
| 96 |
} |
| 97 |
} <strong><span style="color:#881350;">else</span></strong> <strong><span style="color:#881350;">if</span></strong>(mouseJoint){ |
| 98 |
<span style="color:#003369;">cpSpaceRemoveConstraint</span>(space, mouseJoint); |
| 99 |
<span style="color:#003369;">cpConstraintFree</span>(mouseJoint); |
| 100 |
mouseJoint = <strong><span style="color:#881350;">NULL</span></strong>; |
| 101 |
} |
| 102 |
} |
| 103 |
}</pre> |
| 104 |
|
| 105 |
<p>Perform a segment query to see if a laser beam hits a shape. We want to draw particles at both the position where the beam enters and exits the shape.</p> |
| 106 |
|
| 107 |
<pre style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">cpVect a = <span style="color:#003369;">cpv</span>(...), b = <span style="color:#003369;">cpv</span>(...); |
| 108 |
|
| 109 |
cpSegmentQueryInfo info = {}; |
| 110 |
<strong><span style="color:#881350;">if</span></strong>(<span style="color:#003369;">cpSpaceSegmentQueryFirst</span>(space, a, b, -<span style="color:#0000ff;">1</span>, <span style="color:#0000ff;">0</span>, &info)){ |
| 111 |
cpSegmentQueryInfo info2; |
| 112 |
<span style="color:#003369;">cpShapeSegmentQuery</span>(info.shape, b, a, &info2); |
| 113 |
|
| 114 |
cpVect enterPoint = <span style="color:#003369;">cpSegmentQueryHitPoint</span>(a, b, info); |
| 115 |
cpVect exitPoint = <span style="color:#003369;">cpSegmentQueryHitPoint</span>(b, a, info2); |
| 116 |
}</pre> |
| 117 |
|
| 118 |
</body> |
| 119 |
</html> |