File: CollisionCallback.html

package info (click to toggle)
chipmunk 7.0.3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,968 kB
  • sloc: ansic: 29,265; objc: 4,313; ruby: 409; makefile: 10; sh: 1
file content (38 lines) | stat: -rw-r--r-- 3,072 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<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>