-
Notifications
You must be signed in to change notification settings - Fork 96
/
IEMath.h
319 lines (262 loc) · 5.78 KB
/
IEMath.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
Copyright (c) 2013 Randy Gaul http://RandyGaul.net
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef IEMATH_H
#define IEMATH_H
// Impulse Engine Math : IEMath
#include <cmath> // abs, sqrt
#include <cassert> // assert
#include <algorithm> // max, min
typedef float real;
typedef double real64;
typedef signed char int8;
typedef signed short int16;
typedef signed int int32;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
#ifdef WIN32 // these types are not standard, only exist in windows
typedef __int64 int64;
typedef unsigned __int64 uint64;
#endif
typedef float f32;
typedef double f64;
const real PI = 3.141592741f;
const real EPSILON = 0.0001f;
struct Vec2
{
union
{
real m[1][1];
real v[2];
struct
{
real x;
real y;
};
};
Vec2( ) {}
Vec2( real x_, real y_ )
: x( x_ )
, y( y_ )
{
}
void Set( real x_, real y_ )
{
x = x_;
y = y_;
}
Vec2 operator-( void ) const
{
return Vec2( -x, -y );
}
Vec2 operator*( real s ) const
{
return Vec2( x * s, y * s );
}
Vec2 operator/( real s ) const
{
return Vec2( x / s, y / s );
}
void operator*=( real s )
{
x *= s;
y *= s;
}
Vec2 operator+( const Vec2& rhs ) const
{
return Vec2( x + rhs.x, y + rhs.y );
}
Vec2 operator+( real s ) const
{
return Vec2( x + s, y + s );
}
void operator+=( const Vec2& rhs )
{
x += rhs.x;
y += rhs.y;
}
Vec2 operator-( const Vec2& rhs ) const
{
return Vec2( x - rhs.x, y - rhs.y );
}
void operator-=( const Vec2& rhs )
{
x -= rhs.x;
y -= rhs.y;
}
real LenSqr( void ) const
{
return x * x + y * y;
}
real Len( void ) const
{
return std::sqrt( x * x + y * y );
}
void Rotate( real radians )
{
real c = std::cos( radians );
real s = std::sin( radians );
real xp = x * c - y * s;
real yp = x * s + y * c;
x = xp;
y = yp;
}
void Normalize( void )
{
real len = Len( );
if(len > EPSILON)
{
real invLen = 1.0f / len;
x *= invLen;
y *= invLen;
}
}
};
inline Vec2 operator*( float s, const Vec2& v )
{
return Vec2( s * v.x, s * v.y );
}
struct Mat2
{
union
{
struct
{
real m00, m01;
real m10, m11;
};
real m[2][2];
real v[4];
};
Mat2( ) {}
Mat2( real radians )
{
real c = std::cos( radians );
real s = std::sin( radians );
m00 = c; m01 = -s;
m10 = s; m11 = c;
}
Mat2( real a, real b, real c, real d )
: m00( a ), m01( b )
, m10( c ), m11( d )
{
}
void Set( real radians )
{
real c = std::cos( radians );
real s = std::sin( radians );
m00 = c; m01 = -s;
m10 = s; m11 = c;
}
Mat2 Abs( void ) const
{
return Mat2( std::abs( m00 ), std::abs( m01 ), std::abs( m10 ), std::abs( m11 ) );
}
Vec2 AxisX( void ) const
{
return Vec2( m00, m10 );
}
Vec2 AxisY( void ) const
{
return Vec2( m01, m11 );
}
Mat2 Transpose( void ) const
{
return Mat2( m00, m10, m01, m11 );
}
const Vec2 operator*( const Vec2& rhs ) const
{
return Vec2( m00 * rhs.x + m01 * rhs.y, m10 * rhs.x + m11 * rhs.y );
}
const Mat2 operator*( const Mat2& rhs ) const
{
// [00 01] [00 01]
// [10 11] [10 11]
return Mat2(
m[0][0] * rhs.m[0][0] + m[0][1] * rhs.m[1][0],
m[0][0] * rhs.m[0][1] + m[0][1] * rhs.m[1][1],
m[1][0] * rhs.m[0][0] + m[1][1] * rhs.m[1][0],
m[1][0] * rhs.m[0][1] + m[1][1] * rhs.m[1][1]
);
}
};
inline Vec2 Min( const Vec2& a, const Vec2& b )
{
return Vec2( std::min( a.x, b.x ), std::min( a.y, b.y ) );
}
inline Vec2 Max( const Vec2& a, const Vec2& b )
{
return Vec2( std::max( a.x, b.x ), std::max( a.y, b.y ) );
}
inline real Dot( const Vec2& a, const Vec2& b )
{
return a.x * b.x + a.y * b.y;
}
inline real DistSqr( const Vec2& a, const Vec2& b )
{
Vec2 c = a - b;
return Dot( c, c );
}
inline Vec2 Cross( const Vec2& v, real a )
{
return Vec2( a * v.y, -a * v.x );
}
inline Vec2 Cross( real a, const Vec2& v )
{
return Vec2( -a * v.y, a * v.x );
}
inline real Cross( const Vec2& a, const Vec2& b )
{
return a.x * b.y - a.y * b.x;
}
// Comparison with tolerance of EPSILON
inline bool Equal( real a, real b )
{
// <= instead of < for NaN comparison safety
return std::abs( a - b ) <= EPSILON;
}
inline real Sqr( real a )
{
return a * a;
}
inline real Clamp( real min, real max, real a )
{
if (a < min) return min;
if (a > max) return max;
return a;
}
inline int32 Round( real a )
{
return (int32)(a + 0.5f);
}
inline real Random( real l, real h )
{
real a = (real)rand( );
a /= RAND_MAX;
a = (h - l) * a + l;
return a;
}
inline bool BiasGreaterThan( real a, real b )
{
const real k_biasRelative = 0.95f;
const real k_biasAbsolute = 0.01f;
return a >= b * k_biasRelative + a * k_biasAbsolute;
}
const f32 gravityScale = 5.0f;
const Vec2 gravity( 0, 10.0f * gravityScale );
const float dt = 1.0f / 60.0f;
#endif // IEMATH_H