annotate trunk/tests/helloworld.d @ 11:427db6df205c

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