1 module chipmunk.cpVect; 2 3 import chipmunk.chipmunk_types; 4 5 extern (C): 6 7 const cpVect cpvzero = {0.0f,0.0f}; 8 9 // In the C library these are static inlined functions in the header. 10 // We reimplement them here rather than binding to them. 11 12 /// Convenience constructor for cpVect structs. 13 static cpVect cpv(const cpFloat x, const cpFloat y) 14 { 15 cpVect v = {x, y}; 16 return v; 17 } 18 19 /// Check if two vectors are equal. (Be careful when comparing floating point numbers!) 20 static cpBool cpveql(const cpVect v1, const cpVect v2) 21 { 22 return (v1.x == v2.x && v1.y == v2.y); 23 } 24 25 /// Add two vectors 26 static cpVect cpvadd(const cpVect v1, const cpVect v2) 27 { 28 return cpv(v1.x + v2.x, v1.y + v2.y); 29 } 30 31 /// Subtract two vectors. 32 static cpVect cpvsub(const cpVect v1, const cpVect v2) 33 { 34 return cpv(v1.x - v2.x, v1.y - v2.y); 35 } 36 37 /// Negate a vector. 38 static cpVect cpvneg(const cpVect v) 39 { 40 return cpv(-v.x, -v.y); 41 } 42 43 /// Scalar multiplication. 44 static cpVect cpvmult(const cpVect v, const cpFloat s) 45 { 46 return cpv(v.x*s, v.y*s); 47 } 48 49 /// Vector dot product. 50 static cpFloat cpvdot(const cpVect v1, const cpVect v2) 51 { 52 return v1.x*v2.x + v1.y*v2.y; 53 } 54 55 /// 2D vector cross product analog. 56 /// The cross product of 2D vectors results in a 3D vector with only a z component. 57 /// This function returns the magnitude of the z value. 58 static cpFloat cpvcross(const cpVect v1, const cpVect v2) 59 { 60 return v1.x*v2.y - v1.y*v2.x; 61 } 62 63 /// Returns a perpendicular vector. (90 degree rotation) 64 static cpVect cpvperp(const cpVect v) 65 { 66 return cpv(-v.y, v.x); 67 } 68 69 /// Returns a perpendicular vector. (-90 degree rotation) 70 static cpVect cpvrperp(const cpVect v) 71 { 72 return cpv(v.y, -v.x); 73 } 74 75 /// Returns the vector projection of v1 onto v2. 76 static cpVect cpvproject(const cpVect v1, const cpVect v2) 77 { 78 return cpvmult(v2, cpvdot(v1, v2)/cpvdot(v2, v2)); 79 } 80 81 /// Returns the unit length vector for the given angle (in radians). 82 static cpVect cpvforangle(const cpFloat a) 83 { 84 return cpv(cpfcos(a), cpfsin(a)); 85 } 86 87 /// Returns the angular direction v is pointing in (in radians). 88 static cpFloat cpvtoangle(const cpVect v) 89 { 90 return cpfatan2(v.y, v.x); 91 } 92 93 /// Uses complex number multiplication to rotate v1 by v2. Scaling will occur if v1 is not a unit vector. 94 static cpVect cpvrotate(const cpVect v1, const cpVect v2) 95 { 96 return cpv(v1.x*v2.x - v1.y*v2.y, v1.x*v2.y + v1.y*v2.x); 97 } 98 99 /// Inverse of cpvrotate(). 100 static cpVect cpvunrotate(const cpVect v1, const cpVect v2) 101 { 102 return cpv(v1.x*v2.x + v1.y*v2.y, v1.y*v2.x - v1.x*v2.y); 103 } 104 105 /// Returns the squared length of v. Faster than cpvlength() when you only need to compare lengths. 106 static cpFloat cpvlengthsq(const cpVect v) 107 { 108 return cpvdot(v, v); 109 } 110 111 /// Returns the length of v. 112 static cpFloat cpvlength(const cpVect v) 113 { 114 return cpfsqrt(cpvdot(v, v)); 115 } 116 117 /// Linearly interpolate between v1 and v2. 118 static cpVect cpvlerp(const cpVect v1, const cpVect v2, const cpFloat t) 119 { 120 return cpvadd(cpvmult(v1, 1.0f - t), cpvmult(v2, t)); 121 } 122 123 /// Returns a normalized copy of v. 124 static cpVect cpvnormalize(const cpVect v) 125 { 126 // Neat trick I saw somewhere to avoid div/0. 127 return cpvmult(v, 1.0f/(cpvlength(v) + CPFLOAT_MIN)); 128 } 129 130 /// Spherical linearly interpolate between v1 and v2. 131 static cpVect 132 cpvslerp(const cpVect v1, const cpVect v2, const cpFloat t) 133 { 134 cpFloat dot = cpvdot(cpvnormalize(v1), cpvnormalize(v2)); 135 cpFloat omega = cpfacos(cpfclamp(dot, -1.0f, 1.0f)); 136 137 if(omega < 1e-3){ 138 // If the angle between two vectors is very small, lerp instead to avoid precision issues. 139 return cpvlerp(v1, v2, t); 140 } else { 141 cpFloat denom = 1.0f/cpfsin(omega); 142 return cpvadd(cpvmult(v1, cpfsin((1.0f - t)*omega)*denom), cpvmult(v2, cpfsin(t*omega)*denom)); 143 } 144 } 145 146 /// Spherical linearly interpolate between v1 towards v2 by no more than angle a radians 147 static cpVect 148 cpvslerpconst(const cpVect v1, const cpVect v2, const cpFloat a) 149 { 150 cpFloat dot = cpvdot(cpvnormalize(v1), cpvnormalize(v2)); 151 cpFloat omega = cpfacos(cpfclamp(dot, -1.0f, 1.0f)); 152 153 return cpvslerp(v1, v2, cpfmin(a, omega)/omega); 154 } 155 156 /// Clamp v to length len. 157 static cpVect cpvclamp(const cpVect v, const cpFloat len) 158 { 159 return (cpvdot(v,v) > len*len) ? cpvmult(cpvnormalize(v), len) : v; 160 } 161 162 /// Linearly interpolate between v1 towards v2 by distance d. 163 static cpVect cpvlerpconst(cpVect v1, cpVect v2, cpFloat d) 164 { 165 return cpvadd(v1, cpvclamp(cpvsub(v2, v1), d)); 166 } 167 168 /// Returns the distance between v1 and v2. 169 static cpFloat cpvdist(const cpVect v1, const cpVect v2) 170 { 171 return cpvlength(cpvsub(v1, v2)); 172 } 173 174 /// Returns the squared distance between v1 and v2. Faster than cpvdist() when you only need to compare distances. 175 static cpFloat cpvdistsq(const cpVect v1, const cpVect v2) 176 { 177 return cpvlengthsq(cpvsub(v1, v2)); 178 } 179 180 /// Returns true if the distance between v1 and v2 is less than dist. 181 static cpBool cpvnear(const cpVect v1, const cpVect v2, const cpFloat dist) 182 { 183 return cpvdistsq(v1, v2) < dist*dist; 184 } 185 186 /// @} 187 188 /// @defgroup cpMat2x2 cpMat2x2 189 /// 2x2 matrix type used for tensors and such. 190 /// @{ 191 192 // NUKE 193 static cpMat2x2 194 cpMat2x2New(cpFloat a, cpFloat b, cpFloat c, cpFloat d) 195 { 196 cpMat2x2 m = {a, b, c, d}; 197 return m; 198 } 199 200 static cpVect 201 cpMat2x2Transform(cpMat2x2 m, cpVect v) 202 { 203 return cpv(v.x*m.a + v.y*m.b, v.x*m.c + v.y*m.d); 204 }