Chipmunk2D Pro API Reference  7.0.1
 All Classes Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
cpTransform.h
1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
2  *
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:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
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
19  * SOFTWARE.
20  */
21 
22 #ifndef CHIPMUNK_TRANSFORM_H
23 #define CHIPMUNK_TRANSFORM_H
24 
25 #include "chipmunk_types.h"
26 #include "cpVect.h"
27 #include "cpBB.h"
28 
30 static const cpTransform cpTransformIdentity = {1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f};
31 
36 static inline cpTransform
37 cpTransformNew(cpFloat a, cpFloat b, cpFloat c, cpFloat d, cpFloat tx, cpFloat ty)
38 {
39  cpTransform t = {a, b, c, d, tx, ty};
40  return t;
41 }
42 
44 static inline cpTransform
45 cpTransformNewTranspose(cpFloat a, cpFloat c, cpFloat tx, cpFloat b, cpFloat d, cpFloat ty)
46 {
47  cpTransform t = {a, b, c, d, tx, ty};
48  return t;
49 }
50 
52 static inline cpTransform
53 cpTransformInverse(cpTransform t)
54 {
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
59  );
60 }
61 
63 static inline cpTransform
64 cpTransformMult(cpTransform t1, cpTransform t2)
65 {
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
69  );
70 }
71 
73 static inline cpVect
74 cpTransformPoint(cpTransform t, cpVect p)
75 {
76  return cpv(t.a*p.x + t.c*p.y + t.tx, t.b*p.x + t.d*p.y + t.ty);
77 }
78 
80 static inline cpVect
81 cpTransformVect(cpTransform t, cpVect v)
82 {
83  return cpv(t.a*v.x + t.c*v.y, t.b*v.x + t.d*v.y);
84 }
85 
87 static inline cpBB
88 cpTransformbBB(cpTransform t, cpBB bb)
89 {
90  cpVect center = cpBBCenter(bb);
91  cpFloat hw = (bb.r - bb.l)*0.5;
92  cpFloat hh = (bb.t - bb.b)*0.5;
93 
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);
98 }
99 
101 static inline cpTransform
102 cpTransformTranslate(cpVect translate)
103 {
104  return cpTransformNewTranspose(
105  1.0, 0.0, translate.x,
106  0.0, 1.0, translate.y
107  );
108 }
109 
111 static inline cpTransform
112 cpTransformScale(cpFloat scaleX, cpFloat scaleY)
113 {
114  return cpTransformNewTranspose(
115  scaleX, 0.0, 0.0,
116  0.0, scaleY, 0.0
117  );
118 }
119 
121 static inline cpTransform
122 cpTransformRotate(cpFloat radians)
123 {
124  cpVect rot = cpvforangle(radians);
125  return cpTransformNewTranspose(
126  rot.x, -rot.y, 0.0,
127  rot.y, rot.x, 0.0
128  );
129 }
130 
132 static inline cpTransform
133 cpTransformRigid(cpVect translate, cpFloat radians)
134 {
135  cpVect rot = cpvforangle(radians);
136  return cpTransformNewTranspose(
137  rot.x, -rot.y, translate.x,
138  rot.y, rot.x, translate.y
139  );
140 }
141 
143 static inline cpTransform
144 cpTransformRigidInverse(cpTransform t)
145 {
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)
149  );
150 }
151 
152 //MARK: Miscellaneous (but useful) transformation matrices.
153 // See source for documentation...
154 
155 static inline cpTransform
156 cpTransformWrap(cpTransform outer, cpTransform inner)
157 {
158  return cpTransformMult(cpTransformInverse(outer), cpTransformMult(inner, outer));
159 }
160 
161 static inline cpTransform
162 cpTransformWrapInverse(cpTransform outer, cpTransform inner)
163 {
164  return cpTransformMult(outer, cpTransformMult(inner, cpTransformInverse(outer)));
165 }
166 
167 static inline cpTransform
168 cpTransformOrtho(cpBB bb)
169 {
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)
173  );
174 }
175 
176 static inline cpTransform
177 cpTransformBoneScale(cpVect v0, cpVect v1)
178 {
179  cpVect d = cpvsub(v1, v0);
180  return cpTransformNewTranspose(
181  d.x, -d.y, v0.x,
182  d.y, d.x, v0.y
183  );
184 }
185 
186 static inline cpTransform
187 cpTransformAxialScale(cpVect axis, cpVect pivot, cpFloat scale)
188 {
189  cpFloat A = axis.x*axis.y*(scale - 1.0);
190  cpFloat B = cpvdot(axis, pivot)*(1.0 - scale);
191 
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
195  );
196 }
197 
198 #endif