Munk2D Documentation  8.x.x
Loading...
Searching...
No Matches
cpVect.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_VECT_H
24#define CHIPMUNK_VECT_H
25
26#include "chipmunk_types.h"
27
31
33static const cpVect cpvzero = {0.0f,0.0f};
34
36static inline cpVect cpv(const cpFloat x, const cpFloat y)
37{
38 cpVect v = {x, y};
39 return v;
40}
41
43static inline cpBool cpveql(const cpVect v1, const cpVect v2)
44{
45 return (v1.x == v2.x && v1.y == v2.y);
46}
47
49static inline cpVect cpvadd(const cpVect v1, const cpVect v2)
50{
51 return cpv(v1.x + v2.x, v1.y + v2.y);
52}
53
55static inline cpVect cpvsub(const cpVect v1, const cpVect v2)
56{
57 return cpv(v1.x - v2.x, v1.y - v2.y);
58}
59
61static inline cpVect cpvneg(const cpVect v)
62{
63 return cpv(-v.x, -v.y);
64}
65
67static inline cpVect cpvmult(const cpVect v, const cpFloat s)
68{
69 return cpv(v.x*s, v.y*s);
70}
71
73static inline cpFloat cpvdot(const cpVect v1, const cpVect v2)
74{
75 return v1.x*v2.x + v1.y*v2.y;
76}
77
81static inline cpFloat cpvcross(const cpVect v1, const cpVect v2)
82{
83 return v1.x*v2.y - v1.y*v2.x;
84}
85
87static inline cpVect cpvperp(const cpVect v)
88{
89 return cpv(-v.y, v.x);
90}
91
93static inline cpVect cpvrperp(const cpVect v)
94{
95 return cpv(v.y, -v.x);
96}
97
99static inline cpVect cpvproject(const cpVect v1, const cpVect v2)
100{
101 return cpvmult(v2, cpvdot(v1, v2)/cpvdot(v2, v2));
102}
103
105static inline cpVect cpvforangle(const cpFloat a)
106{
107 return cpv(cpfcos(a), cpfsin(a));
108}
109
111static inline cpFloat cpvtoangle(const cpVect v)
112{
113 return cpfatan2(v.y, v.x);
114}
115
117static inline cpVect cpvrotate(const cpVect v1, const cpVect v2)
118{
119 return cpv(v1.x*v2.x - v1.y*v2.y, v1.x*v2.y + v1.y*v2.x);
120}
121
123static inline cpVect cpvunrotate(const cpVect v1, const cpVect v2)
124{
125 return cpv(v1.x*v2.x + v1.y*v2.y, v1.y*v2.x - v1.x*v2.y);
126}
127
129static inline cpFloat cpvlengthsq(const cpVect v)
130{
131 return cpvdot(v, v);
132}
133
135static inline cpFloat cpvlength(const cpVect v)
136{
137 return cpfsqrt(cpvdot(v, v));
138}
139
141static inline cpVect cpvlerp(const cpVect v1, const cpVect v2, const cpFloat t)
142{
143 return cpvadd(cpvmult(v1, 1.0f - t), cpvmult(v2, t));
144}
145
147static inline cpVect cpvnormalize(const cpVect v)
148{
149 // Neat trick I saw somewhere to avoid div/0.
150 return cpvmult(v, 1.0f/(cpvlength(v) + CPFLOAT_MIN));
151}
152
154static inline cpVect
155cpvslerp(const cpVect v1, const cpVect v2, const cpFloat t)
156{
157 cpFloat dot = cpvdot(cpvnormalize(v1), cpvnormalize(v2));
158 cpFloat omega = cpfacos(cpfclamp(dot, -1.0f, 1.0f));
159
160 if(omega < 1e-3){
161 // If the angle between two vectors is very small, lerp instead to avoid precision issues.
162 return cpvlerp(v1, v2, t);
163 } else {
164 cpFloat denom = 1.0f/cpfsin(omega);
165 return cpvadd(cpvmult(v1, cpfsin((1.0f - t)*omega)*denom), cpvmult(v2, cpfsin(t*omega)*denom));
166 }
167}
168
170static inline cpVect
171cpvslerpconst(const cpVect v1, const cpVect v2, const cpFloat a)
172{
173 cpFloat dot = cpvdot(cpvnormalize(v1), cpvnormalize(v2));
174 cpFloat omega = cpfacos(cpfclamp(dot, -1.0f, 1.0f));
175
176 return cpvslerp(v1, v2, cpfmin(a, omega)/omega);
177}
178
180static inline cpVect cpvclamp(const cpVect v, const cpFloat len)
181{
182 return (cpvdot(v,v) > len*len) ? cpvmult(cpvnormalize(v), len) : v;
183}
184
186static inline cpVect cpvlerpconst(cpVect v1, cpVect v2, cpFloat d)
187{
188 return cpvadd(v1, cpvclamp(cpvsub(v2, v1), d));
189}
190
192static inline cpFloat cpvdist(const cpVect v1, const cpVect v2)
193{
194 return cpvlength(cpvsub(v1, v2));
195}
196
198static inline cpFloat cpvdistsq(const cpVect v1, const cpVect v2)
199{
200 return cpvlengthsq(cpvsub(v1, v2));
201}
202
204static inline cpBool cpvnear(const cpVect v1, const cpVect v2, const cpFloat dist)
205{
206 return cpvdistsq(v1, v2) < dist*dist;
207}
208
210
214
215// NUKE
216static inline cpMat2x2
217cpMat2x2New(cpFloat a, cpFloat b, cpFloat c, cpFloat d)
218{
219 cpMat2x2 m = {a, b, c, d};
220 return m;
221}
222
223static inline cpVect
224cpMat2x2Transform(cpMat2x2 m, cpVect v)
225{
226 return cpv(v.x*m.a + v.y*m.b, v.x*m.c + v.y*m.d);
227}
228
230
231#endif
static cpFloat cpfclamp(cpFloat f, cpFloat min, cpFloat max)
Clamp f to be between min and max.
Definition chipmunk_types.h:138
unsigned char cpBool
Chipmunk's boolean type.
Definition chipmunk_types.h:179
double cpFloat
Chipmunk's floating point type.
Definition chipmunk_types.h:68
static cpFloat cpfmin(cpFloat a, cpFloat b)
Return the min of two cpFloats.
Definition chipmunk_types.h:126
static cpFloat cpvdist(const cpVect v1, const cpVect v2)
Returns the distance between v1 and v2.
Definition cpVect.h:192
static cpVect cpvrperp(const cpVect v)
Returns a perpendicular vector. (-90 degree rotation)
Definition cpVect.h:93
static cpVect cpvnormalize(const cpVect v)
Returns a normalized copy of v.
Definition cpVect.h:147
static cpVect cpvadd(const cpVect v1, const cpVect v2)
Add two vectors.
Definition cpVect.h:49
static const cpVect cpvzero
Constant for the zero vector.
Definition cpVect.h:33
static cpVect cpvclamp(const cpVect v, const cpFloat len)
Clamp v to length len.
Definition cpVect.h:180
static cpBool cpvnear(const cpVect v1, const cpVect v2, const cpFloat dist)
Returns true if the distance between v1 and v2 is less than dist.
Definition cpVect.h:204
static cpFloat cpvdot(const cpVect v1, const cpVect v2)
Vector dot product.
Definition cpVect.h:73
static cpVect cpvslerp(const cpVect v1, const cpVect v2, const cpFloat t)
Spherical linearly interpolate between v1 and v2.
Definition cpVect.h:155
static cpVect cpvmult(const cpVect v, const cpFloat s)
Scalar multiplication.
Definition cpVect.h:67
static cpVect cpv(const cpFloat x, const cpFloat y)
Convenience constructor for cpVect structs.
Definition cpVect.h:36
static cpFloat cpvtoangle(const cpVect v)
Returns the angular direction v is pointing in (in radians).
Definition cpVect.h:111
static cpFloat cpvlength(const cpVect v)
Returns the length of v.
Definition cpVect.h:135
static cpVect cpvlerpconst(cpVect v1, cpVect v2, cpFloat d)
Linearly interpolate between v1 towards v2 by distance d.
Definition cpVect.h:186
static cpVect cpvneg(const cpVect v)
Negate a vector.
Definition cpVect.h:61
static cpVect cpvlerp(const cpVect v1, const cpVect v2, const cpFloat t)
Linearly interpolate between v1 and v2.
Definition cpVect.h:141
static cpVect cpvunrotate(const cpVect v1, const cpVect v2)
Inverse of cpvrotate().
Definition cpVect.h:123
static cpVect cpvperp(const cpVect v)
Returns a perpendicular vector. (90 degree rotation)
Definition cpVect.h:87
static cpVect cpvrotate(const cpVect v1, const cpVect v2)
Uses complex number multiplication to rotate v1 by v2. Scaling will occur if v1 is not a unit vector.
Definition cpVect.h:117
static cpVect cpvslerpconst(const cpVect v1, const cpVect v2, const cpFloat a)
Spherical linearly interpolate between v1 towards v2 by no more than angle a radians.
Definition cpVect.h:171
static cpFloat cpvdistsq(const cpVect v1, const cpVect v2)
Returns the squared distance between v1 and v2. Faster than cpvdist() when you only need to compare d...
Definition cpVect.h:198
static cpFloat cpvlengthsq(const cpVect v)
Returns the squared length of v. Faster than cpvlength() when you only need to compare lengths.
Definition cpVect.h:129
static cpBool cpveql(const cpVect v1, const cpVect v2)
Check if two vectors are equal. (Be careful when comparing floating point numbers!...
Definition cpVect.h:43
static cpVect cpvproject(const cpVect v1, const cpVect v2)
Returns the vector projection of v1 onto v2.
Definition cpVect.h:99
static cpFloat cpvcross(const cpVect v1, const cpVect v2)
2D vector cross product analog.
Definition cpVect.h:81
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
Definition chipmunk_types.h:264
Definition chipmunk_types.h:251