1 module chipmunk.chipmunk_types; 2 3 import core.stdc.config; 4 import core.stdc.math; 5 import core.stdc.float_; 6 7 import chipmunk.cpVect; 8 9 extern (C): 10 11 enum CP_NO_GROUP = 0; 12 enum cpBitmask CP_ALL_CATEGORIES = ~0; 13 enum cpCollisionType CP_WILDCARD_COLLISION_TYPE = ~0; 14 15 // TODO: support CP_USE_DOUBLES? Right now I just assume we're using doubles. 16 17 alias cpFloat = double; 18 alias cpfsqrt = sqrt; 19 alias cpfsin = sin; 20 alias cpfcos = cos; 21 alias cpfacos = acos; 22 alias cpfatan2 = atan2; 23 alias cpfmod = fmod; 24 alias cpfexp = exp; 25 alias cpfpow = pow; 26 alias cpffloor = floor; 27 alias cpfceil = ceil; 28 enum CPFLOAT_MIN = DBL_MIN; 29 30 alias c_ulong cpHashValue; 31 alias uint cpCollisionID; 32 alias ubyte cpBool; 33 alias void* cpDataPointer; 34 alias c_ulong cpCollisionType; 35 alias c_ulong cpGroup; 36 alias uint cpBitmask; 37 alias uint cpTimestamp; 38 39 enum cpFalse = 0; 40 enum cpTrue = 1; 41 42 enum INFINITY = cpFloat.infinity; 43 44 enum CP_PI = cast(cpFloat)3.14159265358979323846264338327950288; 45 46 struct cpVect 47 { 48 cpFloat x; 49 cpFloat y; 50 51 cpVect opBinary(string op)(const cpFloat s) const if (op == "*") 52 { 53 return cpvmult(this, s); 54 } 55 56 cpVect opBinary(string op)(const cpVect v2) const if (op == "+") 57 { 58 return cpvadd(this, v2); 59 } 60 61 cpVect opBinary(string op)(const cpVect v2) const if (op == "-") 62 { 63 return cpvsub(this, v2); 64 } 65 66 cpBool opEquals()(const cpVect v2) const if (op == "==") 67 { 68 return cpveql(this, v2); 69 } 70 71 cpVect opUnary(string op)() const if (op == "-") 72 { 73 return cpvneg(this); 74 } 75 } 76 77 struct cpTransform 78 { 79 cpFloat a; 80 cpFloat b; 81 cpFloat c; 82 cpFloat d; 83 cpFloat tx; 84 cpFloat ty; 85 } 86 87 struct cpMat2x2 88 { 89 cpFloat a; 90 cpFloat b; 91 cpFloat c; 92 cpFloat d; 93 } 94 95 /// Return the max of two cpFloats. 96 static cpFloat cpfmax(cpFloat a, cpFloat b) 97 { 98 return (a > b) ? a : b; 99 } 100 101 /// Return the min of two cpFloats. 102 static cpFloat cpfmin(cpFloat a, cpFloat b) 103 { 104 return (a < b) ? a : b; 105 } 106 107 /// Return the absolute value of a cpFloat. 108 static cpFloat cpfabs(cpFloat f) 109 { 110 return (f < 0) ? -f : f; 111 } 112 113 /// Clamp @c f to be between @c min and @c max. 114 static cpFloat cpfclamp(cpFloat f, cpFloat min, cpFloat max) 115 { 116 return cpfmin(cpfmax(f, min), max); 117 } 118 119 /// Clamp @c f to be between 0 and 1. 120 static cpFloat cpfclamp01(cpFloat f) 121 { 122 return cpfmax(0.0f, cpfmin(f, 1.0f)); 123 } 124 125 /// Linearly interpolate (or extrapolate) between @c f1 and @c f2 by @c t percent. 126 static cpFloat cpflerp(cpFloat f1, cpFloat f2, cpFloat t) 127 { 128 return f1*(1.0f - t) + f2*t; 129 } 130 131 /// Linearly interpolate from @c f1 to @c f2 by no more than @c d. 132 static cpFloat cpflerpconst(cpFloat f1, cpFloat f2, cpFloat d) 133 { 134 return f1 + cpfclamp(f2 - f1, -d, d); 135 }