Skip to content

Commit

Permalink
Replace Matrix2 with Matrix3x2 (#870)
Browse files Browse the repository at this point in the history
* Replace `Matrix2` with `Matrix3x2`

* Update benchmarks

* Add XML documentation for `Matrix3x2`
  • Loading branch information
AristurtleDev authored May 27, 2024
1 parent d008b1b commit 49038f5
Show file tree
Hide file tree
Showing 23 changed files with 1,447 additions and 1,150 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
################################################################################
### Build artifacts directory
################################################################################
.artifacts/
*.[Aa]rtifacts/

################################################################################
### OS specific auto generated files
Expand Down
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</ItemGroup>

<!-- Setup Code Analysis using the .editorconfig file -->
<PropertyGroup>
<!-- <PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<AnalysisLevel>latest</AnalysisLevel>
Expand All @@ -41,6 +41,6 @@
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>
</PropertyGroup> -->

</Project>
22 changes: 22 additions & 0 deletions benchmarks/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>

<Import Project="$(MSBuildThisFileDirectory)..\Directory.Build.props" />

<PropertyGroup>
<ArtifactsPath>$(SolutionDirectory).artifacts/benchmarks</ArtifactsPath>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<IsPackable>false</IsPackable>
<NoWarn>NU1701</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL"
Version="3.8.1.303"
PrivateAssets="All" />
<PackageReference Include="BenchmarkDotNet"
Version="0.13.12" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.10" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\cs\MonoGame.Extended.Collisions\MonoGame.Extended.Collisions.csproj" />
</ItemGroup>
Expand Down
195 changes: 195 additions & 0 deletions benchmarks/MonoGame.Extended.Benchmarks/Matrix3x2Benchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using Microsoft.Xna.Framework;

namespace MonoGame.Extended.Benchmarks;

[MemoryDiagnoser]
public class Matrix3x2Benchmarks
{
private Matrix2 _matrix2;
private Matrix3x2 _matrix3x2;

[GlobalSetup]
public void Setup()
{
_matrix2 = new Matrix2(1, 2, 3, 4, 5, 6);
_matrix3x2 = new Matrix3x2(1, 2, 3, 4, 5, 6);
}

[Benchmark]
public Vector2 Matrix2_getTranslation() => _matrix2.Translation;

[Benchmark]
public Vector2 Matrix3x2_getTranslation() => _matrix3x2.Translation;

// [Benchmark]
// public float Matrix2_getRotation() => _matrix2.Rotation;

// [Benchmark]
// public float Matrix3x2_getRotation() => _matrix3x2.Rotation;

// [Benchmark]
// public Vector2 Matrix2_getScale() => _matrix2.Scale;

// [Benchmark]
// public Vector2 Matrix3x2_getScale() => _matrix3x2.Scale;

// [Benchmark]
// public (Vector2, float, Vector2) Matrix2_Decompose()
// {
// Vector2 translation = _matrix2.Translation;
// float rotation = _matrix2.Rotation;
// Vector2 scale = _matrix2.Scale;
// return (translation, rotation, scale);
// }

// [Benchmark]
// public (Vector2, float, Vector2) Matrix3x2_Decompose()
// {
// _matrix3x2.Decompose(out Vector2 translation, out float rotation, out Vector2 scale);
// return (translation, rotation, scale);
// }

// [Benchmark]
// public Vector2 Matrix2_Transform() => _matrix2.Transform(Vector2.One);

// [Benchmark]
// public Vector2 Matrix3x2_Transform() => _matrix3x2.Transform(Vector2.One);

// [Benchmark]
// public float Matrix2_Determinant() => _matrix2.Determinant();

// [Benchmark]
// public float Matrix3x2_Determinant() => _matrix3x2.Determinant();

// [Benchmark]
// public Matrix2 Matrix2_CreateFrom()
// {
// Matrix2.CreateFrom(Vector2.Zero, 1.0f, Vector2.One, Vector2.Zero, out Matrix2 result);
// return result;
// }

// [Benchmark]
// public Matrix3x2 Matrix3x2_CreateFrom()
// {
// Matrix3x2.CreateFrom(Vector2.Zero, 1.0f, Vector2.One, Vector2.Zero, out Matrix3x2 result);
// return result;
// }

// [Benchmark]
// public Matrix2 Matrix2_CreateRotationZ()
// {
// Matrix2.CreateRotationZ(1.0f, out Matrix2 result);
// return result;
// }

// [Benchmark]
// public Matrix3x2 Matrix3x2_CreateRotationZ()
// {
// Matrix3x2.CreateRotationZ(1.0f, out Matrix3x2 result);
// return result;
// }

// [Benchmark]
// public Matrix2 Matrix2_CreateScale()
// {
// Matrix2.CreateScale(1.0f, out Matrix2 result);
// return result;
// }

// [Benchmark]
// public Matrix3x2 Matrix3x2_CreateScale()
// {
// Matrix3x2.CreateScale(1.0f, out Matrix3x2 result);
// return result;
// }

// [Benchmark]
// public Matrix2 Matrix2_CreateTranslation()
// {
// Matrix2.CreateTranslation(1.0f, 1.0f, out Matrix2 result);
// return result;
// }

// [Benchmark]
// public Matrix3x2 Matrix3x2_CreateTranslation()
// {
// Matrix3x2.CreateTranslation(1.0f, 1.0f, out Matrix3x2 result);
// return result;
// }

// [Benchmark]
// public Matrix2 Matrix2_Invert()
// {
// Matrix2.Invert(ref _matrix2, out Matrix2 result);
// return result;
// }

// [Benchmark]
// public Matrix3x2 Matrix3x2_Invert()
// {
// Matrix3x2.Invert(_matrix3x2);
// return _matrix3x2;
// }

// [Benchmark]
// public Matrix2 Matrix2_Add()
// {
// return Matrix2.Add(_matrix2, _matrix2);
// }

// [Benchmark]
// public Matrix3x2 Matrix3x2_Add()
// {
// return Matrix3x2.Add(_matrix3x2, _matrix3x2);
// }

// [Benchmark]
// public Matrix2 Matrix2_Subtract()
// {
// return Matrix2.Subtract(_matrix2, _matrix2);
// }

// [Benchmark]
// public Matrix3x2 Matrix3x2_Subtract()
// {
// return Matrix3x2.Subtract(_matrix3x2, _matrix3x2);
// }

// [Benchmark]
// public Matrix2 Matrix2_Multiply()
// {
// return Matrix2.Subtract(_matrix2, _matrix2);
// }

// [Benchmark]
// public Matrix3x2 Matrix3x2_Multiply()
// {
// return Matrix3x2.Multiply(_matrix3x2, _matrix3x2);
// }

// [Benchmark]
// public Matrix2 Matrix2_Divide()
// {
// return Matrix2.Divide(_matrix2, _matrix2);
// }

// [Benchmark]
// public Matrix3x2 Matrix3x2_Divide()
// {
// return Matrix3x2.Divide(_matrix3x2, _matrix3x2);
// }

// [Benchmark]
// public Matrix Matrix2_ToMatrix()
// {
// return _matrix2.ToMatrix();
// }

// [Benchmark]
// public Matrix Matrix3x2_ToMatrix()
// {
// return _matrix3x2.ToMatrix();
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\source\MonoGame.Extended\MonoGame.Extended.csproj" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions benchmarks/MonoGame.Extended.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using BenchmarkDotNet.Running;
using MonoGame.Extended.Benchmarks;


BenchmarkRunner.Run<Matrix3x2Benchmarks>();

Console.WriteLine("finished");
4 changes: 2 additions & 2 deletions source/MonoGame.Extended.Collisions/CollisionComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,13 @@ private static Vector2 PenetrationVector(CircleF circ, RectangleF rect)

private static Vector2 PenetrationVector(CircleF circleA, OrientedRectangle orientedRectangleB)
{
var rotation = Matrix2.CreateRotationZ(orientedRectangleB.Orientation.Rotation);
var rotation = Matrix3x2.CreateRotationZ(orientedRectangleB.Orientation.Rotation);
var circleCenterInRectangleSpace = rotation.Transform(circleA.Center - orientedRectangleB.Center);
var circleInRectangleSpace = new CircleF(circleCenterInRectangleSpace, circleA.Radius);
var boundingRectangle = new BoundingRectangle(new Point2(), orientedRectangleB.Radii);

var penetrationVector = PenetrationVector(circleInRectangleSpace, boundingRectangle);
var inverseRotation = Matrix2.CreateRotationZ(-orientedRectangleB.Orientation.Rotation);
var inverseRotation = Matrix3x2.CreateRotationZ(-orientedRectangleB.Orientation.Rotation);
var transformedPenetration = inverseRotation.Transform(penetrationVector);

return transformedPenetration;
Expand Down
Loading

0 comments on commit 49038f5

Please sign in to comment.