From a06766b72b300769cf1700e69468e413c05ad510 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Sat, 16 Mar 2024 22:32:26 +0100 Subject: [PATCH] Fixed Inertia --- Expansion1/ExampleInputComponent.cs | 7 ++++--- Expansion2/ExampleShootingInputComponent.cs | 2 +- Expansion2/InertiaComponent.cs | 23 ++++++--------------- Program.cs | 5 +++-- 4 files changed, 14 insertions(+), 23 deletions(-) diff --git a/Expansion1/ExampleInputComponent.cs b/Expansion1/ExampleInputComponent.cs index be6c937..23ccac2 100644 --- a/Expansion1/ExampleInputComponent.cs +++ b/Expansion1/ExampleInputComponent.cs @@ -1,11 +1,12 @@ using Smoll; using Raylib_cs; using System.Numerics; +using Smoll.Ex2; namespace Smoll.Ex1 { class ExampleInputComponent : Component { - Transform2DComponent? transform; + InertiaComponent? transform; public float speedPixelPerSecond; public float angularSpeedTurnPerSecond; @@ -18,12 +19,12 @@ namespace Smoll.Ex1 public override void OnAttached() { base.OnAttached(); - transform = owner.GetComponent(); + transform = owner.GetComponent(); } public override void Update(float deltaTimeSeconds) { - transform ??= owner.GetComponent(); + transform ??= owner.GetComponent(); if(transform == null) throw new Exception("Example Input used on immovable object"); transform.transform.position -= Raylib.IsKeyDown(KeyboardKey.Up) * speedPixelPerSecond * deltaTimeSeconds * Complex.ImaginaryOne; transform.transform.position += Raylib.IsKeyDown(KeyboardKey.Down) * speedPixelPerSecond * deltaTimeSeconds * Complex.ImaginaryOne; diff --git a/Expansion2/ExampleShootingInputComponent.cs b/Expansion2/ExampleShootingInputComponent.cs index 3e25f5f..bf26712 100644 --- a/Expansion2/ExampleShootingInputComponent.cs +++ b/Expansion2/ExampleShootingInputComponent.cs @@ -33,7 +33,7 @@ namespace Smoll.Ex2 shoot.Attach(new Transform2DComponent((float)details.Item1.Real, (float)details.Item1.Imaginary, selfTransform.angle, selfTransform.scale)); shoot.Attach(new TimeToLiveComponent(4f)); shoot.Attach(new SpriteComponent("Beam.png")); - shoot.Attach(new InertiaComponent(1.0f, 0.001f*(float)details.Item2.Real, 0.001f*(float)details.Item2.Imaginary)); + shoot.Attach(new InertiaComponent(1.0f, (float)details.Item2.Real, (float)details.Item2.Imaginary)); } } } diff --git a/Expansion2/InertiaComponent.cs b/Expansion2/InertiaComponent.cs index 0d84424..7db2886 100644 --- a/Expansion2/InertiaComponent.cs +++ b/Expansion2/InertiaComponent.cs @@ -4,7 +4,6 @@ namespace Smoll.Ex2 { sealed class InertiaComponent : Component { public Transform2D transform; public float dampening; - public float minimalDampening = 2.0f; private Transform2DComponent? ownerTransform; public InertiaComponent(float dampening = 1) @@ -37,22 +36,12 @@ namespace Smoll.Ex2 { { ownerTransform ??= owner.GetComponent(); ownerTransform ??= new Transform2DComponent(); - ownerTransform.transform.position += transform.position; - ownerTransform.transform.angle += transform.angle; - - if(dampening != 1) { - if(transform.position.Magnitude > minimalDampening) { - transform.position *= dampening; - } else { - transform.position *= 0; - } - - if(transform.angle > minimalDampening) { - transform.angle *= dampening; - } else { - transform.angle *= 0; - } - } + ownerTransform.transform.position += transform.position * deltaTimeSeconds; + ownerTransform.transform.angle += transform.angle * deltaTimeSeconds; + + transform.position *= dampening; + transform.angle *= dampening; + } } diff --git a/Program.cs b/Program.cs index 78a1d95..8d25816 100644 --- a/Program.cs +++ b/Program.cs @@ -19,7 +19,8 @@ var player = new Entity(layer); debugLayer.Attach(player); player.Attach(new Transform2DComponent(128, 128, 0, 1)); player.Attach(new CrosshairGizmoComponent()); -player.Attach(new ExampleInputComponent(100f, 0.25f)); +player.Attach(new ExampleInputComponent(250f, 0.35f)); +player.Attach(new InertiaComponent(0.999f)); var entity = new Entity(player); entity.Attach(new Transform2DComponent(-16, -16, 0, 4)); @@ -31,7 +32,7 @@ var entity2 = new Entity(entity); entity2.Attach(new Transform2DComponent(5, -10, 0)); entity2.Attach(new SpriteComponent("Laser.png")); var arrow = new ArrowComponent(2, 0); -arrow.rawValue = new System.Numerics.Complex(0, -12); +arrow.rawValue = new System.Numerics.Complex(0, -48); entity2.Attach(arrow); entity2.Attach(new ExampleShootingInputComponent());