1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 #ifndef CHIPMUNK_TRANSFORM_H
23 #define CHIPMUNK_TRANSFORM_H
25 #include "chipmunk_types.h"
29 /// Identity transform matrix.
30 static const cpTransform cpTransformIdentity = {1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f};
32 /// Construct a new transform matrix.
33 /// (a, b) is the x basis vector.
34 /// (c, d) is the y basis vector.
35 /// (tx, ty) is the translation.
36 static inline cpTransform
37 cpTransformNew(cpFloat a, cpFloat b, cpFloat c, cpFloat d, cpFloat tx, cpFloat ty)
39 cpTransform t = {a, b, c, d, tx, ty};
43 /// Construct a new transform matrix in transposed order.
44 static inline cpTransform
45 cpTransformNewTranspose(cpFloat a, cpFloat c, cpFloat tx, cpFloat b, cpFloat d, cpFloat ty)
47 cpTransform t = {a, b, c, d, tx, ty};
51 /// Get the inverse of a transform matrix.
52 static inline cpTransform
53 cpTransformInverse(cpTransform t)
55 cpFloat inv_det = 1.0/(t.a*t.d - t.c*t.b);
56 return cpTransformNewTranspose(
57 t.d*inv_det, -t.c*inv_det, (t.c*t.ty - t.tx*t.d)*inv_det,
58 -t.b*inv_det, t.a*inv_det, (t.tx*t.b - t.a*t.ty)*inv_det
62 /// Multiply two transformation matrices.
63 static inline cpTransform
64 cpTransformMult(cpTransform t1, cpTransform t2)
66 return cpTransformNewTranspose(
67 t1.a*t2.a + t1.c*t2.b, t1.a*t2.c + t1.c*t2.d, t1.a*t2.tx + t1.c*t2.ty + t1.tx,
68 t1.b*t2.a + t1.d*t2.b, t1.b*t2.c + t1.d*t2.d, t1.b*t2.tx + t1.d*t2.ty + t1.ty
72 /// Transform an absolute point. (i.e. a vertex)
74 cpTransformPoint(cpTransform t, cpVect p)
76 return cpv(t.a*p.x + t.c*p.y + t.tx, t.b*p.x + t.d*p.y + t.ty);
79 /// Transform a vector (i.e. a normal)
81 cpTransformVect(cpTransform t, cpVect v)
83 return cpv(t.a*v.x + t.c*v.y, t.b*v.x + t.d*v.y);
88 cpTransformbBB(cpTransform t, cpBB bb)
90 cpVect center = cpBBCenter(bb);
91 cpFloat hw = (bb.r - bb.l)*0.5;
92 cpFloat hh = (bb.t - bb.b)*0.5;
94 cpFloat a = t.a*hw, b = t.c*hh, d = t.b*hw, e = t.d*hh;
95 cpFloat hw_max = cpfmax(cpfabs(a + b), cpfabs(a - b));
96 cpFloat hh_max = cpfmax(cpfabs(d + e), cpfabs(d - e));
97 return cpBBNewForExtents(cpTransformPoint(t, center), hw_max, hh_max);
100 /// Create a transation matrix.
101 static inline cpTransform
102 cpTransformTranslate(cpVect translate)
104 return cpTransformNewTranspose(
105 1.0, 0.0, translate.x,
106 0.0, 1.0, translate.y
110 /// Create a scale matrix.
111 static inline cpTransform
112 cpTransformScale(cpFloat scaleX, cpFloat scaleY)
114 return cpTransformNewTranspose(
120 /// Create a rotation matrix.
121 static inline cpTransform
122 cpTransformRotate(cpFloat radians)
124 cpVect rot = cpvforangle(radians);
125 return cpTransformNewTranspose(
131 /// Create a rigid transformation matrix. (transation + rotation)
132 static inline cpTransform
133 cpTransformRigid(cpVect translate, cpFloat radians)
135 cpVect rot = cpvforangle(radians);
136 return cpTransformNewTranspose(
137 rot.x, -rot.y, translate.x,
138 rot.y, rot.x, translate.y
142 /// Fast inverse of a rigid transformation matrix.
143 static inline cpTransform
144 cpTransformRigidInverse(cpTransform t)
146 return cpTransformNewTranspose(
147 t.d, -t.c, (t.c*t.ty - t.tx*t.d),
148 -t.b, t.a, (t.tx*t.b - t.a*t.ty)
152 //MARK: Miscellaneous (but useful) transformation matrices.
153 // See source for documentation...
155 static inline cpTransform
156 cpTransformWrap(cpTransform outer, cpTransform inner)
158 return cpTransformMult(cpTransformInverse(outer), cpTransformMult(inner, outer));
161 static inline cpTransform
162 cpTransformWrapInverse(cpTransform outer, cpTransform inner)
164 return cpTransformMult(outer, cpTransformMult(inner, cpTransformInverse(outer)));
167 static inline cpTransform
168 cpTransformOrtho(cpBB bb)
170 return cpTransformNewTranspose(
171 2.0/(bb.r - bb.l), 0.0, -(bb.r + bb.l)/(bb.r - bb.l),
172 0.0, 2.0/(bb.t - bb.b), -(bb.t + bb.b)/(bb.t - bb.b)
176 static inline cpTransform
177 cpTransformBoneScale(cpVect v0, cpVect v1)
179 cpVect d = cpvsub(v1, v0);
180 return cpTransformNewTranspose(
186 static inline cpTransform
187 cpTransformAxialScale(cpVect axis, cpVect pivot, cpFloat scale)
189 cpFloat A = axis.x*axis.y*(scale - 1.0);
190 cpFloat B = cpvdot(axis, pivot)*(1.0 - scale);
192 return cpTransformNewTranspose(
193 scale*axis.x*axis.x + axis.y*axis.y, A, axis.x*B,
194 A, axis.x*axis.x + scale*axis.y*axis.y, axis.y*B