| 1 | <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; "><em><span style="color:#236e25;">// Construct a pile of boxes. |
| 2 | // Force them to sleep until the first time they are touched. |
| 3 | // Group them together so that touching any box wakes all of them. |
| 4 | </span></em>cpFloat size = <span style="color:#0000ff;">20</span>; |
| 5 | cpFloat mass = <span style="color:#0000ff;">1</span>; |
| 6 | cpFloat moment = <span style="color:#003369;">cpMomentForBox</span>(mass, size, size); |
| 7 | |
| 8 | cpBody *lastBody = <strong><span style="color:#881350;">NULL</span></strong>; |
| 9 | |
| 10 | <strong><span style="color:#881350;">for</span></strong>(<strong><span style="color:#881350;">int</span></strong> i=<span style="color:#0000ff;">0</span>; i<<span style="color:#0000ff;">5</span>; i++){ |
| 11 | cpBody *body = <span style="color:#003369;">cpSpaceAddBody</span>(space, <span style="color:#003369;">cpBodyNew</span>(mass, moment)); |
| 12 | <span style="color:#003369;">cpBodySetPos</span>(body, <span style="color:#003369;">cpv</span>(<span style="color:#0000ff;">0</span>, i*size)); |
| 13 | |
| 14 | cpShape *shape = <span style="color:#003369;">cpSpaceAddShape</span>(space, <span style="color:#003369;">cpBoxShapeNew</span>(body, size, size)); |
| 15 | <span style="color:#003369;">cpShapeSetFriction</span>(shape, <span style="color:#0000ff;">0.7</span>); |
| 16 | |
| 17 | <em><span style="color:#236e25;">// You can use any sleeping body as a group identifier. |
| 18 | </span></em> <em><span style="color:#236e25;">// Here we just keep a reference to the last body we initialized. |
| 19 | </span></em> <em><span style="color:#236e25;">// Passing NULL as the group starts a new sleeping group. |
| 20 | </span></em> <em><span style="color:#236e25;">// You MUST do this after completely initializing the object. |
| 21 | </span></em> <em><span style="color:#236e25;">// Attaching shapes or calling setter functions will wake the body back up. |
| 22 | </span></em> <span style="color:#003369;">cpBodySleepWithGroup</span>(body, lastBody); |
| 23 | lastBody = body; |
| 24 | } |
| 25 | </pre> |