|
1 | | -# FixedMathSharp |
2 | | - |
3 | | -<p align="center"> |
4 | | - <img src=".assets/icon_128x128.png" alt="FixedMathSharp Icon" width="200"/> |
5 | | -</p> |
6 | | - |
7 | | -**FixedMathSharp** is a high-precision, fixed-point mathematics library |
8 | | -designed to enable deterministic, efficient arithmetic operations. It |
9 | | -provides robust implementations of mathematical functions, trigonometric |
10 | | -operations, and vector transformations, ensuring precision without the |
11 | | -pitfalls of floating-point errors. |
12 | | - |
13 | | -## Key Components |
14 | | - |
15 | | -- **Fixed64**: Represents a 64-bit fixed-point number with configurable |
16 | | - fractional precision. |
17 | | -- **FixedMath**: A partial static class providing core mathematical |
18 | | - functions for fixed-point numbers, including clamping, rounding, and |
19 | | - interpolation. |
20 | | -- **FixedTrigonometry**: A module containing trigonometric and |
21 | | - logarithmic functions, ensuring consistent angle calculations with |
22 | | - fixed-point precision. |
23 | | - |
24 | | -## Usage Overview |
25 | | - |
26 | | -FixedMathSharp is useful for applications where determinism is critical, |
27 | | -such as simulations, games, and physics engines. With support for |
28 | | -precise math functions, developers can rely on consistent results across |
29 | | -platforms and eliminate floating-point discrepancies. |
30 | | - |
31 | | -### Installation |
32 | | - |
33 | | -1. Clone the repository: |
34 | | - `git clone https://github.com/your-repo/fixedmathsharp.git` |
35 | | -2. Add the project as a dependency to your .NET solution. |
36 | | -3. Reference the `FixedMathSharp` namespace in your files: |
37 | | - `using FixedMathSharp;` |
| 1 | +FixedMathSharp |
| 2 | +============== |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +**A high-precision, deterministic fixed-point math library for .NET.** |
| 7 | +Ideal for simulations, games, and physics engines requiring reliable arithmetic without floating-point inaccuracies. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## 🛠️ Key Features |
| 12 | + |
| 13 | +- **Deterministic Calculations:** Perfect for simulations, multiplayer games, and physics engines. |
| 14 | +- **High Precision Arithmetic:** Uses fixed-point math to eliminate floating-point inaccuracies. |
| 15 | +- **Comprehensive Vector Support:** Includes 2D and 3D vector operations (`Vector2d`, `Vector3d`). |
| 16 | +- **Quaternion Rotations:** Leverage `FixedQuaternion` for smooth rotations without gimbal lock. |
| 17 | +- **Matrix Operations:** Supports transformations with `Fixed4x4` and `Fixed3x3` matrices. |
| 18 | +- **Bounding Shapes:** Includes `IBound` structs `BoundingBox`, `BoundingSphere`, and `BoundingArea` for lightweight spatial calculations. |
| 19 | +- **Advanced Math Functions:** Includes trigonometry and common math utilities. |
| 20 | +- **Unity Integration:** Seamless interoperability with Unity using `FixedMathSharp.Editor`. |
| 21 | + |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## 🚀 Installation |
| 26 | + |
| 27 | +### Via NuGet: |
| 28 | +```bash |
| 29 | +dotnet add package FixedMathSharp |
| 30 | +``` |
| 31 | + |
| 32 | +### Testing Locally: |
| 33 | +1. Build the package: |
| 34 | + ```bash |
| 35 | + dotnet pack --configuration Release |
| 36 | + ``` |
| 37 | +2. Create a local NuGet source and test it: |
| 38 | + ```bash |
| 39 | + mkdir ./LocalNuGet |
| 40 | + cp ./bin/Release/*.nupkg ./LocalNuGet/ |
| 41 | + dotnet nuget add source ./LocalNuGet --name LocalNuGet |
| 42 | + dotnet add package FixedMathSharp --source ./LocalNuGet |
| 43 | + ``` |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## 📖 Usage Examples |
| 48 | + |
| 49 | +### Basic Arithmetic with `Fixed64`: |
| 50 | +```csharp |
| 51 | +Fixed64 a = new Fixed64(1.5); |
| 52 | +Fixed64 b = new Fixed64(2.5); |
| 53 | +Fixed64 result = a + b; |
| 54 | +Console.WriteLine(result); // Output: 4.0 |
| 55 | +``` |
| 56 | + |
| 57 | +### Vector Operations: |
| 58 | +```csharp |
| 59 | +Vector3d v1 = new Vector3d(1, 2, 3); |
| 60 | +Vector3d v2 = new Vector3d(4, 5, 6); |
| 61 | +Fixed64 dotProduct = Vector3d.Dot(v1, v2); |
| 62 | +Console.WriteLine(dotProduct); // Output: 32 |
| 63 | +``` |
| 64 | + |
| 65 | +### Quaternion Rotation: |
| 66 | +```csharp |
| 67 | +FixedQuaternion rotation = FixedQuaternion.FromAxisAngle(Vector3d.Up, FixedMath.PiOver2); // 90 degrees around Y-axis |
| 68 | +Vector3d point = new Vector3d(1, 0, 0); |
| 69 | +Vector3d rotatedPoint = rotation.Rotate(point); |
| 70 | +Console.WriteLine(rotatedPoint); // Output: (0, 0, -1) |
| 71 | +``` |
| 72 | + |
| 73 | +### Matrix Transformations: |
| 74 | +```csharp |
| 75 | +Fixed4x4 matrix = Fixed4x4.Identity; |
| 76 | +Vector3d position = new Vector3d(1, 2, 3); |
| 77 | +matrix.SetTransform(position, Vector3d.One, FixedQuaternion.Identity); |
| 78 | +Console.WriteLine(matrix); |
| 79 | +``` |
| 80 | + |
| 81 | +### Bounding Shapes and Intersection |
| 82 | +```csharp |
| 83 | +BoundingBox box = new BoundingBox(new Vector3d(0, 0, 0), new Vector3d(5, 5, 5)); |
| 84 | +BoundingSphere sphere = new BoundingSphere(new Vector3d(3, 3, 3), new Fixed64(1)); |
| 85 | +bool intersects = box.Intersects(sphere); |
| 86 | +Console.WriteLine(intersects); // Output: True |
| 87 | +``` |
| 88 | + |
| 89 | +### Trigonometry Example: |
| 90 | +```csharp |
| 91 | +Fixed64 angle = FixedMath.PiOver4; // 45 degrees |
| 92 | +Fixed64 sinValue = FixedTrigonometry.Sin(angle); |
| 93 | +Console.WriteLine(sinValue); // Output: ~0.707 |
| 94 | +``` |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +## 📦 Library Structure |
| 99 | + |
| 100 | +- **`Fixed64` Struct:** Represents fixed-point numbers for precise arithmetic. |
| 101 | +- **`Vector2d` and `Vector3d` Structs:** Handle 2D and 3D vector operations. |
| 102 | +- **`FixedQuaternion` Struct:** Provides rotation handling without gimbal lock, enabling smooth rotations and quaternion-based transformations. |
| 103 | +- **`IBound` Interface:** Standard interface for bounding shapes `BoundingBox`, `BoundingArea`, and `BoundingSphere`, each offering intersection, containment, and projection logic. |
| 104 | +- **`FixedMath` Static Class:** Provides common math functions. |
| 105 | +- **`FixedTrigonometry` Class:** Offers trigonometric functions using fixed-point math. |
| 106 | +- **`Fixed4x4` and `Fixed3x3`:** Support matrix operations for transformations. |
| 107 | +- **`FixedMathSharp.Editor`:** Extensions for seamless integration with Unity, including property drawers and type conversions. |
38 | 108 |
|
39 | 109 | ### Fixed64 Struct |
40 | 110 |
|
41 | | -**Fixed64** is the core data type representing fixed-point numbers. It |
| 111 | +**Fixed64** is the core data type representing fixed-point numbers. It |
42 | 112 | provides various mathematical operations, including addition, |
43 | 113 | subtraction, multiplication, division, and more. The struct guarantees |
44 | 114 | deterministic behavior by using integer-based arithmetic with a |
45 | 115 | configurable `SHIFT_AMOUNT`. |
46 | 116 |
|
47 | | -#### Example |
| 117 | +--- |
| 118 | + |
| 119 | +## ⚡ Performance Considerations |
48 | 120 |
|
49 | | - var fixedValue = new Fixed64(1.5); |
50 | | - var doubledValue = fixedValue * Fixed64.Two; |
51 | | - Console.WriteLine(doubledValue); // Outputs: 3.0 |
52 | | - |
| 121 | +This library leverages **inline methods** and **fixed-point arithmetic** |
| 122 | +to ensure high precision without the pitfalls of floating-point numbers. |
| 123 | +It is optimized for **deterministic behavior**, making it ideal for physics |
| 124 | +engines, multiplayer simulations, and other time-sensitive applications. |
53 | 125 |
|
54 | | -### FixedMath Static Class |
| 126 | +--- |
55 | 127 |
|
56 | | -**FixedMath** contains essential functions like clamping, interpolation, |
57 | | -rounding, and utility operations for `Fixed64`. It supports operations |
58 | | -like `Clamp`, `MoveTowards`, and `RoundToPrecision`. |
| 128 | +## 🧪 Testing and Validation |
59 | 129 |
|
60 | | -#### Example |
| 130 | +Unit tests are used extensively to validate the correctness of mathematical |
| 131 | +operations. Special **fuzzy comparisons** are employed where small precision |
| 132 | +discrepancies might occur, mimicking floating-point behavior. |
61 | 133 |
|
62 | | - var clampedValue = FixedMath.Clamp(new Fixed64(2.5), Fixed64.Zero, Fixed64.One); |
63 | | - Console.WriteLine(clampedValue); // Outputs: 1.0 |
64 | | - |
| 134 | +To run the tests: |
| 135 | +```bash |
| 136 | +dotnet test --configuration Release |
| 137 | +``` |
65 | 138 |
|
66 | | -### FixedTrigonometry Module |
| 139 | +--- |
67 | 140 |
|
68 | | -The **FixedTrigonometry** module extends FixedMath with trigonometric |
69 | | -functions like `Sin`, `Cos`, `Tan`, and inverse functions. These |
70 | | -functions ensure accurate angle calculations, even with fixed-point |
71 | | -precision. |
| 141 | +## 🛠️ Compatibility |
72 | 142 |
|
73 | | -#### Example |
| 143 | +- **.NET Framework:** 4.7.1+ |
| 144 | +- **Unity3D:** Fully compatible with Unity using the `FixedMathSharp.Editor` extension. |
| 145 | +- **Platforms:** Windows, Linux, macOS |
74 | 146 |
|
75 | | - var angle = FixedMath.DegToRad(new Fixed64(90)); |
76 | | - var sineValue = FixedMath.Sin(angle); |
77 | | - Console.WriteLine(sineValue); // Outputs: 1.0 |
78 | | - |
| 147 | +--- |
79 | 148 |
|
80 | | -## Key Features |
| 149 | +## 📄 License |
81 | 150 |
|
82 | | -- **Deterministic Behavior**: Fixed-point arithmetic ensures consistency |
83 | | - across platforms. |
84 | | -- **Precision Control**: Configurable fractional bits for optimal |
85 | | - precision. |
86 | | -- **Efficient Operations**: Optimized for performance-critical scenarios |
87 | | - with minimal overhead. |
88 | | -- **Comprehensive Trigonometry**: Includes sine, cosine, tangent, and |
89 | | - their inverse functions. |
| 151 | +This project is licensed under the MIT License - see the `LICENSE` file |
| 152 | +for details. |
90 | 153 |
|
91 | | -## Contributing |
| 154 | +--- |
92 | 155 |
|
93 | | -We welcome contributions to improve FixedMathSharp. Please fork the |
94 | | -repository and submit a pull request with your changes. Ensure that all |
95 | | -existing tests pass and add new tests for any new functionality. |
| 156 | +## 👥 Contributors |
96 | 157 |
|
97 | | -### Testing |
| 158 | +- **mrdav30** - Lead Developer |
| 159 | +- Contributions are welcome! Feel free to submit pull requests or report issues. |
98 | 160 |
|
99 | | -Run the unit tests to verify that the library functions correctly: |
| 161 | +--- |
100 | 162 |
|
101 | | - dotnet test |
| 163 | +## 📧 Contact |
102 | 164 |
|
103 | | -## License |
| 165 | +For questions or support, reach out to **mrdav30** via GitHub or open an issue in the repository. |
104 | 166 |
|
105 | | -This project is licensed under the MIT License - see the `LICENSE` file |
106 | | -for details. |
| 167 | +--- |
0 commit comments