change Transform2D.TransformMatrix to a Matrix3x2

drone
Evan Hemsley 2020-01-01 22:57:57 -08:00
parent 1eee8bd923
commit 7f15e73de3
3 changed files with 21 additions and 8 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
bin/ bin/
obj/ obj/
.vscode .vscode
.vs

View File

@ -16,7 +16,7 @@ namespace MoonTools.Core.Structs
private float _rotation; private float _rotation;
private Vector2 _scale; private Vector2 _scale;
public Matrix4x4 TransformMatrix { get; private set; } public Matrix3x2 TransformMatrix { get; private set; }
public Position2D Position public Position2D Position
{ {
@ -119,11 +119,11 @@ namespace MoonTools.Core.Structs
return new Transform2D(Position + other.Position, Rotation + other.Rotation, Scale * other.Scale); return new Transform2D(Position + other.Position, Rotation + other.Rotation, Scale * other.Scale);
} }
private static Matrix4x4 CreateTransformMatrix(Position2D translation, float rotation, Vector2 scale) private static Matrix3x2 CreateTransformMatrix(Position2D translation, float rotation, Vector2 scale)
{ {
return Matrix4x4.CreateScale(scale.X, scale.Y, 1) * return Matrix3x2.CreateScale(scale.X, scale.Y) *
Matrix4x4.CreateRotationZ(rotation) * Matrix3x2.CreateRotation(rotation) *
Matrix4x4.CreateTranslation(translation.X, translation.Y, 0); Matrix3x2.CreateTranslation(translation.X, translation.Y);
} }
public override bool Equals(Object other) public override bool Equals(Object other)

View File

@ -14,7 +14,7 @@ namespace Tests
var transformA = new Transform2D(new Position2D(0, 1), 4f, new Vector2(2, 1)); var transformA = new Transform2D(new Position2D(0, 1), 4f, new Vector2(2, 1));
var transformB = new Transform2D(new Position2D(0, 1), 4f, new Vector2(2, 1)); var transformB = new Transform2D(new Position2D(0, 1), 4f, new Vector2(2, 1));
transformA.Should().BeEquivalentTo(transformB); transformA.Equals(transformB).Should().BeTrue();
} }
[Test] [Test]
@ -23,7 +23,7 @@ namespace Tests
var transformA = new Transform2D(new Position2D(2, 3)); var transformA = new Transform2D(new Position2D(2, 3));
var transformB = new Transform2D(new Position2D(5, 1)); var transformB = new Transform2D(new Position2D(5, 1));
transformA.Should().NotBeEquivalentTo(transformB); transformA.Equals(transformB).Should().BeFalse();
} }
[Test] [Test]
@ -52,5 +52,17 @@ namespace Tests
transformA.Compose(transformB).Should().BeEquivalentTo(new Transform2D(new Position2D(19, 3), 3 * (float)System.Math.PI / 4, new Vector2(3, 2))); transformA.Compose(transformB).Should().BeEquivalentTo(new Transform2D(new Position2D(19, 3), 3 * (float)System.Math.PI / 4, new Vector2(3, 2)));
} }
[Test]
public void Transform()
{
var transformA = new Transform2D(Position2D.Zero, (float)System.Math.PI / 2, Vector2.One);
var transformB = new Transform2D(new Vector2(0, 2), (float)System.Math.PI, Vector2.One);
var transformC = new Transform2D(new Vector2(-2, 0), (float)System.Math.PI * 2, new Vector2(3, 1));
Vector2.Transform(new Vector2(-1, 0), transformA.TransformMatrix).Should().Be(new Vector2(0, -1));
Vector2.Transform(new Vector2(-1, 0), transformB.TransformMatrix).Should().Be(new Vector2(1, 2));
Vector2.Transform(new Vector2(-1, 0), transformC.TransformMatrix).Should().Be(new Vector2(-5, 0));
}
} }
} }