comparison trunk/tests/ChipmunkDemos/samples/UnsafeOps.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.UnsafeOps;
5
6 import chipmunkd.chipmunk;
7
8 import samples.ChipmunkDemo;
9
10 import gameApp;
11
12 import std.math;
13
14 static cpSpace *space;
15
16 enum M_PI = PI;
17 enum M_PI_2 = PI*0.5f;
18
19 enum NUM_CIRCLES = 30;
20
21 static cpShape *circles[NUM_CIRCLES];
22 static cpFloat circleRadius = 30.0f;
23
24 static void
25 update(int ticks)
26 {
27 if(arrowDirection.y){
28 circleRadius = cpfmax(10.0f, circleRadius + arrowDirection.y);
29
30 for(int i=0; i<NUM_CIRCLES; i++){
31 circles[i]._body.m = cpMomentForCircle(1.0f, 0.0f, circleRadius, cpvzero);
32 cpCircleShapeSetRadius(circles[i], circleRadius);
33 }
34 }
35
36 int steps = 1;
37 cpFloat dt = 1.0f/60.0f/cast(cpFloat)steps;
38
39 for(int i=0; i<steps; i++){
40 cpSpaceStep(space, dt);
41 }
42 }
43
44 static cpSpace *
45 init()
46 {
47 cpResetShapeIdCounter();
48
49 space = cpSpaceNew();
50 space.iterations = 5;
51 space.gravity = cpv(0, -100);
52
53 cpSpaceResizeStaticHash(space, 40.0f, 999);
54 cpSpaceResizeActiveHash(space, 30.0f, 2999);
55
56 cpBody *_body, staticBody = &space.staticBody;
57 cpShape *shape;
58
59 shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
60 shape.e = 1.0f; shape.u = 1.0f;
61 shape.layers = NOT_GRABABLE_MASK;
62
63 shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
64 shape.e = 1.0f; shape.u = 1.0f;
65 shape.layers = NOT_GRABABLE_MASK;
66
67 shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
68 shape.e = 1.0f; shape.u = 1.0f;
69 shape.layers = NOT_GRABABLE_MASK;
70
71 for(int i=0; i<NUM_CIRCLES; i++){
72 _body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForCircle(1.0f, 0.0f, circleRadius, cpvzero)));
73 _body.p = cpvmult(cpv(frand()*2.0f - 1.0f, frand()*2.0f - 1.0f), circleRadius*5.0f);
74
75 circles[i] = shape = cpSpaceAddShape(space, cpCircleShapeNew(_body, circleRadius, cpvzero));
76 shape.e = 0.0f; shape.u = 1.0f;
77 }
78
79 //strcat(messageString,
80 // "chipmunk_unsafe.h Contains functions for changing shapes, but they can cause severe stability problems if used incorrectly.\n"
81 // "Shape changes occur as instantaneous changes to position without an accompanying velocity change. USE WITH CAUTION!");
82 return space;
83 }
84
85 static void
86 destroy()
87 {
88 cpSpaceFreeChildren(space);
89 cpSpaceFree(space);
90 }
91
92 chipmunkDemo UnsafeOps = {
93 "Unsafe Operations",
94 null,
95 &init,
96 &update,
97 &destroy,
98 };