4
|
1
|
|
2 // written in the D programming language
|
|
3
|
|
4 module chipmunkd.chipmunk;
|
13
|
5
|
|
6 import std.stdio;
|
|
7 import std.string:format;
|
|
8 import std.conv:to;
|
|
9
|
|
10 void
|
|
11 cpMessage(string message, string condition, string file, int line, bool isError)
|
|
12 {
|
|
13 stderr.writeln(.format("%s:%s", isError ? "Aborting due to Chipmunk error" : "Chipmunk warning", message));
|
|
14 stderr.writeln(.format("\tFailed condition: %s", condition));
|
|
15 stderr.writeln(.format("\tSource: %s(%s)", file, line));
|
|
16
|
|
17 if(isError) {debug{assert(false);}else{asm{int 3;}}}
|
|
18 }
|
4
|
19
|
|
20 debug
|
|
21 {
|
13
|
22 template cpAssertWarn(string _condition, string message,string _f = __FILE__,int _l=__LINE__)
|
|
23 {
|
|
24 enum cpAssertWarn = "if(!("~_condition~"))"~
|
|
25 "cpMessage(\""~message~"\",\""~_condition~"\",r\""~_f~"\","~to!string(_l)~",false);";
|
|
26 }
|
4
|
27 }
|
|
28 else
|
|
29 {
|
13
|
30 template cpAssertWarn(string _condition, string message,string _f = __FILE__,int _l=__LINE__)
|
|
31 {
|
|
32 enum cpAssertWarn = "";
|
|
33 }
|
4
|
34 }
|
|
35
|
13
|
36 //TODO
|
4
|
37 //#ifdef NDEBUG
|
|
38 // #define cpAssert(condition, message)
|
|
39 //#else
|
|
40 // #define cpAssert(condition, message) if(!(condition)) cpMessage(message, #condition, __FILE__, __LINE__, 1)
|
|
41 //#endif
|
|
42 //
|
13
|
43
|
4
|
44 import chipmunkd.chipmunk_types_h;
|
|
45 import core.stdc.stdlib;
|
|
46
|
|
47 enum INFINITY = cpFloat.infinity;
|
|
48
|
|
49 // Maximum allocated size for various Chipmunk buffer sizes
|
|
50 enum CP_BUFFER_BYTES = (32*1024);
|
|
51
|
|
52 alias core.stdc.stdlib.malloc cpmalloc;
|
|
53 alias core.stdc.stdlib.calloc cpcalloc;
|
|
54 alias core.stdc.stdlib.realloc cprealloc;
|
|
55 alias core.stdc.stdlib.free cpfree;
|
|
56
|
|
57 public import chipmunkd.cpVect_h,chipmunkd.cpVect;
|
|
58 public import chipmunkd.cpBB;
|
|
59 public import chipmunkd.cpArray;
|
|
60 public import chipmunkd.cpHashSet;
|
|
61 public import chipmunkd.cpSpaceHash;
|
|
62 //
|
|
63 public import chipmunkd.cpBody;
|
|
64 public import chipmunkd.cpShape;
|
|
65 public import chipmunkd.cpPolyShape;
|
|
66 //
|
|
67 public import chipmunkd.cpArbiter;
|
|
68 public import chipmunkd.cpCollision;
|
|
69 //
|
|
70 public import chipmunkd.constraints.cpConstraint;
|
|
71 //
|
|
72 public import chipmunkd.cpSpace;
|
|
73 public import chipmunkd.cpSpaceComponent;
|
|
74 public import chipmunkd.cpSpaceQuery;
|
|
75 public import chipmunkd.cpSpaceStep;
|
|
76
|
|
77 public import chipmunkd.chipmunk_types_h;
|
|
78
|
|
79 enum cpHashValue CP_HASH_COEF = cast(cpHashValue)(3344921057uL); // ulong to uint ??
|
|
80 static cpHashValue CP_HASH_PAIR(T)(T A, T B) {return (cast(cpHashValue)(A)*CP_HASH_COEF ^ cast(cpHashValue)(B)*CP_HASH_COEF);}
|
|
81
|
|
82 extern const char *cpVersionString;
|
|
83 void cpInitChipmunk()
|
|
84 {
|
|
85 cpInitCollisionFuncs();
|
|
86 }
|
|
87
|
|
88 /**
|
|
89 Calculate the moment of inertia for a circle.
|
|
90 r1 and r2 are the inner and outer diameters. A solid circle has an inner diameter of 0.
|
|
91 */
|
|
92 cpFloat
|
|
93 cpMomentForCircle(cpFloat m, cpFloat r1, cpFloat r2, cpVect offset)
|
|
94 {
|
|
95 return (1.0f/2.0f)*m*(r1*r1 + r2*r2) + m*cpvdot(offset, offset);
|
|
96 }
|
|
97 /**
|
|
98 Calculate the moment of inertia for a line segment.
|
|
99 Beveling radius is not supported.
|
|
100 */
|
|
101 cpFloat
|
|
102 cpMomentForSegment(cpFloat m, cpVect a, cpVect b)
|
|
103 {
|
|
104 cpFloat length = cpvlength(cpvsub(b, a));
|
|
105 cpVect offset = cpvmult(cpvadd(a, b), 1.0f/2.0f);
|
|
106
|
|
107 return m*length*length/12.0f + m*cpvdot(offset, offset);
|
|
108 }
|
|
109
|
|
110 /**
|
|
111 Calculate the moment of inertia for a solid polygon shape.
|
|
112 */
|
|
113 cpFloat
|
|
114 cpMomentForPoly(cpFloat m, const int numVerts, cpVect *verts, cpVect offset)
|
|
115 {
|
|
116 cpVect *tVerts = cast(cpVect *)cpcalloc(numVerts, cpVect.sizeof);
|
|
117 for(int i=0; i<numVerts; i++)
|
|
118 tVerts[i] = cpvadd(verts[i], offset);
|
|
119
|
|
120 cpFloat sum1 = 0.0f;
|
|
121 cpFloat sum2 = 0.0f;
|
|
122 for(int i=0; i<numVerts; i++){
|
|
123 cpVect v1 = tVerts[i];
|
|
124 cpVect v2 = tVerts[(i+1)%numVerts];
|
|
125
|
|
126 cpFloat a = cpvcross(v2, v1);
|
|
127 cpFloat b = cpvdot(v1, v1) + cpvdot(v1, v2) + cpvdot(v2, v2);
|
|
128
|
|
129 sum1 += a*b;
|
|
130 sum2 += a;
|
|
131 }
|
|
132
|
|
133 cpfree(tVerts);
|
|
134 return (m*sum1)/(6.0f*sum2);
|
|
135 }
|
|
136
|
|
137 /**
|
|
138 Calculate the moment of inertia for a solid box.
|
|
139 */
|
|
140 cpFloat
|
|
141 cpMomentForBox(cpFloat m, cpFloat width, cpFloat height)
|
|
142 {
|
|
143 return m*(width*width + height*height)/12.0f;
|
|
144 }
|