prosperon/source/engine/thirdparty/Chipmunk2D/doc/examples/CollisionCallback.html
2022-01-25 15:22:03 +00:00

38 lines
3 KiB
HTML
Executable file

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