36 lines
3.0 KiB
HTML
36 lines
3.0 KiB
HTML
|
|
<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>
|
||
|
|
<span style="color:#003369;">postStepRemove</span>(cpSpace *space, cpShape *shape, <strong><span style="color:#881350;">void</span></strong> *unused)
|
||
|
|
{
|
||
|
|
<span style="color:#003369;">cpSpaceRemoveShape</span>(space, shape);
|
||
|
|
<span style="color:#003369;">cpSpaceRemoveBody</span>(space, shape->body);
|
||
|
|
|
||
|
|
<span style="color:#003369;">cpShapeFree</span>(shape);
|
||
|
|
<span style="color:#003369;">cpBodyFree</span>(shape->body);
|
||
|
|
}
|
||
|
|
|
||
|
|
<strong><span style="color:#881350;">static</span></strong> <strong><span style="color:#881350;">int</span></strong>
|
||
|
|
<span style="color:#003369;">begin</span>(cpArbiter *arb, cpSpace *space, <strong><span style="color:#881350;">void</span></strong> *unused)
|
||
|
|
{
|
||
|
|
<em><span style="color:#236e25;">// Get the cpShapes involved in the collision
|
||
|
|
</span></em> <em><span style="color:#236e25;">// The order will be the same as you defined in the handler definition
|
||
|
|
</span></em> <em><span style="color:#236e25;">// a->collision_type will be BULLET_TYPE and b->collision_type will be MONSTER_TYPE
|
||
|
|
</span></em> <span style="color:#003369;">CP_ARBITER_GET_SHAPES</span>(arb, a, b);
|
||
|
|
|
||
|
|
<em><span style="color:#236e25;">// The macro expands exactly as if you had typed this:
|
||
|
|
</span></em> <em><span style="color:#236e25;">// cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b);
|
||
|
|
</span></em>
|
||
|
|
<em><span style="color:#236e25;">// Add a post step callback to safely remove the body and shape from the space.
|
||
|
|
</span></em> <em><span style="color:#236e25;">// Calling cpSpaceRemove*() directly from a collision handler callback can cause crashes.
|
||
|
|
</span></em> <span style="color:#003369;">cpSpaceAddPostStepCallback</span>(space, (cpPostStepFunc)postStepRemove, b, <strong><span style="color:#881350;">NULL</span></strong>);
|
||
|
|
|
||
|
|
<em><span style="color:#236e25;">// The object is dead, don’t process the collision further
|
||
|
|
</span></em> <strong><span style="color:#881350;">return</span></strong> <span style="color:#0000ff;">0</span>;
|
||
|
|
}
|
||
|
|
|
||
|
|
<span style="color:#683821;">#define BULLET_TYPE </span><span style="color:#0000ff;">1</span><span style="color:#683821;">
|
||
|
|
#define MONSTER_TYPE </span><span style="color:#0000ff;">2</span><span style="color:#683821;">
|
||
|
|
</span>
|
||
|
|
<em><span style="color:#236e25;">// Define a collision handler for bullets and monsters
|
||
|
|
// Kill the monster by removing it’s shape and body from the space as soon as it’s hit by a bullet
|
||
|
|
</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>
|