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