comparison trunk/tests/simple.d @ 12:8697699b2c5a

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