Debian Codegraph @

doc/examples/CollisionCallback.html

raw symbols SCIP
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 &nbsp;&nbsp;<span style="color:#003369;">cpSpaceRemoveShape</span>(space, shape);<br />
5 &nbsp;&nbsp;<span style="color:#003369;">cpSpaceRemoveBody</span>(space, shape-&gt;body);<br />
6 &nbsp;&nbsp;<br />
7 &nbsp;&nbsp;<span style="color:#003369;">cpShapeFree</span>(shape);<br />
8 &nbsp;&nbsp;<span style="color:#003369;">cpBodyFree</span>(shape-&gt;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 &nbsp;&nbsp;<span style="color:#236e25;">// Get the cpShapes involved in the collision<br />
15 </span>&nbsp;&nbsp;<span style="color:#236e25;">// The order will be the same as you defined in the handler definition<br />
16 </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 />
17 </span>&nbsp;&nbsp;<span style="color:#003369;">CP_ARBITER_GET_SHAPES</span>(arb, a, b);<br />
18 &nbsp;&nbsp;<br />
19 &nbsp;&nbsp;<span style="color:#236e25;">// The macro expands exactly as if you had typed this:<br />
20 </span>&nbsp;&nbsp;<span style="color:#236e25;">// cpShape *a, *b; cpArbiterGetShapes(arb, &amp;a, &amp;b);<br />
21 </span>&nbsp;&nbsp;<br />
22 &nbsp;&nbsp;<span style="color:#236e25;">// Add a post step callback to safely remove the body and shape from the space.<br />
23 </span>&nbsp;&nbsp;<span style="color:#236e25;">// Calling cpSpaceRemove*() directly from a collision handler callback can cause crashes.<br />
24 </span>&nbsp;&nbsp;<span style="color:#003369;">cpSpaceAddPostStepCallback</span>(space, (cpPostStepFunc)postStepRemove, b, <span style="color:#881350;">NULL</span>);<br />
25 &nbsp;&nbsp;<br />
26 &nbsp;&nbsp;<span style="color:#236e25;">// The object is dead, don&rsquo;t process the collision further<br />
27 </span>&nbsp;&nbsp;<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&rsquo;s shape and body from the space as soon as it&rsquo;s hit by a bullet <br />
35 </span>cpCollisionHandler *handler = <span style="color:#003369;">cpSpaceAddCollisionHandler</span>(space, BULLET_TYPE, MONSTER_TYPE);<br />
36 handler-&gt;beginFunc = begin;<br />
37 <br />
38 </div>