aboutsummaryrefslogtreecommitdiff
path: root/Client/Source/Phy2D/Common
diff options
context:
space:
mode:
Diffstat (limited to 'Client/Source/Phy2D/Common')
-rw-r--r--Client/Source/Phy2D/Common/Math.h191
-rw-r--r--Client/Source/Phy2D/Common/Settings.h82
-rw-r--r--Client/Source/Phy2D/Common/Type.h0
3 files changed, 76 insertions, 197 deletions
diff --git a/Client/Source/Phy2D/Common/Math.h b/Client/Source/Phy2D/Common/Math.h
index 9c0ff8f..e69de29 100644
--- a/Client/Source/Phy2D/Common/Math.h
+++ b/Client/Source/Phy2D/Common/Math.h
@@ -1,191 +0,0 @@
-#pragma once
-
-#include <math.h>
-#include <assert.h>
-#include <stdlib.h>
-#include <string>
-
-#include "Settings.h"
-
-namespace Phy2D
-{
-
- struct Vec2
- {
- Vec2() {}
- Vec2(number x, number y) : x(x), y(y) {}
-
- void Set(number x_, number 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 *= (number a)
- {
- x *= a; y *= a;
- }
-
- number Length() const
- {
- return SQRT(x * x + y * y);
- }
-
- std::string ToString()
- {
- return std::to_string((float)x) + "," + std::to_string((float)y);
- }
-
- number x, y;
- };
-
- struct Mat22
- {
- Mat22() {}
- Mat22(number angle)
- {
- number c = COS(angle), s = 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
- {
- number a = col1.x, b = col2.x, c = col1.y, d = col2.y;
- Mat22 B;
- number det = a * d - b * c;
- assert(det != 0.0f);
- det = (number)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 number Dot(const Vec2& a, const Vec2& b)
- {
- return a.x * b.x + a.y * b.y;
- }
-
- inline number Cross(const Vec2& a, const Vec2& b)
- {
- return a.x * b.y - a.y * b.x;
- }
-
- inline Vec2 Cross(const Vec2& a, number s)
- {
- return Vec2(s * a.y, -s * a.x);
- }
-
- inline Vec2 Cross(number 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 * (number 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 number Abs(number 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 number Sign(number x)
- {
- return (float) x < 0.0f ? -1.0f : 1.0f;
- }
-
- inline number Min(number a, number b)
- {
- return a < b ? a : b;
- }
-
- inline number Max(number a, number b)
- {
- return a > b ? a : b;
- }
-
- inline number Clamp(number a, number low, number 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 number Random()
- //{
- // number r = (number)rand();
- // r /= RAND_MAX;
- // r = (number)2.0f * r - 1.0f;
- // return r;
- //}
-
- //inline number Random(number lo, number hi)
- //{
- // number r = (number)rand();
- // r /= RAND_MAX;
- // r = (hi - lo) * r + lo;
- // return r;
- //}
-
-}
diff --git a/Client/Source/Phy2D/Common/Settings.h b/Client/Source/Phy2D/Common/Settings.h
index 5c24b57..318a76c 100644
--- a/Client/Source/Phy2D/Common/Settings.h
+++ b/Client/Source/Phy2D/Common/Settings.h
@@ -1,27 +1,97 @@
#pragma once
+#define NUMBER_FLOAT 1
+#define NUMBER_LIBFIX 2
+#define NUMBER_FPM 3
+
+#define NUMBER_ALIAS NUMBER_FPM
+
+#if NUMBER_ALIAS == NUMBER_LIBFIX
#include "libfixmath/libfixmath/fixmath.h"
+#elif NUMBER_ALIAS == NUMBER_FPM
+#include "fpm/include/fpm/fixed.hpp"
+#include "fpm/include/fpm/math.hpp"
+#endif
namespace Phy2D
{
-#define NUMBER_FLOAT false
+#if NUMBER_ALIAS == NUMBER_FLOAT
-#if NUMBER_FLOAT
-typedef float number;
+ typedef float fixed;
#define NUMBER_MAX (FLT_MAX)
#define NUMBER_MIN (FLT_MIN)
#define SQRT(a) (sqrt((a)))
#define SIN(a) (sin((a)))
#define COS(a) (cos((a)))
-#else
-// 同时一定要开启内联函数扩展,否则执行效率会非常低
-typedef Fix16 number;
+#define PI (3.1415925f)
+
+#elif NUMBER_ALIAS == NUMBER_LIBFIX
+
+ // 同时一定要开启内联函数扩展,否则执行效率会非常低
+ typedef Fix16 fixed;
#define NUMBER_MAX (fix16_maximum)
#define NUMBER_MIN (fix16_minimum)
#define SQRT(a) ((a).sqrt())
#define SIN(a) ((a).sin())
#define COS(a) ((a).cos())
+#define PI (fix16_pi)
+
+#elif NUMBER_ALIAS == NUMBER_FPM
+
+ template <typename T>
+ struct Limits {};
+
+ template <>
+ struct Limits<fpm::fixed_16_16>
+ {
+ static constexpr bool is_signed() noexcept { return true; }
+ static constexpr int digits() noexcept { return 31; }
+ static constexpr int max_digits10() noexcept { return 5 + 5; }
+ static constexpr int min_exponent() noexcept { return -15; }
+ static constexpr int max_exponent() noexcept { return 15; }
+ static constexpr int min_exponent10() noexcept { return -4; }
+ static constexpr int max_exponent10() noexcept { return 4; }
+ static constexpr fpm::fixed_16_16 min() noexcept { return fpm::fixed_16_16::from_raw_value(-2147483647 - 1); }
+ static constexpr fpm::fixed_16_16 max() noexcept { return fpm::fixed_16_16::from_raw_value(2147483647); }
+ };
+
+ template <>
+ struct Limits<fpm::fixed_24_8>
+ {
+ static constexpr bool is_signed() noexcept { return true; }
+ static constexpr int digits() noexcept { return 31; }
+ static constexpr int max_digits10() noexcept { return 7 + 3; }
+ static constexpr int min_exponent() noexcept { return -7; }
+ static constexpr int max_exponent() noexcept { return 23; }
+ static constexpr int min_exponent10() noexcept { return -2; }
+ static constexpr int max_exponent10() noexcept { return 6; }
+ static constexpr fpm::fixed_24_8 min() noexcept { return fpm::fixed_24_8::from_raw_value(-2147483647 - 1); }
+ static constexpr fpm::fixed_24_8 max() noexcept { return fpm::fixed_24_8::from_raw_value(2147483647); }
+ };
+
+ template <>
+ struct Limits<fpm::fixed_8_24>
+ {
+ static constexpr bool is_signed() noexcept { return true; }
+ static constexpr int digits() noexcept { return 31; }
+ static constexpr int max_digits10() noexcept { return 3 + 8; }
+ static constexpr int min_exponent() noexcept { return -23; }
+ static constexpr int max_exponent() noexcept { return 7; }
+ static constexpr int min_exponent10() noexcept { return -7; }
+ static constexpr int max_exponent10() noexcept { return 2; }
+ static constexpr fpm::fixed_8_24 min() noexcept { return fpm::fixed_8_24::from_raw_value(-2147483647 - 1); }
+ static constexpr fpm::fixed_8_24 max() noexcept { return fpm::fixed_8_24::from_raw_value(2147483647); }
+ };
+
+ typedef fpm::fixed_16_16 fixed;
+#define NUMBER_MAX (Limits<fixed>::max())
+#define NUMBER_MIN (Limits<fixed>::min())
+#define SQRT(a) (fpm::sqrt((a)))
+#define SIN(a) (fpm::sin((a)))
+#define COS(a) (fpm::cos((a)))
+#define PI (fixed::pi())
+
#endif
} \ No newline at end of file
diff --git a/Client/Source/Phy2D/Common/Type.h b/Client/Source/Phy2D/Common/Type.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Client/Source/Phy2D/Common/Type.h