Mercurial > projects > chipmunkd
comparison trunk/chipmunkd/chipmunk.d @ 23:4ceef5833c8c
updated to chipmunk 5.3.3
author | Extrawurst |
---|---|
date | Fri, 10 Dec 2010 02:10:27 +0100 |
parents | df4ebc8add66 |
children |
comparison
equal
deleted
inserted
replaced
22:ed2c81f3d1df | 23:4ceef5833c8c |
---|---|
4 module chipmunkd.chipmunk; | 4 module chipmunkd.chipmunk; |
5 | 5 |
6 import std.stdio; | 6 import std.stdio; |
7 import std.string:format; | 7 import std.string:format; |
8 import std.conv:to; | 8 import std.conv:to; |
9 import std.math:PI; | |
9 | 10 |
10 void | 11 void |
11 cpMessage(string message, string condition, string file, int line, bool isError) | 12 cpMessage(string message, string condition, string file, int line, bool isError) |
12 { | 13 { |
13 stderr.writeln(.format("%s:%s", isError ? "Aborting due to Chipmunk error" : "Chipmunk warning", message)); | 14 stderr.writeln(.format("%s:%s", isError ? "Aborting due to Chipmunk error" : "Chipmunk warning", message)); |
90 r1 and r2 are the inner and outer diameters. A solid circle has an inner diameter of 0. | 91 r1 and r2 are the inner and outer diameters. A solid circle has an inner diameter of 0. |
91 */ | 92 */ |
92 cpFloat | 93 cpFloat |
93 cpMomentForCircle(cpFloat m, cpFloat r1, cpFloat r2, cpVect offset) | 94 cpMomentForCircle(cpFloat m, cpFloat r1, cpFloat r2, cpVect offset) |
94 { | 95 { |
95 return (1.0f/2.0f)*m*(r1*r1 + r2*r2) + m*cpvdot(offset, offset); | 96 return m*(0.5f*(r1*r1 + r2*r2) + cpvlengthsq(offset)); |
96 } | 97 } |
98 | |
99 /** | |
100 Calculate area of a hollow circle. | |
101 */ | |
102 cpFloat cpAreaForCircle(cpFloat r1, cpFloat r2) | |
103 { | |
104 return 2.0f*cast(cpFloat)PI*cpfabs(r1*r1 - r2*r2); | |
105 } | |
106 | |
97 /** | 107 /** |
98 Calculate the moment of inertia for a line segment. | 108 Calculate the moment of inertia for a line segment. |
99 Beveling radius is not supported. | 109 Beveling radius is not supported. |
100 */ | 110 */ |
101 cpFloat | 111 cpFloat |
102 cpMomentForSegment(cpFloat m, cpVect a, cpVect b) | 112 cpMomentForSegment(cpFloat m, cpVect a, cpVect b) |
103 { | 113 { |
104 cpFloat length = cpvlength(cpvsub(b, a)); | 114 cpFloat length = cpvlength(cpvsub(b, a)); |
105 cpVect offset = cpvmult(cpvadd(a, b), 1.0f/2.0f); | 115 cpVect offset = cpvmult(cpvadd(a, b), 1.0f/2.0f); |
106 | 116 |
107 return m*length*length/12.0f + m*cpvdot(offset, offset); | 117 return m*(length*length/12.0f + cpvlengthsq(offset)); |
118 } | |
119 | |
120 /** | |
121 Calculate the area of a fattened (capsule shaped) line segment. | |
122 */ | |
123 cpFloat | |
124 cpAreaForSegment(cpVect a, cpVect b, cpFloat r) | |
125 { | |
126 return 2.0f*r*(cast(cpFloat)PI*r + cpvdist(a, b)); | |
108 } | 127 } |
109 | 128 |
110 /** | 129 /** |
111 Calculate the moment of inertia for a solid polygon shape. | 130 Calculate the moment of inertia for a solid polygon shape. |
112 */ | 131 */ |
133 cpfree(tVerts); | 152 cpfree(tVerts); |
134 return (m*sum1)/(6.0f*sum2); | 153 return (m*sum1)/(6.0f*sum2); |
135 } | 154 } |
136 | 155 |
137 /** | 156 /** |
157 Calculate the signed area of a polygon. | |
158 */ | |
159 cpFloat cpAreaForPoly(const int numVerts, const cpVect *verts) | |
160 { | |
161 cpFloat area = 0.0f; | |
162 for(int i=0; i<numVerts; i++){ | |
163 area += cpvcross(verts[i], verts[(i+1)%numVerts]); | |
164 } | |
165 | |
166 return area/2.0f; | |
167 } | |
168 | |
169 /** | |
170 Calculate the natural centroid of a polygon. | |
171 */ | |
172 cpVect cpCentroidForPoly(const int numVerts, const cpVect *verts) | |
173 { | |
174 cpFloat sum = 0.0f; | |
175 cpVect vsum = cpvzero; | |
176 | |
177 for(int i=0; i<numVerts; i++){ | |
178 cpVect v1 = verts[i]; | |
179 cpVect v2 = verts[(i+1)%numVerts]; | |
180 cpFloat cross = cpvcross(v1, v2); | |
181 | |
182 sum += cross; | |
183 vsum = cpvadd(vsum, cpvmult(cpvadd(v1, v2), cross)); | |
184 } | |
185 | |
186 return cpvmult(vsum, 1.0f/(3.0f*sum)); | |
187 } | |
188 | |
189 /** | |
190 Center the polygon on the origin. (Subtracts the centroid of the polygon from each vertex) | |
191 */ | |
192 void cpRecenterPoly(const int numVerts, cpVect *verts) | |
193 { | |
194 cpVect centroid = cpCentroidForPoly(numVerts, verts); | |
195 | |
196 for(int i=0; i<numVerts; i++){ | |
197 verts[i] = cpvsub(verts[i], centroid); | |
198 } | |
199 } | |
200 | |
201 /** | |
138 Calculate the moment of inertia for a solid box. | 202 Calculate the moment of inertia for a solid box. |
139 */ | 203 */ |
140 cpFloat | 204 cpFloat |
141 cpMomentForBox(cpFloat m, cpFloat width, cpFloat height) | 205 cpMomentForBox(cpFloat m, cpFloat width, cpFloat height) |
142 { | 206 { |