annotate trunk/tests/helloworld.d @ 10:99dd8466b465

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