Something went wrong. Try again.
Box2d lite but uses fixed points instead of floats for embedded devices, fork from https://github.com/erincatto/box2d-lite github.com/erincatto/box2d-lite
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197/** Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com** Permission to use, copy, modify, distribute and sell this software* and its documentation for any purpose is hereby granted without fee,* provided that the above copyright notice appear in all copies.* Erin Catto makes no representations about the suitability * of this software for any purpose. * It is provided "as is" without express or implied warranty.*/
#ifndef MATHUTILS_H#define MATHUTILS_H
#include <math.h>#include <float.h>#include <assert.h>#include <stdlib.h>
const float k_pi = 3.14159265358979323846264f;
struct Vec2{ Vec2() {} Vec2(float x, float y) : x(x), y(y) {}
void Set(float x_, float y_) { x = x_; y = y_; }
Vec2 operator -() { return Vec2(-x, -y); } void operator += (const Vec2& v) { x += v.x; y += v.y; } void operator -= (const Vec2& v) { x -= v.x; y -= v.y; }
void operator *= (float a) { x *= a; y *= a; }
float Length() const { return sqrtf(x * x + y * y); }
float x, y;};
struct Mat22{ Mat22() {} Mat22(float angle) { float c = cosf(angle), s = sinf(angle); col1.x = c; col2.x = -s; col1.y = s; col2.y = c; }
Mat22(const Vec2& col1, const Vec2& col2) : col1(col1), col2(col2) {}
Mat22 Transpose() const { return Mat22(Vec2(col1.x, col2.x), Vec2(col1.y, col2.y)); }
Mat22 Invert() const { float a = col1.x, b = col2.x, c = col1.y, d = col2.y; Mat22 B; float det = a * d - b * c; assert(det != 0.0f); det = 1.0f / det; B.col1.x = det * d; B.col2.x = -det * b; B.col1.y = -det * c; B.col2.y = det * a; return B; }
Vec2 col1, col2;};
inline float Dot(const Vec2& a, const Vec2& b){ return a.x * b.x + a.y * b.y;}
inline float Cross(const Vec2& a, const Vec2& b){ return a.x * b.y - a.y * b.x;}
inline Vec2 Cross(const Vec2& a, float s){ return Vec2(s * a.y, -s * a.x);}
inline Vec2 Cross(float s, const Vec2& a){ return Vec2(-s * a.y, s * a.x);}
inline Vec2 operator * (const Mat22& A, const Vec2& v){ return Vec2(A.col1.x * v.x + A.col2.x * v.y, A.col1.y * v.x + A.col2.y * v.y);}
inline Vec2 operator + (const Vec2& a, const Vec2& b){ return Vec2(a.x + b.x, a.y + b.y);}
inline Vec2 operator - (const Vec2& a, const Vec2& b){ return Vec2(a.x - b.x, a.y - b.y);}
inline Vec2 operator * (float s, const Vec2& v){ return Vec2(s * v.x, s * v.y);}
inline Mat22 operator + (const Mat22& A, const Mat22& B){ return Mat22(A.col1 + B.col1, A.col2 + B.col2);}
inline Mat22 operator * (const Mat22& A, const Mat22& B){ return Mat22(A * B.col1, A * B.col2);}
inline float Abs(float a){ return a > 0.0f ? a : -a;}
inline Vec2 Abs(const Vec2& a){ return Vec2(fabsf(a.x), fabsf(a.y));}
inline Mat22 Abs(const Mat22& A){ return Mat22(Abs(A.col1), Abs(A.col2));}
inline float Sign(float x){ return x < 0.0f ? -1.0f : 1.0f;}
inline float Min(float a, float b){ return a < b ? a : b;}
inline float Max(float a, float b){ return a > b ? a : b;}
inline float Clamp(float a, float low, float high){ return Max(low, Min(a, high));}
template<typename T> inline void Swap(T& a, T& b){ T tmp = a; a = b; b = tmp;}
// Random number in range [-1,1]inline float Random(){ float r = (float)rand(); r /= RAND_MAX; r = 2.0f * r - 1.0f; return r;}
inline float Random(float lo, float hi){ float r = (float)rand(); r /= RAND_MAX; r = (hi - lo) * r + lo; return r;}
#endif