10
|
1 import chipmunkd.chipmunk;
|
|
2
|
|
3 import std.stdio;
|
|
4
|
|
5 void main()
|
|
6 {
|
|
7 // this allocated internal memory of the engine and must be called befor any collision can occur otherwise things get ugly
|
|
8 cpInitChipmunk();
|
|
9
|
|
10 // Create a space, a space is a simulation world. It simulates the motions of rigid bodies,
|
|
11 // handles collisions between them, and simulates the joints between them.
|
|
12 cpSpace* space = cpSpaceNew();
|
|
13
|
|
14
|
|
15 // Lets set some parameters of the space:
|
|
16 // More iterations make the simulation more accurate but slower
|
|
17 space.iterations = 10;
|
|
18 // These parameters tune the efficiency of the collision detection.
|
|
19 // For more info: http://code.google.com/p/chipmunk-physics/wiki/cpSpace
|
|
20 cpSpaceResizeStaticHash(space, 30.0f, 1000);
|
|
21 cpSpaceResizeActiveHash(space, 30.0f, 1000);
|
|
22 // Give it some gravity
|
|
23 space.gravity = cpv(0, -1);
|
|
24
|
|
25
|
|
26 // Create A ground segment along the bottom of the screen
|
|
27 // By attaching it to &space.staticBody instead of a body, we make it a static shape.
|
|
28 cpShape *ground = cpSegmentShapeNew(&space.staticBody, cpv(-320,-240), cpv(320,-240), 0.0f);
|
|
29 // Set some parameters of the shape.
|
|
30 // For more info: http://code.google.com/p/chipmunk-physics/wiki/cpShape
|
|
31 ground.e = 1.0f; ground.u = 1.0f;
|
|
32 // Add the shape to the space as a static shape
|
|
33 // If a shape never changes position, add it as static so Chipmunk knows it only needs to
|
|
34 // calculate collision information for it once when it is added.
|
|
35 // Do not change the postion of a static shape after adding it.
|
|
36 cpSpaceAddShape(space, ground);
|
|
37
|
|
38 // Add a moving circle object.
|
|
39 cpFloat radius = 15.0f;
|
|
40 cpFloat mass = 10.0f;
|
|
41 // This time we need to give a mass and moment of inertia when creating the circle.
|
|
42 cpBody *ballBody = cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero));
|
|
43 // Set some parameters of the body:
|
|
44 // For more info: http://code.google.com/p/chipmunk-physics/wiki/cpBody
|
|
45 ballBody.p = cpv(0, -100 + radius+50);
|
|
46 ballBody.v = cpv(0, -20);
|
|
47 // Add the body to the space so it will be simulated and move around.
|
|
48 cpSpaceAddBody(space, ballBody);
|
|
49
|
|
50
|
|
51 // Add a circle shape for the ball.
|
|
52 // Shapes are always defined relative to the center of gravity of the body they are attached to.
|
|
53 // When the body moves or rotates, the shape will move with it.
|
|
54 // Additionally, all of the cpSpaceAdd*() functions return the thing they added so you can create and add in one go.
|
|
55 cpShape *ballShape = cpSpaceAddShape(space, cpCircleShapeNew(ballBody, radius, cpvzero));
|
|
56 ballShape.e = 0.0f; ballShape.u = 0.9f;
|
|
57
|
|
58 bool finished=false;
|
|
59 while(!finished)
|
|
60 {
|
|
61 // Chipmunk allows you to use a different timestep each frame, but it works much better when you use a fixed timestep.
|
|
62 // An excellent article on why fixed timesteps for game logic can be found here: http://gafferongames.com/game-physics/fix-your-timestep/
|
|
63 cpSpaceStep(space, 1.0f/60.0f);
|
|
64
|
|
65 // print position
|
|
66 writefln("pos %s,%s",ballBody.p.x,ballBody.p.y);
|
|
67
|
|
68 // stop simulation once the ball touches the ground
|
|
69 finished = (ballBody.p.y < -225);
|
|
70 }
|
|
71
|
|
72 // free our objects
|
|
73 cpSpaceFreeChildren(space);
|
|
74 // free the space
|
|
75 cpSpaceFree(space);
|
|
76 } |