From e759d2c44b7836e17f074fd0cb281585ef25f460 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Fri, 15 Mar 2024 15:25:22 +0100 Subject: [PATCH] Simplified transform --- Expansion1/CrosshairGizmoComponent.cs | 43 +++++++++++++++++++++++++++ Expansion1/RectangleGizmoComponent.cs | 10 +++---- Program.cs | 7 +++-- Smoll/Transform2D.cs | 40 +++++++++++++------------ 4 files changed, 74 insertions(+), 26 deletions(-) create mode 100644 Expansion1/CrosshairGizmoComponent.cs diff --git a/Expansion1/CrosshairGizmoComponent.cs b/Expansion1/CrosshairGizmoComponent.cs new file mode 100644 index 0000000..c919761 --- /dev/null +++ b/Expansion1/CrosshairGizmoComponent.cs @@ -0,0 +1,43 @@ +using System.Collections.Specialized; +using System.Numerics; +using Raylib_cs; + +namespace Smoll.Ex1 { + class CrosshairGizmoComponent : Smoll.Component { + Transform2DComponent? ownerTransform; + + public CrosshairGizmoComponent() + {} + + public override void OnAttached() { + ownerTransform = owner.GetComponent(); + } + + public override void Draw(Smoll.Layer.DrawMode drawMode) { + ownerTransform ??= owner.GetComponent(); + ownerTransform ??= new Transform2DComponent(); + var tr = ownerTransform.AbsoluteTransform(); + Raylib.DrawCircleLines((int)tr.position.Real, (int)tr.position.Imaginary, 5.5f, Color.Black); + + Raylib.DrawLine( + (int)tr.position.Real, + (int)tr.position.Imaginary-10, + (int)tr.position.Real, + (int)tr.position.Imaginary+10, + Color.Lime); + Raylib.DrawLine( + (int)tr.position.Real-10, + (int)tr.position.Imaginary, + (int)tr.position.Real+10, + (int)tr.position.Imaginary, + Color.Red); + Raylib.DrawText( + tr.angle.ToString(), + (int)tr.position.Real+10, + (int)tr.position.Imaginary+10, + 14, + Color.Magenta + ); + } + } +} \ No newline at end of file diff --git a/Expansion1/RectangleGizmoComponent.cs b/Expansion1/RectangleGizmoComponent.cs index 25d603e..943b3a5 100644 --- a/Expansion1/RectangleGizmoComponent.cs +++ b/Expansion1/RectangleGizmoComponent.cs @@ -25,14 +25,14 @@ namespace Smoll.Ex1 { new Rectangle( (float)tr.position.Real, (float)tr.position.Imaginary, - (float)(size.Real*tr.scale.Real), - (float)(size.Imaginary*tr.scale.Imaginary) + (float)(size.Real*tr.scale), + (float)(size.Imaginary*tr.scale) ), new Vector2( - (float)(tr.anchor.Real), - (float)(tr.anchor.Imaginary) + (float)(0), + (float)(0) ), - tr.angle, + tr.angle*360f/MathF.PI, color ); } diff --git a/Program.cs b/Program.cs index e80f2e8..b2b8d2f 100644 --- a/Program.cs +++ b/Program.cs @@ -5,13 +5,15 @@ using Smoll.Ex1; Layer layer = new Layer(); var entity = new Entity(layer); -entity.Attach(new Transform2DComponent(128, 128, 0, 64, 32)); +entity.Attach(new Transform2DComponent(128, 128, 0)); entity.Attach(new RectangleGizmoComponent(128, 64, Color.Blue)); entity.Attach(new ExampleInputComponent(100f, 0.25f)); +entity.Attach(new CrosshairGizmoComponent()); var entity2 = new Entity(entity); -entity2.Attach(new Transform2DComponent(128, 128, 0, 32, 32)); +entity2.Attach(new Transform2DComponent(128, 128, 0)); entity2.Attach(new RectangleGizmoComponent(64, 64, Color.Red)); +entity2.Attach(new CrosshairGizmoComponent()); Engine engine = new Engine(); engine.layers.Add(layer); @@ -24,5 +26,6 @@ while(!Raylib.WindowShouldClose()) { engine.Draw(); Raylib.DrawFPS(0,0); Raylib.EndDrawing(); + GC.Collect(0); } Raylib.CloseWindow(); \ No newline at end of file diff --git a/Smoll/Transform2D.cs b/Smoll/Transform2D.cs index 0adbc79..19732d9 100644 --- a/Smoll/Transform2D.cs +++ b/Smoll/Transform2D.cs @@ -4,36 +4,35 @@ namespace Smoll { struct Transform2D { public Complex position; - public Complex anchor; public float angle; - public Complex scale; + public float scale; + + public void ForceInvariant() { + while(angle > 2*MathF.PI) angle -= 2*MathF.PI; + while(angle < 0) angle += 2*MathF.PI; + } } sealed class Transform2DComponent : Component { public Transform2D transform; public Transform2DComponent() - : this(0,0,0,0,0,1,1) + : this(0,0,0,1) {} public Transform2DComponent(float x, float y) - : this(x,y,0,0,0,1,1) + : this(x,y,0,1) {} public Transform2DComponent(float x, float y, float angle) - : this(x,y,angle,0,0,1,1) - {} - - public Transform2DComponent(float x, float y, float angle, float center_x, float center_y) - : this(x,y,angle,center_x,center_y,1,1) + : this(x,y,angle,1) {} - public Transform2DComponent(float x, float y, float angle, float center_x, float center_y, float scale_x, float scale_y) { + public Transform2DComponent(float x, float y, float angle, float scale) { transform = new Transform2D(); transform.position = y*Complex.ImaginaryOne + x; transform.angle = angle; - transform.anchor = center_y*Complex.ImaginaryOne + center_x; - transform.scale = scale_y*Complex.ImaginaryOne + scale_x; + transform.scale = scale; } public Transform2D AbsoluteTransform() { @@ -44,18 +43,21 @@ sealed class Transform2DComponent : Component { var component = up.GetComponent(); if(component != null) { Transform2D combined = component.AbsoluteTransform(); - var unscaled_pos = (transform.position - transform.anchor)*Complex.Exp(Complex.ImaginaryOne * combined.angle) + transform.anchor; - combined.position = combined.scale.Real*unscaled_pos.Real + combined.scale.Imaginary*unscaled_pos.Imaginary*Complex.ImaginaryOne - + combined.position + combined.anchor; - var unscaled_anchor = (transform.anchor - transform.position)*Complex.Exp(Complex.ImaginaryOne * combined.angle) + transform.position; - combined.anchor = transform.scale.Real*unscaled_anchor.Real + transform.scale.Imaginary*unscaled_anchor.Imaginary*Complex.ImaginaryOne; - combined.scale = transform.scale.Imaginary*combined.scale.Imaginary*Complex.ImaginaryOne + transform.scale.Real * combined.scale.Real; + var unscaled_pos = (transform.position)*Complex.Exp(Complex.ImaginaryOne * combined.angle); + combined.position = unscaled_pos + combined.position; + combined.scale *= transform.scale; combined.angle += transform.angle; + combined.ForceInvariant(); return combined; } up = up.parent; } while(true); } -} + + public override void Update(float _) + { + transform.ForceInvariant(); + } + } } \ No newline at end of file