Mercurial > projects > chipmunkd
comparison trunk/tests/ChipmunkDemos/samples/Simple.d @ 16:af2f61a96318
ported chipmunk demos
author | Extrawurst |
---|---|
date | Sat, 04 Dec 2010 02:02:29 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
15:df4ebc8add66 | 16:af2f61a96318 |
---|---|
1 | |
2 // written in the D programming language | |
3 | |
4 module samples.Simple; | |
5 | |
6 import chipmunkd.chipmunk; | |
7 | |
8 import samples.ChipmunkDemo; | |
9 | |
10 static cpSpace *space; | |
11 | |
12 // Init is called by the demo code to set up the demo. | |
13 static cpSpace * | |
14 init() | |
15 { | |
16 // Create a space, a space is a simulation world. It simulates the motions of rigid bodies, | |
17 // handles collisions between them, and simulates the joints between them. | |
18 space = cpSpaceNew(); | |
19 | |
20 // Lets set some parameters of the space: | |
21 // More iterations make the simulation more accurate but slower | |
22 space.iterations = 10; | |
23 // These parameters tune the efficiency of the collision detection. | |
24 // For more info: http://code.google.com/p/chipmunk-physics/wiki/cpSpace | |
25 cpSpaceResizeStaticHash(space, 30.0f, 1000); | |
26 cpSpaceResizeActiveHash(space, 30.0f, 1000); | |
27 // Give it some gravity | |
28 space.gravity = cpv(0, -100); | |
29 | |
30 // Create A ground segment along the bottom of the screen | |
31 // By attaching it to &space.staticBody instead of a body, we make it a static shape. | |
32 cpShape *ground = cpSegmentShapeNew(&space.staticBody, cpv(-320,-240), cpv(320,-240), 0.0f); | |
33 // Set some parameters of the shape. | |
34 // For more info: http://code.google.com/p/chipmunk-physics/wiki/cpShape | |
35 ground.e = 1.0f; ground.u = 1.0f; | |
36 ground.layers = NOT_GRABABLE_MASK; // Used by the Demo mouse grabbing code | |
37 // Add the shape to the space as a static shape | |
38 // If a shape never changes position, add it as static so Chipmunk knows it only needs to | |
39 // calculate collision information for it once when it is added. | |
40 // Do not change the postion of a static shape after adding it. | |
41 cpSpaceAddShape(space, ground); | |
42 | |
43 // Add a moving circle object. | |
44 cpFloat radius = 15.0f; | |
45 cpFloat mass = 10.0f; | |
46 // This time we need to give a mass and moment of inertia when creating the circle. | |
47 cpBody *ballBody = cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero)); | |
48 // Set some parameters of the body: | |
49 // For more info: http://code.google.com/p/chipmunk-physics/wiki/cpBody | |
50 ballBody.p = cpv(0, -100 + radius+50); | |
51 ballBody.v = cpv(0, -20); | |
52 // Add the body to the space so it will be simulated and move around. | |
53 cpSpaceAddBody(space, ballBody); | |
54 | |
55 | |
56 // Add a circle shape for the ball. | |
57 // Shapes are always defined relative to the center of gravity of the body they are attached to. | |
58 // When the body moves or rotates, the shape will move with it. | |
59 // Additionally, all of the cpSpaceAdd*() functions return the thing they added so you can create and add in one go. | |
60 cpShape *ballShape = cpSpaceAddShape(space, cpCircleShapeNew(ballBody, radius, cpvzero)); | |
61 ballShape.e = 0.0f; ballShape.u = 0.9f; | |
62 | |
63 return space; | |
64 } | |
65 | |
66 // Update is called by the demo code each frame. | |
67 static void | |
68 update(int ticks) | |
69 { | |
70 // Chipmunk allows you to use a different timestep each frame, but it works much better when you use a fixed timestep. | |
71 // An excellent article on why fixed timesteps for game logic can be found here: http://gafferongames.com/game-physics/fix-your-timestep/ | |
72 cpSpaceStep(space, 1.0f/60.0f); | |
73 } | |
74 | |
75 // destroy is called by the demo code to free all the memory we've allocated | |
76 static void | |
77 destroy() | |
78 { | |
79 cpSpaceFreeChildren(space); | |
80 cpSpaceFree(space); | |
81 } | |
82 | |
83 chipmunkDemo Simple = { | |
84 "Simple", | |
85 null, | |
86 &init, | |
87 &update, | |
88 &destroy, | |
89 }; |