| 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:#78492a;">#include </span><span style="color:#f4181b;"><stdio.h></span><span style="color:#78492a;"><br /> |
| 2 | #include </span><span style="color:#f4181b;"><chipmunk.h></span><span style="color:#78492a;"><br /> |
| 3 | </span><br /> |
| 4 | <span style="color:#a71790;">int</span> <span style="color:#003668;">main</span>(<span style="color:#a71790;">void</span>){<br /> |
| 5 | <em><span style="color:#236e25;">// cpVect is a 2D vector and cpv() is a shortcut for initializing them.<br /> |
| 6 | </span></em> cpVect gravity = <span style="color:#003668;">cpv</span>(<span style="color:#0000ff;">0</span>, -<span style="color:#0000ff;">100</span>);<br /> |
| 7 | <br /> |
| 8 | <em><span style="color:#236e25;">// Create an empty space.<br /> |
| 9 | </span></em> cpSpace *space = <span style="color:#003668;">cpSpaceNew</span>();<br /> |
| 10 | <span style="color:#003668;">cpSpaceSetGravity</span>(space, gravity);<br /> |
| 11 | <br /> |
| 12 | <em><span style="color:#236e25;">// Add a static line segment shape for the ground.<br /> |
| 13 | </span></em> <em><span style="color:#236e25;">// We'll make it slightly tilted so the ball will roll off.<br /> |
| 14 | </span></em> <em><span style="color:#236e25;">// We attach it to a static body to tell Chipmunk it shouldn't be movable.<br /> |
| 15 | </span></em> cpShape *ground = <span style="color:#003668;">cpSegmentShapeNew</span>(<span style="color:#003668;">cpSpaceGetStaticBody</span>(space), <span style="color:#003668;">cpv</span>(-<span style="color:#0000ff;">20</span>, <span style="color:#0000ff;">5</span>), <span style="color:#003668;">cpv</span>(<span style="color:#0000ff;">20</span>, -<span style="color:#0000ff;">5</span>), <span style="color:#0000ff;">0</span>);<br /> |
| 16 | <span style="color:#003668;">cpShapeSetFriction</span>(ground, <span style="color:#0000ff;">1</span>);<br /> |
| 17 | <span style="color:#003668;">cpSpaceAddShape</span>(space, ground);<br /> |
| 18 | <br /> |
| 19 | <em><span style="color:#236e25;">// Now let's make a ball that falls onto the line and rolls off.<br /> |
| 20 | </span></em> <em><span style="color:#236e25;">// First we need to make a cpBody to hold the physical properties of the object.<br /> |
| 21 | </span></em> <em><span style="color:#236e25;">// These include the mass, position, velocity, angle, etc. of the object.<br /> |
| 22 | </span></em> <em><span style="color:#236e25;">// Then we attach collision shapes to the cpBody to give it a size and shape.<br /> |
| 23 | </span></em> <br /> |
| 24 | cpFloat radius = <span style="color:#0000ff;">5</span>;<br /> |
| 25 | cpFloat mass = <span style="color:#0000ff;">1</span>;<br /> |
| 26 | <br /> |
| 27 | <em><span style="color:#236e25;">// The moment of inertia is like mass for rotation<br /> |
| 28 | </span></em> <em><span style="color:#236e25;">// Use the cpMomentFor*() functions to help you approximate it.<br /> |
| 29 | </span></em> cpFloat moment = <span style="color:#003668;">cpMomentForCircle</span>(mass, <span style="color:#0000ff;">0</span>, radius, cpvzero);<br /> |
| 30 | <br /> |
| 31 | <em><span style="color:#236e25;">// The cpSpaceAdd*() functions return the thing that you are adding.<br /> |
| 32 | </span></em> <em><span style="color:#236e25;">// It's convenient to create and add an object in one line.<br /> |
| 33 | </span></em> cpBody *ballBody = <span style="color:#003668;">cpSpaceAddBody</span>(space, <span style="color:#003668;">cpBodyNew</span>(mass, moment));<br /> |
| 34 | <span style="color:#003668;">cpBodySetPosition</span>(ballBody, <span style="color:#003668;">cpv</span>(<span style="color:#0000ff;">0</span>, <span style="color:#0000ff;">15</span>));<br /> |
| 35 | <br /> |
| 36 | <em><span style="color:#236e25;">// Now we create the collision shape for the ball.<br /> |
| 37 | </span></em> <em><span style="color:#236e25;">// You can create multiple collision shapes that point to the same body.<br /> |
| 38 | </span></em> <em><span style="color:#236e25;">// They will all be attached to the body and move around to follow it.<br /> |
| 39 | </span></em> cpShape *ballShape = <span style="color:#003668;">cpSpaceAddShape</span>(space, <span style="color:#003668;">cpCircleShapeNew</span>(ballBody, radius, cpvzero));<br /> |
| 40 | <span style="color:#003668;">cpShapeSetFriction</span>(ballShape, <span style="color:#0000ff;">0.7</span>);<br /> |
| 41 | <br /> |
| 42 | <em><span style="color:#236e25;">// Now that it's all set up, we simulate all the objects in the space by<br /> |
| 43 | </span></em> <em><span style="color:#236e25;">// stepping forward through time in small increments called steps.<br /> |
| 44 | </span></em> <em><span style="color:#236e25;">// It is *highly* recommended to use a fixed size time step.<br /> |
| 45 | </span></em> cpFloat timeStep = <span style="color:#0000ff;">1.0</span>/<span style="color:#0000ff;">60.0</span>;<br /> |
| 46 | <span style="color:#a71790;">for</span>(cpFloat time = <span style="color:#0000ff;">0</span>; time < <span style="color:#0000ff;">2</span>; time += timeStep){<br /> |
| 47 | cpVect pos = <span style="color:#003668;">cpBodyGetPosition</span>(ballBody);<br /> |
| 48 | cpVect vel = <span style="color:#003668;">cpBodyGetVelocity</span>(ballBody);<br /> |
| 49 | <span style="color:#003668;">printf</span>(<br /> |
| 50 | <span style="color:#f4181b;">"Time is %5.2f. ballBody is at (%5.2f, %5.2f). It's velocity is (%5.2f, %5.2f)\n"</span>,<br /> |
| 51 | time, pos.x, pos.y, vel.x, vel.y<br /> |
| 52 | );<br /> |
| 53 | <br /> |
| 54 | <span style="color:#003668;">cpSpaceStep</span>(space, timeStep);<br /> |
| 55 | }<br /> |
| 56 | <br /> |
| 57 | <em><span style="color:#236e25;">// Clean up our objects and exit!<br /> |
| 58 | </span></em> <span style="color:#003668;">cpShapeFree</span>(ballShape);<br /> |
| 59 | <span style="color:#003668;">cpBodyFree</span>(ballBody);<br /> |
| 60 | <span style="color:#003668;">cpShapeFree</span>(ground);<br /> |
| 61 | <span style="color:#003668;">cpSpaceFree</span>(space);<br /> |
| 62 | <br /> |
| 63 | <span style="color:#a71790;">return</span> <span style="color:#0000ff;">0</span>;<br /> |
| 64 | }</div> |