Munk2D Documentation  8.x.x
Loading...
Searching...
No Matches
cpTransform.h
1/* Copyright (c) 2025 Victor Blomqvist
2 * Copyright (c) 2007-2024 Scott Lembcke and Howling Moon Software
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21*/
22
23#ifndef CHIPMUNK_TRANSFORM_H
24#define CHIPMUNK_TRANSFORM_H
25
26#include "chipmunk_types.h"
27#include "cpVect.h"
28#include "cpBB.h"
29
31static const cpTransform cpTransformIdentity = {1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f};
32
37static inline cpTransform
38cpTransformNew(cpFloat a, cpFloat b, cpFloat c, cpFloat d, cpFloat tx, cpFloat ty)
39{
40 cpTransform t = {a, b, c, d, tx, ty};
41 return t;
42}
43
45static inline cpTransform
46cpTransformNewTranspose(cpFloat a, cpFloat c, cpFloat tx, cpFloat b, cpFloat d, cpFloat ty)
47{
48 cpTransform t = {a, b, c, d, tx, ty};
49 return t;
50}
51
53static inline cpTransform
54cpTransformInverse(cpTransform t)
55{
56 cpFloat inv_det = 1.0/(t.a*t.d - t.c*t.b);
57 return cpTransformNewTranspose(
58 t.d*inv_det, -t.c*inv_det, (t.c*t.ty - t.tx*t.d)*inv_det,
59 -t.b*inv_det, t.a*inv_det, (t.tx*t.b - t.a*t.ty)*inv_det
60 );
61}
62
64static inline cpTransform
65cpTransformMult(cpTransform t1, cpTransform t2)
66{
67 return cpTransformNewTranspose(
68 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,
69 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
70 );
71}
72
74static inline cpVect
75cpTransformPoint(cpTransform t, cpVect p)
76{
77 return cpv(t.a*p.x + t.c*p.y + t.tx, t.b*p.x + t.d*p.y + t.ty);
78}
79
81static inline cpVect
82cpTransformVect(cpTransform t, cpVect v)
83{
84 return cpv(t.a*v.x + t.c*v.y, t.b*v.x + t.d*v.y);
85}
86
88static inline cpBB
89cpTransformbBB(cpTransform t, cpBB bb)
90{
91 cpVect center = cpBBCenter(bb);
92 cpFloat hw = (bb.r - bb.l)*0.5;
93 cpFloat hh = (bb.t - bb.b)*0.5;
94
95 cpFloat a = t.a*hw, b = t.c*hh, d = t.b*hw, e = t.d*hh;
96 cpFloat hw_max = cpfmax(cpfabs(a + b), cpfabs(a - b));
97 cpFloat hh_max = cpfmax(cpfabs(d + e), cpfabs(d - e));
98 return cpBBNewForExtents(cpTransformPoint(t, center), hw_max, hh_max);
99}
100
102static inline cpTransform
103cpTransformTranslate(cpVect translate)
104{
105 return cpTransformNewTranspose(
106 1.0, 0.0, translate.x,
107 0.0, 1.0, translate.y
108 );
109}
110
112static inline cpTransform
113cpTransformScale(cpFloat scaleX, cpFloat scaleY)
114{
115 return cpTransformNewTranspose(
116 scaleX, 0.0, 0.0,
117 0.0, scaleY, 0.0
118 );
119}
120
122static inline cpTransform
123cpTransformRotate(cpFloat radians)
124{
125 cpVect rot = cpvforangle(radians);
126 return cpTransformNewTranspose(
127 rot.x, -rot.y, 0.0,
128 rot.y, rot.x, 0.0
129 );
130}
131
133static inline cpTransform
134cpTransformRigid(cpVect translate, cpFloat radians)
135{
136 cpVect rot = cpvforangle(radians);
137 return cpTransformNewTranspose(
138 rot.x, -rot.y, translate.x,
139 rot.y, rot.x, translate.y
140 );
141}
142
144static inline cpTransform
145cpTransformRigidInverse(cpTransform t)
146{
147 return cpTransformNewTranspose(
148 t.d, -t.c, (t.c*t.ty - t.tx*t.d),
149 -t.b, t.a, (t.tx*t.b - t.a*t.ty)
150 );
151}
152
153//MARK: Miscellaneous (but useful) transformation matrices.
154// See source for documentation...
155
156static inline cpTransform
157cpTransformWrap(cpTransform outer, cpTransform inner)
158{
159 return cpTransformMult(cpTransformInverse(outer), cpTransformMult(inner, outer));
160}
161
162static inline cpTransform
163cpTransformWrapInverse(cpTransform outer, cpTransform inner)
164{
165 return cpTransformMult(outer, cpTransformMult(inner, cpTransformInverse(outer)));
166}
167
168static inline cpTransform
169cpTransformOrtho(cpBB bb)
170{
171 return cpTransformNewTranspose(
172 2.0/(bb.r - bb.l), 0.0, -(bb.r + bb.l)/(bb.r - bb.l),
173 0.0, 2.0/(bb.t - bb.b), -(bb.t + bb.b)/(bb.t - bb.b)
174 );
175}
176
177static inline cpTransform
178cpTransformBoneScale(cpVect v0, cpVect v1)
179{
180 cpVect d = cpvsub(v1, v0);
181 return cpTransformNewTranspose(
182 d.x, -d.y, v0.x,
183 d.y, d.x, v0.y
184 );
185}
186
187static inline cpTransform
188cpTransformAxialScale(cpVect axis, cpVect pivot, cpFloat scale)
189{
190 cpFloat A = axis.x*axis.y*(scale - 1.0);
191 cpFloat B = cpvdot(axis, pivot)*(1.0 - scale);
192
193 return cpTransformNewTranspose(
194 scale*axis.x*axis.x + axis.y*axis.y, A, axis.x*B,
195 A, axis.x*axis.x + scale*axis.y*axis.y, axis.y*B
196 );
197}
198
199#endif
static cpFloat cpfabs(cpFloat f)
Return the absolute value of a cpFloat.
Definition chipmunk_types.h:132
double cpFloat
Chipmunk's floating point type.
Definition chipmunk_types.h:68
static cpFloat cpfmax(cpFloat a, cpFloat b)
Return the max of two cpFloats.
Definition chipmunk_types.h:120
static cpBB cpBBNewForExtents(const cpVect c, const cpFloat hw, const cpFloat hh)
Constructs a cpBB centered on a point with the given extents (half sizes).
Definition cpBB.h:47
static cpVect cpBBCenter(cpBB bb)
Returns the center of a bounding box.
Definition cpBB.h:98
static cpFloat cpvdot(const cpVect v1, const cpVect v2)
Vector dot product.
Definition cpVect.h:73
static cpVect cpv(const cpFloat x, const cpFloat y)
Convenience constructor for cpVect structs.
Definition cpVect.h:36
static cpVect cpvforangle(const cpFloat a)
Returns the unit length vector for the given angle (in radians).
Definition cpVect.h:105
static cpVect cpvsub(const cpVect v1, const cpVect v2)
Subtract two vectors.
Definition cpVect.h:55
Chipmunk's axis-aligned 2D bounding box type. (left, bottom, right, top)
Definition cpBB.h:34
Column major affine transform.
Definition chipmunk_types.h:258
Definition chipmunk_types.h:251