# HG changeset patch # User Extrawurst # Date 1291420289 -3600 # Node ID df4ebc8add6612fa58c4df3c16dee98ce9432369 # Parent d88862c82f066d0196d79c456ed0f5ff94262ba5 rename/refactoring diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/chipmunk.d --- a/trunk/chipmunkd/chipmunk.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/chipmunk.d Sat Dec 04 00:51:29 2010 +0100 @@ -41,7 +41,7 @@ //#endif // -import chipmunkd.chipmunk_types_h; +import chipmunkd.chipmunk_types; import core.stdc.stdlib; enum INFINITY = cpFloat.infinity; @@ -54,7 +54,7 @@ alias core.stdc.stdlib.realloc cprealloc; alias core.stdc.stdlib.free cpfree; -public import chipmunkd.cpVect_h,chipmunkd.cpVect; +public import chipmunkd.cpVect; public import chipmunkd.cpBB; public import chipmunkd.cpArray; public import chipmunkd.cpHashSet; @@ -74,7 +74,7 @@ public import chipmunkd.cpSpaceQuery; public import chipmunkd.cpSpaceStep; -public import chipmunkd.chipmunk_types_h; +public import chipmunkd.chipmunk_types; enum cpHashValue CP_HASH_COEF = cast(cpHashValue)(3344921057uL); // ulong to uint ?? static cpHashValue CP_HASH_PAIR(T)(T A, T B) {return (cast(cpHashValue)(A)*CP_HASH_COEF ^ cast(cpHashValue)(B)*CP_HASH_COEF);} diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/chipmunk_types.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trunk/chipmunkd/chipmunk_types.d Sat Dec 04 00:51:29 2010 +0100 @@ -0,0 +1,159 @@ + +// written in the D programming language + +module chipmunkd.chipmunk_types; + +//#ifdef __APPLE__ +// #import "TargetConditionals.h" +//#endif +// +//#ifndef CP_USE_DOUBLES +// // Use single precision floats on the iPhone. +// #if TARGET_OS_IPHONE +// #define CP_USE_DOUBLES 0 +// #else +// // use doubles by default for higher precision +// #define CP_USE_DOUBLES 1 +// #endif +//#endif +// +version(CP_USE_DOUBLES){ + alias double cpFloat; + //TODO: +// #define cpfsqrt sqrt +// #define cpfsin sin +// #define cpfcos cos +// #define cpfacos acos +// #define cpfatan2 atan2 +// #define cpfmod fmod +// #define cpfexp exp +// #define cpfpow pow +// #define cpffloor floor +// #define cpfceil ceil +}else{ + extern (C) nothrow { + float sqrtf(float); + float sinf(float); + float cosf(float); + float acosf(float); + float atan2f(float,float); + float fmodf(float,float); + float expf(float); + float powf(float,float); + float floorf(float); + float ceilf(float); + } + + alias float cpFloat; + + alias sqrtf cpfsqrt; + alias sinf cpfsin; + alias cosf cpfcos; + alias acosf cpfacos; + alias atan2f cpfatan2; + alias fmodf cpfmod; + alias expf cpfexp; + alias powf cpfpow; + alias floorf cpffloor; + alias ceilf cpfceil; +} + +static cpFloat +cpfmax(cpFloat a, cpFloat b) +{ + return (a > b) ? a : b; +} + +static cpFloat +cpfmin(cpFloat a, cpFloat b) +{ + return (a < b) ? a : b; +} + +static cpFloat +cpfabs(cpFloat n) +{ + return (n < 0) ? -n : n; +} + +static cpFloat +cpfclamp(cpFloat f, cpFloat min, cpFloat max) +{ + return cpfmin(cpfmax(f, min), max); +} + +static cpFloat +cpflerp(cpFloat f1, cpFloat f2, cpFloat t) +{ + return f1*(1.0f - t) + f2*t; +} + +static cpFloat +cpflerpconst(cpFloat f1, cpFloat f2, cpFloat d) +{ + return f1 + cpfclamp(f2 - f1, -d, d); +} + +//#if TARGET_OS_IPHONE +// // CGPoints are structurally the same, and allow +// // easy interoperability with other iPhone libraries +// #import +// typedef CGPoint cpVect; +//#else + struct cpVect{cpFloat x = 0; cpFloat y=0;} +//#endif + +alias uint cpHashValue; + +// Oh C, how we love to define our own boolean types to get compiler compatibility +//#ifdef CP_BOOL_TYPE +// typedef CP_BOOL_TYPE cpBool; +//#else + alias bool cpBool; +//#endif + +//#ifndef cpTrue + enum cpTrue = true; +//#endif + +//#ifndef cpFalse + enum cpFalse = false; +//#endif + +//#ifdef CP_DATA_POINTER_TYPE +// typedef CP_DATA_POINTER_TYPE cpDataPointer; +//#else + alias void * cpDataPointer; +//#endif + +//#ifdef CP_COLLISION_TYPE_TYPE +// typedef CP_COLLISION_TYPE_TYPE cpCollisionType; +//#else + alias uint cpCollisionType; +//#endif + +//#ifdef CP_GROUP_TYPE +// typedef CP_GROUP_TYPE cpGroup; +//#else + alias uint cpGroup; +//#endif + +//#ifdef CP_LAYERS_TYPE +// typedef CP_GROUP_TYPE cpLayers; +//#else + alias uint cpLayers; +//#endif + +//#ifdef CP_TIMESTAMP_TYPE +// typedef CP_TIMESTAMP_TYPE cpTimestamp; +//#else + alias uint cpTimestamp; +//#endif + +//#ifndef CP_NO_GROUP + enum CP_NO_GROUP = 0; +//#endif + +//#ifndef CP_ALL_LAYERS + enum CP_ALL_LAYERS = ~0; +//#endif diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/chipmunk_types_h.d --- a/trunk/chipmunkd/chipmunk_types_h.d Fri Dec 03 23:55:20 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,159 +0,0 @@ - -// written in the D programming language - -module chipmunkd.chipmunk_types_h; - -//#ifdef __APPLE__ -// #import "TargetConditionals.h" -//#endif -// -//#ifndef CP_USE_DOUBLES -// // Use single precision floats on the iPhone. -// #if TARGET_OS_IPHONE -// #define CP_USE_DOUBLES 0 -// #else -// // use doubles by default for higher precision -// #define CP_USE_DOUBLES 1 -// #endif -//#endif -// -version(CP_USE_DOUBLES){ - alias double cpFloat; - //TODO: -// #define cpfsqrt sqrt -// #define cpfsin sin -// #define cpfcos cos -// #define cpfacos acos -// #define cpfatan2 atan2 -// #define cpfmod fmod -// #define cpfexp exp -// #define cpfpow pow -// #define cpffloor floor -// #define cpfceil ceil -}else{ - extern (C) nothrow { - float sqrtf(float); - float sinf(float); - float cosf(float); - float acosf(float); - float atan2f(float,float); - float fmodf(float,float); - float expf(float); - float powf(float,float); - float floorf(float); - float ceilf(float); - } - - alias float cpFloat; - - alias sqrtf cpfsqrt; - alias sinf cpfsin; - alias cosf cpfcos; - alias acosf cpfacos; - alias atan2f cpfatan2; - alias fmodf cpfmod; - alias expf cpfexp; - alias powf cpfpow; - alias floorf cpffloor; - alias ceilf cpfceil; -} - -static cpFloat -cpfmax(cpFloat a, cpFloat b) -{ - return (a > b) ? a : b; -} - -static cpFloat -cpfmin(cpFloat a, cpFloat b) -{ - return (a < b) ? a : b; -} - -static cpFloat -cpfabs(cpFloat n) -{ - return (n < 0) ? -n : n; -} - -static cpFloat -cpfclamp(cpFloat f, cpFloat min, cpFloat max) -{ - return cpfmin(cpfmax(f, min), max); -} - -static cpFloat -cpflerp(cpFloat f1, cpFloat f2, cpFloat t) -{ - return f1*(1.0f - t) + f2*t; -} - -static cpFloat -cpflerpconst(cpFloat f1, cpFloat f2, cpFloat d) -{ - return f1 + cpfclamp(f2 - f1, -d, d); -} - -//#if TARGET_OS_IPHONE -// // CGPoints are structurally the same, and allow -// // easy interoperability with other iPhone libraries -// #import -// typedef CGPoint cpVect; -//#else - struct cpVect{cpFloat x = 0; cpFloat y=0;} -//#endif - -alias uint cpHashValue; - -// Oh C, how we love to define our own boolean types to get compiler compatibility -//#ifdef CP_BOOL_TYPE -// typedef CP_BOOL_TYPE cpBool; -//#else - alias bool cpBool; -//#endif - -//#ifndef cpTrue - enum cpTrue = true; -//#endif - -//#ifndef cpFalse - enum cpFalse = false; -//#endif - -//#ifdef CP_DATA_POINTER_TYPE -// typedef CP_DATA_POINTER_TYPE cpDataPointer; -//#else - alias void * cpDataPointer; -//#endif - -//#ifdef CP_COLLISION_TYPE_TYPE -// typedef CP_COLLISION_TYPE_TYPE cpCollisionType; -//#else - alias uint cpCollisionType; -//#endif - -//#ifdef CP_GROUP_TYPE -// typedef CP_GROUP_TYPE cpGroup; -//#else - alias uint cpGroup; -//#endif - -//#ifdef CP_LAYERS_TYPE -// typedef CP_GROUP_TYPE cpLayers; -//#else - alias uint cpLayers; -//#endif - -//#ifdef CP_TIMESTAMP_TYPE -// typedef CP_TIMESTAMP_TYPE cpTimestamp; -//#else - alias uint cpTimestamp; -//#endif - -//#ifndef CP_NO_GROUP - enum CP_NO_GROUP = 0; -//#endif - -//#ifndef CP_ALL_LAYERS - enum CP_ALL_LAYERS = ~0; -//#endif diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/constraints/cpConstraint.d --- a/trunk/chipmunkd/constraints/cpConstraint.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/constraints/cpConstraint.d Sat Dec 04 00:51:29 2010 +0100 @@ -4,9 +4,6 @@ module chipmunkd.constraints.cpConstraint; import chipmunkd.chipmunk; -import chipmunkd.chipmunk_types_h; -import chipmunkd.cpVect,chipmunkd.cpVect_h; -import chipmunkd.cpBody; alias void function(cpConstraint *constraint, cpFloat dt, cpFloat dt_inv) cpConstraintPreStepFunction; alias void function(cpConstraint *constraint) cpConstraintApplyImpulseFunction; diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/constraints/cpPinJoint.d --- a/trunk/chipmunkd/constraints/cpPinJoint.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/constraints/cpPinJoint.d Sat Dec 04 00:51:29 2010 +0100 @@ -4,9 +4,6 @@ module chipmunkd.constraints.cpPinJoint; import chipmunkd.chipmunk; -import chipmunkd.chipmunk_types_h; -import chipmunkd.cpVect,chipmunkd.cpVect_h; -import chipmunkd.cpBody; import chipmunkd.constraints.util; //const cpConstraintClass *cpPinJointGetClass(); diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/constraints/util.d --- a/trunk/chipmunkd/constraints/util.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/constraints/util.d Sat Dec 04 00:51:29 2010 +0100 @@ -4,8 +4,8 @@ module chipmunkd.constraints.util; import chipmunkd.cpBody; -import chipmunkd.chipmunk_types_h; -import chipmunkd.cpVect,chipmunkd.cpVect_h; +import chipmunkd.chipmunk_types; +import chipmunkd.cpVect; template CP_DefineClassGetter(string t) { diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/cpArbiter.d --- a/trunk/chipmunkd/cpArbiter.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/cpArbiter.d Sat Dec 04 00:51:29 2010 +0100 @@ -3,12 +3,7 @@ module chipmunkd.cpArbiter; -import chipmunkd.cpSpace; -import chipmunkd.cpBody; -import chipmunkd.cpShape; import chipmunkd.chipmunk; -import chipmunkd.chipmunk_types_h; -import chipmunkd.cpVect,chipmunkd.cpVect_h; import chipmunkd.constraints.util; // Determines how fast penetrations resolve themselves. diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/cpArray.d --- a/trunk/chipmunkd/cpArray.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/cpArray.d Sat Dec 04 00:51:29 2010 +0100 @@ -4,7 +4,7 @@ module chipmunkd.cpArray; import chipmunkd.chipmunk; -import chipmunkd.chipmunk_types_h; +import chipmunkd.chipmunk_types; import core.stdc.string:memcpy; diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/cpBB.d --- a/trunk/chipmunkd/cpBB.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/cpBB.d Sat Dec 04 00:51:29 2010 +0100 @@ -3,8 +3,8 @@ module chipmunkd.cpBB; -import chipmunkd.cpVect_h; -import chipmunkd.chipmunk_types_h; +import chipmunkd.cpVect; +import chipmunkd.chipmunk_types; struct cpBB{ cpFloat l, b, r ,t; diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/cpBody.d --- a/trunk/chipmunkd/cpBody.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/cpBody.d Sat Dec 04 00:51:29 2010 +0100 @@ -4,9 +4,6 @@ module chipmunkd.cpBody; import chipmunkd.chipmunk; -import chipmunkd.chipmunk_types_h; -import chipmunkd.cpVect,chipmunkd.cpVect_h; -import chipmunkd.cpShape; alias void function(cpBody *_body, cpVect gravity, cpFloat damping, cpFloat dt)cpBodyVelocityFunc; alias void function(cpBody *_body, cpFloat dt)cpBodyPositionFunc; diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/cpCollision.d --- a/trunk/chipmunkd/cpCollision.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/cpCollision.d Sat Dec 04 00:51:29 2010 +0100 @@ -3,7 +3,7 @@ module chipmunkd.cpCollision; -import chipmunkd.chipmunk_types_h; +import chipmunkd.chipmunk_types; import chipmunkd.chipmunk; import chipmunkd.cpShape; diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/cpHashSet.d --- a/trunk/chipmunkd/cpHashSet.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/cpHashSet.d Sat Dec 04 00:51:29 2010 +0100 @@ -4,7 +4,7 @@ module chipmunkd.cpHashSet; import chipmunkd.chipmunk; -import chipmunkd.chipmunk_types_h; +import chipmunkd.chipmunk_types; import chipmunkd.cpArray; import chipmunkd.prime; diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/cpPolyShape.d --- a/trunk/chipmunkd/cpPolyShape.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/cpPolyShape.d Sat Dec 04 00:51:29 2010 +0100 @@ -3,12 +3,7 @@ module chipmunkd.cpPolyShape; -import chipmunkd.cpShape; -import chipmunkd.cpBody; import chipmunkd.chipmunk; -import chipmunkd.chipmunk_types_h; -import chipmunkd.cpVect_h; -import chipmunkd.cpBB; // Axis structure used by cpPolyShape. struct cpPolyShapeAxis{ diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/cpShape.d --- a/trunk/chipmunkd/cpShape.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/cpShape.d Sat Dec 04 00:51:29 2010 +0100 @@ -3,11 +3,7 @@ module chipmunkd.cpShape; -import chipmunkd.cpBody; import chipmunkd.chipmunk; -import chipmunkd.chipmunk_types_h; -import chipmunkd.cpVect_h; -import chipmunkd.cpBB; import std.stdio; diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/cpSpace.d --- a/trunk/chipmunkd/cpSpace.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/cpSpace.d Sat Dec 04 00:51:29 2010 +0100 @@ -4,18 +4,6 @@ module chipmunkd.cpSpace; import chipmunkd.chipmunk; -import chipmunkd.chipmunk_types_h; -import chipmunkd.cpVect,chipmunkd.cpVect_h; -import chipmunkd.cpHashSet; -import chipmunkd.cpCollision; -import chipmunkd.cpBody; -import chipmunkd.cpArray; -import chipmunkd.cpShape; -import chipmunkd.cpBB; -import chipmunkd.cpArbiter; -import chipmunkd.cpSpaceHash; -import chipmunkd.constraints.cpConstraint; -import chipmunkd.cpSpaceQuery; // Number of frames that contact information should persist. //extern cpTimestamp cp_contact_persistence; diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/cpSpaceComponent.d --- a/trunk/chipmunkd/cpSpaceComponent.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/cpSpaceComponent.d Sat Dec 04 00:51:29 2010 +0100 @@ -4,9 +4,6 @@ module chipmunkd.cpSpaceComponent; import chipmunkd.chipmunk; -import chipmunkd.chipmunk_types_h; -import chipmunkd.cpVect,chipmunkd.cpVect_h; -import chipmunkd.cpBody; // Chipmunk uses a data structure called a disjoint set forest. // My attempts to find a way to splice circularly linked lists in diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/cpSpaceHash.d --- a/trunk/chipmunkd/cpSpaceHash.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/cpSpaceHash.d Sat Dec 04 00:51:29 2010 +0100 @@ -4,11 +4,6 @@ module chipmunkd.cpSpaceHash; import chipmunkd.chipmunk; -import chipmunkd.chipmunk_types_h; -import chipmunkd.cpVect,chipmunkd.cpVect_h; -import chipmunkd.cpBB; -import chipmunkd.cpHashSet; -import chipmunkd.cpArray; import chipmunkd.prime; // The spatial hash is Chipmunk's default (and currently only) spatial index type. diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/cpSpaceQuery.d --- a/trunk/chipmunkd/cpSpaceQuery.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/cpSpaceQuery.d Sat Dec 04 00:51:29 2010 +0100 @@ -4,7 +4,7 @@ module chipmunkd.cpSpaceQuery; import chipmunkd.chipmunk; -import chipmunkd.chipmunk_types_h; +import chipmunkd.chipmunk_types; struct pointQueryContext { cpLayers layers; diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/cpVect.d --- a/trunk/chipmunkd/cpVect.d Fri Dec 03 23:55:20 2010 +0100 +++ b/trunk/chipmunkd/cpVect.d Sat Dec 04 00:51:29 2010 +0100 @@ -3,8 +3,7 @@ module chipmunkd.cpVect; -import chipmunkd.cpVect_h; -import chipmunkd.chipmunk_types_h; +import chipmunkd.chipmunk_types; cpFloat cpvlength(const cpVect v) @@ -50,4 +49,191 @@ cpvstr(const cpVect v) { return .format("(%s,%s)",v.x, v.y); +} + +/// Constant for the zero vector. +static const cpVect cpvzero = {0.0f,0.0f}; + +/// Convenience constructor for cpVect structs. +static cpVect +cpv(const cpFloat x, const cpFloat y) +{ + cpVect v = {x, y}; + return v; +} + +// non-inlined functions + +///// Returns the length of v. +//cpFloat cpvlength(const cpVect v); +// +///// Spherical linearly interpolate between v1 and v2. +//cpVect cpvslerp(const cpVect v1, const cpVect v2, const cpFloat t); +// +///// Spherical linearly interpolate between v1 towards v2 by no more than angle a radians +//cpVect cpvslerpconst(const cpVect v1, const cpVect v2, const cpFloat a); +// +///// Returns the unit length vector for the given angle (in radians). +//cpVect cpvforangle(const cpFloat a); +// +///// Returns the angular direction v is pointing in (in radians). +//cpFloat cpvtoangle(const cpVect v); + +/** + Returns a string representation of v. Intended mostly for debugging purposes and not production use. + + @attention The string points to a static local and is reset every time the function is called. + If you want to print more than one vector you will have to split up your printing onto separate lines. +*/ +//char *cpvstr(const cpVect v); + +/// Check if two vectors are equal. (Be careful when comparing floating point numbers!) +static cpBool +cpveql(const cpVect v1, const cpVect v2) +{ + return (v1.x == v2.x && v1.y == v2.y); +} + +/// Add two vectors +static cpVect +cpvadd(const cpVect v1, const cpVect v2) +{ + return cpv(v1.x + v2.x, v1.y + v2.y); +} + +/// Negate a vector. +static cpVect +cpvneg(const cpVect v) +{ + return cpv(-v.x, -v.y); +} + +/// Subtract two vectors. +static cpVect +cpvsub(const cpVect v1, const cpVect v2) +{ + return cpv(v1.x - v2.x, v1.y - v2.y); +} + +/// Scalar multiplication. +static cpVect +cpvmult(const cpVect v, const cpFloat s) +{ + return cpv(v.x*s, v.y*s); +} + +/// Vector dot product. +static cpFloat +cpvdot(const cpVect v1, const cpVect v2) +{ + return v1.x*v2.x + v1.y*v2.y; +} + +/** + 2D vector cross product analog. + The cross product of 2D vectors results in a 3D vector with only a z component. + This function returns the magnitude of the z value. +*/ +static cpFloat +cpvcross(const cpVect v1, const cpVect v2) +{ + return v1.x*v2.y - v1.y*v2.x; +} + +/// Returns a perpendicular vector. (90 degree rotation) +static cpVect +cpvperp(const cpVect v) +{ + return cpv(-v.y, v.x); +} + +/// Returns a perpendicular vector. (-90 degree rotation) +static cpVect +cpvrperp(const cpVect v) +{ + return cpv(v.y, -v.x); +} + +/// Returns the vector projection of v1 onto v2. +static cpVect +cpvproject(const cpVect v1, const cpVect v2) +{ + return cpvmult(v2, cpvdot(v1, v2)/cpvdot(v2, v2)); +} + +/// Uses complex number multiplication to rotate v1 by v2. Scaling will occur if v1 is not a unit vector. +static cpVect +cpvrotate(const cpVect v1, const cpVect v2) +{ + return cpv(v1.x*v2.x - v1.y*v2.y, v1.x*v2.y + v1.y*v2.x); +} + +/// Inverse of cpvrotate(). +static cpVect +cpvunrotate(const cpVect v1, const cpVect v2) +{ + return cpv(v1.x*v2.x + v1.y*v2.y, v1.y*v2.x - v1.x*v2.y); +} + +/// Returns the squared length of v. Faster than cpvlength() when you only need to compare lengths. +static cpFloat +cpvlengthsq(const cpVect v) +{ + return cpvdot(v, v); +} + +/// Linearly interpolate between v1 and v2. +static cpVect +cpvlerp(const cpVect v1, const cpVect v2, const cpFloat t) +{ + return cpvadd(cpvmult(v1, 1.0f - t), cpvmult(v2, t)); +} + +/// Returns a normalized copy of v. +static cpVect +cpvnormalize(const cpVect v) +{ + return cpvmult(v, 1.0f/cpvlength(v)); +} + +/// Returns a normalized copy of v or cpvzero if v was already cpvzero. Protects against divide by zero errors. +static cpVect +cpvnormalize_safe(const cpVect v) +{ + return (v.x == 0.0f && v.y == 0.0f ? cpvzero : cpvnormalize(v)); +} + +/// Clamp v to length len. +static cpVect +cpvclamp(const cpVect v, const cpFloat len) +{ + return (cpvdot(v,v) > len*len) ? cpvmult(cpvnormalize(v), len) : v; +} + +/// Linearly interpolate between v1 towards v2 by distance d. +static cpVect +cpvlerpconst(cpVect v1, cpVect v2, cpFloat d) +{ + return cpvadd(v1, cpvclamp(cpvsub(v2, v1), d)); +} + +/// Returns the distance between v1 and v2. +static cpFloat +cpvdist(const cpVect v1, const cpVect v2) +{ + return cpvlength(cpvsub(v1, v2)); +} + +/// Returns the squared distance between v1 and v2. Faster than cpvdist() when you only need to compare distances. +static cpFloat +cpvdistsq(const cpVect v1, const cpVect v2) +{ + return cpvlengthsq(cpvsub(v1, v2)); +} + +/// Returns true if the distance between v1 and v2 is less than dist. +static cpBool +cpvnear(const cpVect v1, const cpVect v2, const cpFloat dist) +{ + return cpvdistsq(v1, v2) < dist*dist; } \ No newline at end of file diff -r d88862c82f06 -r df4ebc8add66 trunk/chipmunkd/cpVect_h.d --- a/trunk/chipmunkd/cpVect_h.d Fri Dec 03 23:55:20 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,194 +0,0 @@ - -// written in the D programming language - -module chipmunkd.cpVect_h; - -import chipmunkd.cpVect; -import chipmunkd.chipmunk_types_h; - -/// Constant for the zero vector. -static const cpVect cpvzero = {0.0f,0.0f}; - -/// Convenience constructor for cpVect structs. -static cpVect -cpv(const cpFloat x, const cpFloat y) -{ - cpVect v = {x, y}; - return v; -} - -// non-inlined functions - -///// Returns the length of v. -//cpFloat cpvlength(const cpVect v); -// -///// Spherical linearly interpolate between v1 and v2. -//cpVect cpvslerp(const cpVect v1, const cpVect v2, const cpFloat t); -// -///// Spherical linearly interpolate between v1 towards v2 by no more than angle a radians -//cpVect cpvslerpconst(const cpVect v1, const cpVect v2, const cpFloat a); -// -///// Returns the unit length vector for the given angle (in radians). -//cpVect cpvforangle(const cpFloat a); -// -///// Returns the angular direction v is pointing in (in radians). -//cpFloat cpvtoangle(const cpVect v); - -/** - Returns a string representation of v. Intended mostly for debugging purposes and not production use. - - @attention The string points to a static local and is reset every time the function is called. - If you want to print more than one vector you will have to split up your printing onto separate lines. -*/ -//char *cpvstr(const cpVect v); - -/// Check if two vectors are equal. (Be careful when comparing floating point numbers!) -static cpBool -cpveql(const cpVect v1, const cpVect v2) -{ - return (v1.x == v2.x && v1.y == v2.y); -} - -/// Add two vectors -static cpVect -cpvadd(const cpVect v1, const cpVect v2) -{ - return cpv(v1.x + v2.x, v1.y + v2.y); -} - -/// Negate a vector. -static cpVect -cpvneg(const cpVect v) -{ - return cpv(-v.x, -v.y); -} - -/// Subtract two vectors. -static cpVect -cpvsub(const cpVect v1, const cpVect v2) -{ - return cpv(v1.x - v2.x, v1.y - v2.y); -} - -/// Scalar multiplication. -static cpVect -cpvmult(const cpVect v, const cpFloat s) -{ - return cpv(v.x*s, v.y*s); -} - -/// Vector dot product. -static cpFloat -cpvdot(const cpVect v1, const cpVect v2) -{ - return v1.x*v2.x + v1.y*v2.y; -} - -/** - 2D vector cross product analog. - The cross product of 2D vectors results in a 3D vector with only a z component. - This function returns the magnitude of the z value. -*/ -static cpFloat -cpvcross(const cpVect v1, const cpVect v2) -{ - return v1.x*v2.y - v1.y*v2.x; -} - -/// Returns a perpendicular vector. (90 degree rotation) -static cpVect -cpvperp(const cpVect v) -{ - return cpv(-v.y, v.x); -} - -/// Returns a perpendicular vector. (-90 degree rotation) -static cpVect -cpvrperp(const cpVect v) -{ - return cpv(v.y, -v.x); -} - -/// Returns the vector projection of v1 onto v2. -static cpVect -cpvproject(const cpVect v1, const cpVect v2) -{ - return cpvmult(v2, cpvdot(v1, v2)/cpvdot(v2, v2)); -} - -/// Uses complex number multiplication to rotate v1 by v2. Scaling will occur if v1 is not a unit vector. -static cpVect -cpvrotate(const cpVect v1, const cpVect v2) -{ - return cpv(v1.x*v2.x - v1.y*v2.y, v1.x*v2.y + v1.y*v2.x); -} - -/// Inverse of cpvrotate(). -static cpVect -cpvunrotate(const cpVect v1, const cpVect v2) -{ - return cpv(v1.x*v2.x + v1.y*v2.y, v1.y*v2.x - v1.x*v2.y); -} - -/// Returns the squared length of v. Faster than cpvlength() when you only need to compare lengths. -static cpFloat -cpvlengthsq(const cpVect v) -{ - return cpvdot(v, v); -} - -/// Linearly interpolate between v1 and v2. -static cpVect -cpvlerp(const cpVect v1, const cpVect v2, const cpFloat t) -{ - return cpvadd(cpvmult(v1, 1.0f - t), cpvmult(v2, t)); -} - -/// Returns a normalized copy of v. -static cpVect -cpvnormalize(const cpVect v) -{ - return cpvmult(v, 1.0f/cpvlength(v)); -} - -/// Returns a normalized copy of v or cpvzero if v was already cpvzero. Protects against divide by zero errors. -static cpVect -cpvnormalize_safe(const cpVect v) -{ - return (v.x == 0.0f && v.y == 0.0f ? cpvzero : cpvnormalize(v)); -} - -/// Clamp v to length len. -static cpVect -cpvclamp(const cpVect v, const cpFloat len) -{ - return (cpvdot(v,v) > len*len) ? cpvmult(cpvnormalize(v), len) : v; -} - -/// Linearly interpolate between v1 towards v2 by distance d. -static cpVect -cpvlerpconst(cpVect v1, cpVect v2, cpFloat d) -{ - return cpvadd(v1, cpvclamp(cpvsub(v2, v1), d)); -} - -/// Returns the distance between v1 and v2. -static cpFloat -cpvdist(const cpVect v1, const cpVect v2) -{ - return cpvlength(cpvsub(v1, v2)); -} - -/// Returns the squared distance between v1 and v2. Faster than cpvdist() when you only need to compare distances. -static cpFloat -cpvdistsq(const cpVect v1, const cpVect v2) -{ - return cpvlengthsq(cpvsub(v1, v2)); -} - -/// Returns true if the distance between v1 and v2 is less than dist. -static cpBool -cpvnear(const cpVect v1, const cpVect v2, const cpFloat dist) -{ - return cpvdistsq(v1, v2) < dist*dist; -} \ No newline at end of file