|
9 | 9 | [](https://github.com/mrdav30/FixedMathSharp/blob/main/LICENSE) |
10 | 10 | [](https://github.com/mrdav30/FixedMathSharp) |
11 | 11 |
|
12 | | -**A high-precision, deterministic fixed-point math library for .NET.** |
13 | | -Ideal for simulations, games, and physics engines requiring reliable arithmetic without floating-point inaccuracies. |
| 12 | +**A deterministic fixed-point math library for .NET.** |
| 13 | +Built for simulations, games, and physics-heavy code that needs reliable results without floating-point drift. |
14 | 14 |
|
15 | 15 | --- |
16 | 16 |
|
17 | 17 | ## 🛠️ Key Features |
18 | 18 |
|
19 | | -- **Deterministic Calculations:** Ensures consistent results across different platforms. |
20 | | -- **High Precision Arithmetic:** Uses fixed-point math to eliminate floating-point inaccuracies. |
21 | | -- **Comprehensive Vector Support:** Includes 2D and 3D vector operations (`Vector2d`, `Vector3d`). |
22 | | -- **Quaternion Rotations:** Leverage `FixedQuaternion` for smooth rotations without gimbal lock. |
23 | | -- **Matrix Operations:** Supports transformations with `Fixed4x4` and `Fixed3x3` matrices. |
24 | | -- **Bounding Shapes:** Includes `IBound` structs `BoundingBox`, `BoundingSphere`, and `BoundingArea` for lightweight spatial calculations. |
25 | | -- **Advanced Math Functions:** Includes trigonometry and common math utilities. |
26 | | -- **Framework Agnostic:** Works with **.NET, Unity, and other game engines**. |
27 | | -- **Flexible Serialization Options:** The default build includes out-of-the-box `MemoryPack` support across serializable structs, and a `NoMemoryPack` variant is available for projects that need to avoid the MemoryPack dependency. |
| 19 | +- **Deterministic fixed-point arithmetic:** Consistent results across platforms with `Fixed64`. |
| 20 | +- **Core math types included:** `Vector2d`, `Vector3d`, `FixedQuaternion`, `Fixed3x3`, and `Fixed4x4`. |
| 21 | +- **Spatial helpers:** `BoundingBox`, `BoundingSphere`, and `BoundingArea` for lightweight bounds checks. |
| 22 | +- **Shared math utilities:** Common math and trigonometry helpers via `FixedMath` and `FixedTrigonometry`. |
| 23 | +- **Deterministic RNG:** `DeterministicRandom` for repeatable procedural generation and simulations. |
| 24 | +- **Flexible packaging:** Use the default package with `MemoryPack`, or the `NoMemoryPack` package when you want the same API without that dependency. |
| 25 | +- **Broad .NET compatibility:** Targets modern .NET while remaining friendly to engine and tooling workflows. |
28 | 26 |
|
29 | 27 | --- |
30 | 28 |
|
31 | 29 | ## 🚀 Installation |
32 | 30 |
|
33 | | -Clone the repository and add it to your project: |
| 31 | +For most .NET projects, start with the standard package: |
34 | 32 |
|
35 | | -### Non-Unity Projects |
36 | | - |
37 | | -1. **Choose the package that fits your runtime**: |
38 | | - - Use `FixedMathSharp` if you want the standard package with built-in `MemoryPack` support. |
39 | | - - Use `FixedMathSharp.NoMemoryPack` if you want the same math library without the `MemoryPack` dependency, such as when integrating with toolchains that do better without MemoryPack-generated code. |
40 | | - |
41 | | -2. **Install via NuGet**: |
42 | | - - Standard package: |
| 33 | +```bash |
| 34 | +dotnet add package FixedMathSharp |
| 35 | +``` |
43 | 36 |
|
44 | | - ```bash |
45 | | - dotnet add package FixedMathSharp |
46 | | - ``` |
| 37 | +### Non-Unity Projects |
47 | 38 |
|
48 | | - - No-MemoryPack package: |
| 39 | +Choose the package that fits your runtime: |
49 | 40 |
|
50 | | - ```bash |
51 | | - dotnet add package FixedMathSharp.NoMemoryPack |
52 | | - ``` |
| 41 | +| Package | Best for | Install | |
| 42 | +| --- | --- | --- | |
| 43 | +| `FixedMathSharp` | Most .NET applications. Includes built-in `MemoryPack` support. | `dotnet add package FixedMathSharp` | |
| 44 | +| `FixedMathSharp.NoMemoryPack` | Projects that want the same math API without a `MemoryPack` dependency, including custom serializer setups and Burst AOT-sensitive workflows. | `dotnet add package FixedMathSharp.NoMemoryPack` | |
53 | 45 |
|
54 | | - - If you're using `FluentAssertions` in your test project, the companion assertions package is available here: |
55 | | - [FixedMathSharp.FluentAssertions](https://www.nuget.org/packages/FixedMathSharp.FluentAssertions) |
| 46 | +If you're using `FluentAssertions` in your test project, the companion assertions package is available here: |
| 47 | +[FixedMathSharp.FluentAssertions](https://www.nuget.org/packages/FixedMathSharp.FluentAssertions) |
56 | 48 |
|
57 | | -3. **Or Download/Clone**: |
58 | | - - Clone the repository or download the source code. |
| 49 | +### Build From Source |
59 | 50 |
|
60 | | - ```bash |
61 | | - git clone https://github.com/mrdav30/FixedMathSharp.git |
62 | | - ``` |
| 51 | +Clone the repository and build locally: |
63 | 52 |
|
64 | | -4. **Add to Project**: |
| 53 | +```bash |
| 54 | +git clone https://github.com/mrdav30/FixedMathSharp.git |
| 55 | +dotnet restore |
| 56 | +dotnet build --configuration Debug --no-restore |
| 57 | +``` |
65 | 58 |
|
66 | | - - Include the FixedMathSharp project or its DLLs in your build process. |
| 59 | +You can also reference the project directly or consume the generated package artifacts in your own build process. |
67 | 60 |
|
68 | 61 | ### Package Variants |
69 | 62 |
|
70 | | -FixedMathSharp is published in two build variants so you can choose between convenience and maximum compatibility: |
71 | | -
|
72 | | -- `FixedMathSharp` |
73 | | - Includes `MemoryPack` and its generated serialization support. This is the best default choice for most .NET applications. |
74 | | -- `FixedMathSharp.NoMemoryPack` |
75 | | - Excludes the `MemoryPack` package and uses internal shim attributes so the same source can compile without the dependency. Choose this when you do not need built-in MemoryPack serialization, when you prefer to use a different serializer, or when your target environment is sensitive to MemoryPack-generated code paths. |
76 | | -
|
77 | | -Both variants expose the same core fixed-point math API. The main difference is whether `MemoryPack` is part of the package and serialization surface. |
| 63 | +The published NuGet packages map directly to the source-build configurations below. |
78 | 64 |
|
79 | 65 | If you build from source, the repository also provides matching release configurations: |
80 | 66 |
|
81 | 67 | - `Release` builds the standard `FixedMathSharp` package and archives. |
82 | 68 | - `ReleaseNoMemoryPack` builds the `FixedMathSharp.NoMemoryPack` package and archives. |
83 | 69 |
|
84 | | -If you use Unity Burst AOT, prefer the `NoMemoryPack` build. `MemoryPack`'s Unity support is centered on IL2CPP via its .NET Source Generator path, so the no-MemoryPack variant is the safer choice for Burst AOT scenarios. |
| 70 | +If you use Unity Burst AOT, prefer the `NoMemoryPack` variant. |
85 | 71 |
|
86 | 72 | ### Unity Integration |
87 | 73 |
|
88 | | -FixedMathSharp is now maintained as a separate Unity package. For Unity-specific implementations, refer to: |
| 74 | +FixedMathSharp is maintained as a separate Unity package. For Unity-specific implementations, refer to: |
89 | 75 |
|
90 | 76 | 🔗 [FixedMathSharp-Unity Repository](https://github.com/mrdav30/FixedMathSharp-Unity). |
91 | 77 |
|
92 | | -If you are evaluating the .NET package directly for Unity-adjacent tooling, the `NoMemoryPack` variant is the safer starting point when you want to avoid the MemoryPack dependency entirely. In particular, if you use Burst AOT, prefer `FixedMathSharp.NoMemoryPack`. |
| 78 | +If you are evaluating this .NET package for Unity-adjacent tooling using Burst AOT, prefer `FixedMathSharp.NoMemoryPack`. |
93 | 79 |
|
94 | 80 | --- |
95 | 81 |
|
@@ -180,40 +166,38 @@ int loot = rngOre.Next(1, 5); // [1,5) |
180 | 166 |
|
181 | 167 | ## 📦 Library Structure |
182 | 168 |
|
183 | | -- **`Fixed64` Struct:** Represents fixed-point numbers for precise arithmetic. |
184 | | -- **`Vector2d` and `Vector3d` Structs:** Handle 2D and 3D vector operations. |
185 | | -- **`FixedQuaternion` Struct:** Provides rotation handling without gimbal lock, enabling smooth rotations and quaternion-based transformations. |
186 | | -- **`IBound` Interface:** Standard interface for bounding shapes `BoundingBox`, `BoundingArea`, and `BoundingSphere`, each offering intersection, containment, and projection logic. |
187 | | -- **`FixedMath` Static Class:** Provides common math and trigonometric functions using fixed-point math. |
188 | | -- **`Fixed4x4` and `Fixed3x3`:** Support matrix operations for transformations. |
189 | | -- **`DeterministicRandom` Struct:** Seedable, allocation-free RNG for repeatable procedural generation. |
| 169 | +- **`Fixed64` Struct:** Core Q32.32 fixed-point scalar type. |
| 170 | +- **`Vector2d` and `Vector3d` Structs:** 2D and 3D vector math. |
| 171 | +- **`FixedQuaternion` Struct:** Deterministic quaternion rotations. |
| 172 | +- **`Fixed4x4` and `Fixed3x3`:** Matrix math for transforms and orientation. |
| 173 | +- **`IBound` Interface and bounds types:** `BoundingBox`, `BoundingArea`, and `BoundingSphere` for intersection, containment, and projection queries. |
| 174 | +- **`FixedMath` and `FixedTrigonometry`:** Shared numeric and trigonometric helpers. |
| 175 | +- **`DeterministicRandom` Struct:** Seedable, allocation-free RNG for repeatable procedural generation. |
190 | 176 |
|
191 | 177 | ### Fixed64 Struct |
192 | 178 |
|
193 | | -**Fixed64** is the core data type representing fixed-point numbers. It provides various mathematical operations, including addition, subtraction, multiplication, division, and more. |
194 | | -The struct guarantees deterministic behavior by using integer-based arithmetic with a configurable `SHIFT_AMOUNT`. |
| 179 | +`Fixed64` is the center of the library: a deterministic fixed-point number type backed by integer arithmetic. It is the type used throughout the vector, matrix, quaternion, bounds, and helper APIs. |
195 | 180 |
|
196 | 181 | --- |
197 | 182 |
|
198 | 183 | ## ⚡ Performance Considerations |
199 | 184 |
|
200 | 185 | FixedMathSharp is optimized for high-performance deterministic calculations: |
201 | 186 |
|
202 | | -- **Inline methods and bit-shifting optimizations** ensure minimal overhead. |
203 | | -- **Eliminates floating-point drift**, making it ideal for lockstep simulations. |
204 | | -- **Supports fuzzy equality comparisons** for handling minor precision deviations. |
| 187 | +- **Inline methods and bit-shifting optimizations** keep hot paths lightweight. |
| 188 | +- **Deterministic arithmetic** avoids floating-point drift in lockstep or replay-driven systems. |
| 189 | +- **Fuzzy equality helpers** are available where precision tolerances are useful. |
205 | 190 |
|
206 | 191 | --- |
207 | 192 |
|
208 | 193 | ## 🧪 Testing and Validation |
209 | 194 |
|
210 | | -Unit tests are used extensively to validate the correctness of mathematical operations. |
211 | | -Special **fuzzy comparisons** are employed where small precision discrepancies might occur, mimicking floating-point behavior. |
| 195 | +The library is covered by xUnit tests for core arithmetic, vectors, bounds, serialization, and deterministic random behavior. Fuzzy comparisons are used where a tolerance-based check is more appropriate than exact equality. |
212 | 196 |
|
213 | 197 | To run the tests: |
214 | 198 |
|
215 | 199 | ```bash |
216 | | -dotnet test --configuration debug |
| 200 | +dotnet test --configuration Debug |
217 | 201 | ``` |
218 | 202 |
|
219 | 203 | --- |
|
0 commit comments