64 lines
6.5 KiB
HTML
64 lines
6.5 KiB
HTML
|
<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 />
|
||
|
#include </span><span style="color:#f4181b;"><chipmunk.h></span><span style="color:#78492a;"><br />
|
||
|
</span><br />
|
||
|
<span style="color:#a71790;">int</span> <span style="color:#003668;">main</span>(<span style="color:#a71790;">void</span>){<br />
|
||
|
<em><span style="color:#236e25;">// cpVect is a 2D vector and cpv() is a shortcut for initializing them.<br />
|
||
|
</span></em> cpVect gravity = <span style="color:#003668;">cpv</span>(<span style="color:#0000ff;">0</span>, -<span style="color:#0000ff;">100</span>);<br />
|
||
|
<br />
|
||
|
<em><span style="color:#236e25;">// Create an empty space.<br />
|
||
|
</span></em> cpSpace *space = <span style="color:#003668;">cpSpaceNew</span>();<br />
|
||
|
<span style="color:#003668;">cpSpaceSetGravity</span>(space, gravity);<br />
|
||
|
<br />
|
||
|
<em><span style="color:#236e25;">// Add a static line segment shape for the ground.<br />
|
||
|
</span></em> <em><span style="color:#236e25;">// We'll make it slightly tilted so the ball will roll off.<br />
|
||
|
</span></em> <em><span style="color:#236e25;">// We attach it to a static body to tell Chipmunk it shouldn't be movable.<br />
|
||
|
</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 />
|
||
|
<span style="color:#003668;">cpShapeSetFriction</span>(ground, <span style="color:#0000ff;">1</span>);<br />
|
||
|
<span style="color:#003668;">cpSpaceAddShape</span>(space, ground);<br />
|
||
|
<br />
|
||
|
<em><span style="color:#236e25;">// Now let's make a ball that falls onto the line and rolls off.<br />
|
||
|
</span></em> <em><span style="color:#236e25;">// First we need to make a cpBody to hold the physical properties of the object.<br />
|
||
|
</span></em> <em><span style="color:#236e25;">// These include the mass, position, velocity, angle, etc. of the object.<br />
|
||
|
</span></em> <em><span style="color:#236e25;">// Then we attach collision shapes to the cpBody to give it a size and shape.<br />
|
||
|
</span></em> <br />
|
||
|
cpFloat radius = <span style="color:#0000ff;">5</span>;<br />
|
||
|
cpFloat mass = <span style="color:#0000ff;">1</span>;<br />
|
||
|
<br />
|
||
|
<em><span style="color:#236e25;">// The moment of inertia is like mass for rotation<br />
|
||
|
</span></em> <em><span style="color:#236e25;">// Use the cpMomentFor*() functions to help you approximate it.<br />
|
||
|
</span></em> cpFloat moment = <span style="color:#003668;">cpMomentForCircle</span>(mass, <span style="color:#0000ff;">0</span>, radius, cpvzero);<br />
|
||
|
<br />
|
||
|
<em><span style="color:#236e25;">// The cpSpaceAdd*() functions return the thing that you are adding.<br />
|
||
|
</span></em> <em><span style="color:#236e25;">// It's convenient to create and add an object in one line.<br />
|
||
|
</span></em> cpBody *ballBody = <span style="color:#003668;">cpSpaceAddBody</span>(space, <span style="color:#003668;">cpBodyNew</span>(mass, moment));<br />
|
||
|
<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 />
|
||
|
<br />
|
||
|
<em><span style="color:#236e25;">// Now we create the collision shape for the ball.<br />
|
||
|
</span></em> <em><span style="color:#236e25;">// You can create multiple collision shapes that point to the same body.<br />
|
||
|
</span></em> <em><span style="color:#236e25;">// They will all be attached to the body and move around to follow it.<br />
|
||
|
</span></em> cpShape *ballShape = <span style="color:#003668;">cpSpaceAddShape</span>(space, <span style="color:#003668;">cpCircleShapeNew</span>(ballBody, radius, cpvzero));<br />
|
||
|
<span style="color:#003668;">cpShapeSetFriction</span>(ballShape, <span style="color:#0000ff;">0.7</span>);<br />
|
||
|
<br />
|
||
|
<em><span style="color:#236e25;">// Now that it's all set up, we simulate all the objects in the space by<br />
|
||
|
</span></em> <em><span style="color:#236e25;">// stepping forward through time in small increments called steps.<br />
|
||
|
</span></em> <em><span style="color:#236e25;">// It is *highly* recommended to use a fixed size time step.<br />
|
||
|
</span></em> cpFloat timeStep = <span style="color:#0000ff;">1.0</span>/<span style="color:#0000ff;">60.0</span>;<br />
|
||
|
<span style="color:#a71790;">for</span>(cpFloat time = <span style="color:#0000ff;">0</span>; time < <span style="color:#0000ff;">2</span>; time += timeStep){<br />
|
||
|
cpVect pos = <span style="color:#003668;">cpBodyGetPosition</span>(ballBody);<br />
|
||
|
cpVect vel = <span style="color:#003668;">cpBodyGetVelocity</span>(ballBody);<br />
|
||
|
<span style="color:#003668;">printf</span>(<br />
|
||
|
<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 />
|
||
|
time, pos.x, pos.y, vel.x, vel.y<br />
|
||
|
);<br />
|
||
|
<br />
|
||
|
<span style="color:#003668;">cpSpaceStep</span>(space, timeStep);<br />
|
||
|
}<br />
|
||
|
<br />
|
||
|
<em><span style="color:#236e25;">// Clean up our objects and exit!<br />
|
||
|
</span></em> <span style="color:#003668;">cpShapeFree</span>(ballShape);<br />
|
||
|
<span style="color:#003668;">cpBodyFree</span>(ballBody);<br />
|
||
|
<span style="color:#003668;">cpShapeFree</span>(ground);<br />
|
||
|
<span style="color:#003668;">cpSpaceFree</span>(space);<br />
|
||
|
<br />
|
||
|
<span style="color:#a71790;">return</span> <span style="color:#0000ff;">0</span>;<br />
|
||
|
}</div>
|