Chipmunk2D Pro API Reference  6.1.5
 All Classes Functions Variables Typedefs Properties Groups Pages
cpVect.h
1 /* Copyright (c) 2007 Scott Lembcke
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 
25 
27 static const cpVect cpvzero = {0.0f,0.0f};
28 
30 static inline cpVect cpv(const cpFloat x, const cpFloat y)
31 {
32  cpVect v = {x, y};
33  return v;
34 }
35 
37 cpVect cpvslerp(const cpVect v1, const cpVect v2, const cpFloat t);
38 
40 cpVect cpvslerpconst(const cpVect v1, const cpVect v2, const cpFloat a);
41 
45 char* cpvstr(const cpVect v);
46 
48 static inline cpBool cpveql(const cpVect v1, const cpVect v2)
49 {
50  return (v1.x == v2.x && v1.y == v2.y);
51 }
52 
54 static inline cpVect cpvadd(const cpVect v1, const cpVect v2)
55 {
56  return cpv(v1.x + v2.x, v1.y + v2.y);
57 }
58 
60 static inline cpVect cpvsub(const cpVect v1, const cpVect v2)
61 {
62  return cpv(v1.x - v2.x, v1.y - v2.y);
63 }
64 
66 static inline cpVect cpvneg(const cpVect v)
67 {
68  return cpv(-v.x, -v.y);
69 }
70 
72 static inline cpVect cpvmult(const cpVect v, const cpFloat s)
73 {
74  return cpv(v.x*s, v.y*s);
75 }
76 
78 static inline cpFloat cpvdot(const cpVect v1, const cpVect v2)
79 {
80  return v1.x*v2.x + v1.y*v2.y;
81 }
82 
86 static inline cpFloat cpvcross(const cpVect v1, const cpVect v2)
87 {
88  return v1.x*v2.y - v1.y*v2.x;
89 }
90 
92 static inline cpVect cpvperp(const cpVect v)
93 {
94  return cpv(-v.y, v.x);
95 }
96 
98 static inline cpVect cpvrperp(const cpVect v)
99 {
100  return cpv(v.y, -v.x);
101 }
102 
104 static inline cpVect cpvproject(const cpVect v1, const cpVect v2)
105 {
106  return cpvmult(v2, cpvdot(v1, v2)/cpvdot(v2, v2));
107 }
108 
110 static inline cpVect cpvforangle(const cpFloat a)
111 {
112  return cpv(cpfcos(a), cpfsin(a));
113 }
114 
116 static inline cpFloat cpvtoangle(const cpVect v)
117 {
118  return cpfatan2(v.y, v.x);
119 }
120 
122 static inline cpVect cpvrotate(const cpVect v1, const cpVect v2)
123 {
124  return cpv(v1.x*v2.x - v1.y*v2.y, v1.x*v2.y + v1.y*v2.x);
125 }
126 
128 static inline cpVect cpvunrotate(const cpVect v1, const cpVect v2)
129 {
130  return cpv(v1.x*v2.x + v1.y*v2.y, v1.y*v2.x - v1.x*v2.y);
131 }
132 
134 static inline cpFloat cpvlengthsq(const cpVect v)
135 {
136  return cpvdot(v, v);
137 }
138 
140 static inline cpFloat cpvlength(const cpVect v)
141 {
142  return cpfsqrt(cpvdot(v, v));
143 }
144 
146 static inline cpVect cpvlerp(const cpVect v1, const cpVect v2, const cpFloat t)
147 {
148  return cpvadd(cpvmult(v1, 1.0f - t), cpvmult(v2, t));
149 }
150 
152 static inline cpVect cpvnormalize(const cpVect v)
153 {
154  return cpvmult(v, 1.0f/cpvlength(v));
155 }
156 
158 static inline cpVect cpvnormalize_safe(const cpVect v)
159 {
160  return (v.x == 0.0f && v.y == 0.0f ? cpvzero : cpvnormalize(v));
161 }
162 
164 static inline cpVect cpvclamp(const cpVect v, const cpFloat len)
165 {
166  return (cpvdot(v,v) > len*len) ? cpvmult(cpvnormalize(v), len) : v;
167 }
168 
170 static inline cpVect cpvlerpconst(cpVect v1, cpVect v2, cpFloat d)
171 {
172  return cpvadd(v1, cpvclamp(cpvsub(v2, v1), d));
173 }
174 
176 static inline cpFloat cpvdist(const cpVect v1, const cpVect v2)
177 {
178  return cpvlength(cpvsub(v1, v2));
179 }
180 
182 static inline cpFloat cpvdistsq(const cpVect v1, const cpVect v2)
183 {
184  return cpvlengthsq(cpvsub(v1, v2));
185 }
186 
188 static inline cpBool cpvnear(const cpVect v1, const cpVect v2, const cpFloat dist)
189 {
190  return cpvdistsq(v1, v2) < dist*dist;
191 }
192 
194 
198 
199 static inline cpMat2x2
200 cpMat2x2New(cpFloat a, cpFloat b, cpFloat c, cpFloat d)
201 {
202  cpMat2x2 m = {a, b, c, d};
203  return m;
204 }
205 
206 static inline cpVect
207 cpMat2x2Transform(cpMat2x2 m, cpVect v)
208 {
209  return cpv(v.x*m.a + v.y*m.b, v.x*m.c + v.y*m.d);
210 }
211