| 1 | <div 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; "><span style="color:#881350;">static</span> <span style="color:#881350;">void</span><br /> |
| 2 | <span style="color:#003369;">postStepRemove</span>(cpSpace *space, cpShape *shape, <span style="color:#881350;">void</span> *unused)<br /> |
| 3 | {<br /> |
| 4 | <span style="color:#003369;">cpSpaceRemoveShape</span>(space, shape);<br /> |
| 5 | <span style="color:#003369;">cpSpaceRemoveBody</span>(space, shape->body);<br /> |
| 6 | <br /> |
| 7 | <span style="color:#003369;">cpShapeFree</span>(shape);<br /> |
| 8 | <span style="color:#003369;">cpBodyFree</span>(shape->body);<br /> |
| 9 | }<br /> |
| 10 | <br /> |
| 11 | <span style="color:#881350;">static</span> <span style="color:#881350;">int</span><br /> |
| 12 | <span style="color:#003369;">begin</span>(cpArbiter *arb, cpSpace *space, <span style="color:#881350;">void</span> *data)<br /> |
| 13 | {<br /> |
| 14 | <span style="color:#236e25;">// Get the cpShapes involved in the collision<br /> |
| 15 | </span> <span style="color:#236e25;">// The order will be the same as you defined in the handler definition<br /> |
| 16 | </span> <span style="color:#236e25;">// a->collision_type will be BULLET_TYPE and b->collision_type will be MONSTER_TYPE<br /> |
| 17 | </span> <span style="color:#003369;">CP_ARBITER_GET_SHAPES</span>(arb, a, b);<br /> |
| 18 | <br /> |
| 19 | <span style="color:#236e25;">// The macro expands exactly as if you had typed this:<br /> |
| 20 | </span> <span style="color:#236e25;">// cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b);<br /> |
| 21 | </span> <br /> |
| 22 | <span style="color:#236e25;">// Add a post step callback to safely remove the body and shape from the space.<br /> |
| 23 | </span> <span style="color:#236e25;">// Calling cpSpaceRemove*() directly from a collision handler callback can cause crashes.<br /> |
| 24 | </span> <span style="color:#003369;">cpSpaceAddPostStepCallback</span>(space, (cpPostStepFunc)postStepRemove, b, <span style="color:#881350;">NULL</span>);<br /> |
| 25 | <br /> |
| 26 | <span style="color:#236e25;">// The object is dead, don’t process the collision further<br /> |
| 27 | </span> <span style="color:#881350;">return</span> <span style="color:#0000ff;">0</span>;<br /> |
| 28 | }<br /> |
| 29 | <br /> |
| 30 | <span style="color:#683821;">#define BULLET_TYPE </span><span style="color:#0000ff;">1</span><span style="color:#683821;"><br /> |
| 31 | #define MONSTER_TYPE </span><span style="color:#0000ff;">2</span><span style="color:#683821;"><br /> |
| 32 | </span><br /> |
| 33 | <span style="color:#236e25;">// Define a collision handler for bullets and monsters<br /> |
| 34 | // Kill the monster by removing it’s shape and body from the space as soon as it’s hit by a bullet <br /> |
| 35 | </span>cpCollisionHandler *handler = <span style="color:#003369;">cpSpaceAddCollisionHandler</span>(space, BULLET_TYPE, MONSTER_TYPE);<br /> |
| 36 | handler->beginFunc = begin;<br /> |
| 37 | <br /> |
| 38 | </div> |