-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from Tornado-Technology/transform-implementation
Transform implementation
- Loading branch information
Showing
22 changed files
with
862 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,56 @@ | ||
using Hypercube.Shared.Math.Matrix; | ||
using Hypercube.Shared.Math; | ||
using Hypercube.Shared.Math.Matrix; | ||
using Hypercube.Shared.Math.Transform; | ||
using Hypercube.Shared.Math.Vector; | ||
|
||
namespace Hypercube.Client.Graphics.Viewports; | ||
|
||
public class Camera2D : ICamera | ||
public sealed class Camera2D : ICamera | ||
{ | ||
public Vector3 Position { get; private set; } | ||
public Vector3 Rotation { get; private set; } | ||
public Vector3 Scale { get; private set; } = Vector3.One; | ||
public Matrix4X4 Projection { get; private set; } | ||
|
||
public Vector3 Position => _transform.Position; | ||
public Vector3 Rotation => _transform.Rotation.ToEuler(); | ||
public Vector3 Scale => _transform.Scale; | ||
public Vector2Int Size { get; private set; } | ||
|
||
private readonly float _zFar; | ||
private readonly float _zNear; | ||
private Vector2Int Size { get; set; } | ||
|
||
|
||
private Vector2 HalfSize => Size / 2f; | ||
public Matrix4X4 Projection { get; private set; } | ||
private Transform3 _transform = new(); | ||
|
||
public Camera2D(Vector2Int size, Vector2 position, float zNear, float zFar) | ||
{ | ||
Size = size; | ||
Position = new Vector3(position); | ||
_zNear = zNear; | ||
_zFar = zFar; | ||
|
||
SetPosition(new Vector3(position)); | ||
|
||
UpdateProjection(); | ||
} | ||
|
||
public void SetSize(Vector2Int size) | ||
{ | ||
Size = size; | ||
UpdateProjection(); | ||
} | ||
|
||
public void SetPosition(Vector3 position) | ||
{ | ||
Position = position; | ||
_transform.SetPosition(position); | ||
UpdateProjection(); | ||
} | ||
|
||
public void SetRotation(Vector3 rotation) | ||
{ | ||
Rotation = Rotation.WithZ(rotation.Z); | ||
_transform.SetRotation(Quaternion.FromEuler(0, 0, rotation.Z)); | ||
UpdateProjection(); | ||
} | ||
|
||
public void SetScale(Vector3 scale) | ||
{ | ||
Scale = scale; | ||
_transform.SetScale(scale); | ||
UpdateProjection(); | ||
} | ||
|
||
private void UpdateProjection() | ||
{ | ||
var projection = Matrix4X4.CreateOrthographic(Size, _zNear, _zFar); | ||
|
||
var translate = Matrix4X4.CreateTranslation(Position); | ||
var rotation = Matrix4X4.CreateRotationZ(Rotation.Z); | ||
var scale = Matrix4X4.CreateScale(Scale); | ||
|
||
Projection = projection * translate * rotation * scale; | ||
Projection = projection * _transform.Matrix; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,120 @@ | ||
using System.Runtime.CompilerServices; | ||
using Hypercube.Shared.Math.Extensions; | ||
using Hypercube.Shared.Math.Vector; | ||
|
||
namespace Hypercube.Shared.Math; | ||
|
||
public readonly struct Angle(double theta) | ||
public readonly struct Angle : IEquatable<Angle>, IEquatable<double> | ||
{ | ||
public static readonly Angle Zero = new(0); | ||
|
||
public readonly double Theta; | ||
|
||
public double Degrees | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => Theta * HyperMath.RadiansToDegrees; | ||
} | ||
|
||
public Vector2 Vector | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => new((float)System.Math.Cos(Theta), (float)System.Math.Sin(Theta)); | ||
} | ||
|
||
public Angle(double theta) | ||
{ | ||
Theta = theta; | ||
} | ||
|
||
public Angle(Vector2 vector2) | ||
{ | ||
vector2 = vector2.Normalized; | ||
Theta = System.Math.Atan2(vector2.X, vector2.Y); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool Equals(Angle other) | ||
{ | ||
return Theta.AboutEquals(other.Theta); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool Equals(double other) | ||
{ | ||
return Theta.AboutEquals(other); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public override bool Equals(object? obj) | ||
{ | ||
return (obj is double theta && Equals(theta)) || | ||
(obj is Angle angle && Equals(angle)); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public override int GetHashCode() | ||
{ | ||
return Theta.GetHashCode(); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public override string ToString() | ||
{ | ||
return $"{Degrees} deg"; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool operator ==(Angle a, double b) | ||
{ | ||
return a.Equals(b); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool operator !=(Angle a, double b) | ||
{ | ||
return !a.Equals(b); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool operator ==(Angle a, Angle b) | ||
{ | ||
return a.Equals(b); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool operator !=(Angle a, Angle b) | ||
{ | ||
return !a.Equals(b); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator double(Angle angle) | ||
{ | ||
return angle.Theta; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator Angle(double radians) | ||
{ | ||
return new Angle(radians); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator Angle(float radians) | ||
{ | ||
return new Angle(radians); | ||
} | ||
|
||
public readonly double Theta = theta; | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static Angle FromDegrees(double degrees) | ||
{ | ||
return new Angle(degrees * HyperMath.DegreesToRadians); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator float(Angle angle) | ||
public static Angle FromDegrees(float degrees) | ||
{ | ||
return (float)angle.Theta; | ||
return new Angle(degrees * HyperMath.DegreesToRadians); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Hypercube.Shared.Math; | ||
|
||
public static class HyperMath | ||
{ | ||
public const double PI = System.Math.PI; | ||
|
||
public const double PIOver2 = PI / 2; | ||
public const double PIOver4 = PI / 4; | ||
public const double PIOver6 = PI / 6; | ||
|
||
public const double TwoPI = 2 * PI; | ||
public const double ThreePiOver2 = 3 * PI / 2; | ||
|
||
public const double RadiansToDegrees = 180 / PI; | ||
public const double DegreesToRadians = PI / 180; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Hypercube.Shared.Math; | ||
|
||
public static class HyperMathF | ||
{ | ||
public const float PI = MathF.PI; | ||
|
||
public const float PIOver2 = PI / 2; | ||
public const float PIOver4 = PI / 4; | ||
public const float PIOver6 = PI / 6; | ||
|
||
public const float TwoPI = 2 * PI; | ||
public const float ThreePiOver2 = 3 * PI / 2; | ||
|
||
public const float RadiansToDegrees = 180 / PI; | ||
public const float DegreesToRadians = PI / 180; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.