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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202/** 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 <assert.h>#include <math.h>#include <stdlib.h>#include "fixed.hpp"#include "math.hpp"
using fixed_float = fpm::fixed<std::int32_t, std::int64_t, 16, false>;
const fixed_float k_pi { 3.141592653f };
struct Vec2{ Vec2() {} Vec2(float x, float y) : x(fixed_float(x)), y(fixed_float(y)) {} Vec2(fixed_float x, fixed_float y) : x(x), y(y) {}
void Set(fixed_float x_, fixed_float y_) { x = x_; y = y_; } void Set(float x_, float y_) { x = fixed_float(x_); y = fixed_float(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 *= (fixed_float a) { x *= a; y *= a; }
fixed_float Length() const { return fpm::sqrt(x * x + y * y); }
fixed_float x, y;};
struct Mat22{ Mat22() {} Mat22(fixed_float angle) { fixed_float c = fpm::cos(angle), s = fpm::sin(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 { fixed_float a = col1.x, b = col2.x, c = col1.y, d = col2.y; Mat22 B; fixed_float det = a * d - b * c; assert(det != 0.0f); det = fixed_float(1.0) / 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 fixed_float Dot(const Vec2& a, const Vec2& b){ return a.x * b.x + a.y * b.y;}
inline fixed_float Cross(const Vec2& a, const Vec2& b){ return a.x * b.y - a.y * b.x;}
inline Vec2 Cross(const Vec2& a, fixed_float s){ return Vec2(s * a.y, -s * a.x);}
inline Vec2 Cross(fixed_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 * (fixed_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 fixed_float Abs(fixed_float a){ return a > fixed_float(0.0f) ? a : -a;}
inline Vec2 Abs(const Vec2& a){ return Vec2(fpm::abs(a.x), fpm::abs(a.y));}
inline Mat22 Abs(const Mat22& A){ return Mat22(Abs(A.col1), Abs(A.col2));}
inline fixed_float Sign(fixed_float x){ return x < fixed_float(0.0f) ? fixed_float(-1.0f) : fixed_float(1.0f);}
inline fixed_float Min(fixed_float a, fixed_float b){ return a < b ? a : b;}
inline fixed_float Max(fixed_float a, fixed_float b){ return a > b ? a : b;}
inline fixed_float Clamp(fixed_float a, fixed_float low, fixed_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 fixed_float Random(){ fixed_float r = (fixed_float)rand(); r /= RAND_MAX; r = fixed_float(2.0f) * r - fixed_float(1.0f); return r;}
inline fixed_float Random(fixed_float lo, fixed_float hi){ fixed_float r = (fixed_float)rand(); r /= RAND_MAX; r = (hi - lo) * r + lo; return r;}
#endif