Mercurial > projects > chipmunkd
diff 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 |
line wrap: on
line diff
--- a/trunk/chipmunkd/chipmunk.d Thu Dec 09 22:25:04 2010 +0100 +++ b/trunk/chipmunkd/chipmunk.d Fri Dec 10 02:10:27 2010 +0100 @@ -6,6 +6,7 @@ import std.stdio; import std.string:format; import std.conv:to; +import std.math:PI; void cpMessage(string message, string condition, string file, int line, bool isError) @@ -92,8 +93,17 @@ cpFloat cpMomentForCircle(cpFloat m, cpFloat r1, cpFloat r2, cpVect offset) { - return (1.0f/2.0f)*m*(r1*r1 + r2*r2) + m*cpvdot(offset, offset); + return m*(0.5f*(r1*r1 + r2*r2) + cpvlengthsq(offset)); } + +/** + Calculate area of a hollow circle. +*/ +cpFloat cpAreaForCircle(cpFloat r1, cpFloat r2) +{ + return 2.0f*cast(cpFloat)PI*cpfabs(r1*r1 - r2*r2); +} + /** Calculate the moment of inertia for a line segment. Beveling radius is not supported. @@ -104,7 +114,16 @@ cpFloat length = cpvlength(cpvsub(b, a)); cpVect offset = cpvmult(cpvadd(a, b), 1.0f/2.0f); - return m*length*length/12.0f + m*cpvdot(offset, offset); + return m*(length*length/12.0f + cpvlengthsq(offset)); +} + +/** + Calculate the area of a fattened (capsule shaped) line segment. +*/ +cpFloat +cpAreaForSegment(cpVect a, cpVect b, cpFloat r) +{ + return 2.0f*r*(cast(cpFloat)PI*r + cpvdist(a, b)); } /** @@ -135,6 +154,51 @@ } /** + Calculate the signed area of a polygon. +*/ +cpFloat cpAreaForPoly(const int numVerts, const cpVect *verts) +{ + cpFloat area = 0.0f; + for(int i=0; i<numVerts; i++){ + area += cpvcross(verts[i], verts[(i+1)%numVerts]); + } + + return area/2.0f; +} + +/** + Calculate the natural centroid of a polygon. +*/ +cpVect cpCentroidForPoly(const int numVerts, const cpVect *verts) +{ + cpFloat sum = 0.0f; + cpVect vsum = cpvzero; + + for(int i=0; i<numVerts; i++){ + cpVect v1 = verts[i]; + cpVect v2 = verts[(i+1)%numVerts]; + cpFloat cross = cpvcross(v1, v2); + + sum += cross; + vsum = cpvadd(vsum, cpvmult(cpvadd(v1, v2), cross)); + } + + return cpvmult(vsum, 1.0f/(3.0f*sum)); +} + +/** + Center the polygon on the origin. (Subtracts the centroid of the polygon from each vertex) +*/ +void cpRecenterPoly(const int numVerts, cpVect *verts) +{ + cpVect centroid = cpCentroidForPoly(numVerts, verts); + + for(int i=0; i<numVerts; i++){ + verts[i] = cpvsub(verts[i], centroid); + } +} + +/** Calculate the moment of inertia for a solid box. */ cpFloat