From 7bffd014010e19f614ba35f5fa1e6e97c28bfac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Tue, 5 Sep 2023 23:15:33 +0200 Subject: [PATCH] Rigidbody2D Get/Set Linear and Angular Velocity --- GlitchyEngine/src/Scripting/ScriptGlue.bf | 40 ++++++++++++ .../src/World/Components/Components.bf | 61 +++++++++++++++++++ ScriptCore/Components/RigidBody2D.cs | 28 +++++++++ ScriptCore/ScriptGlue.cs | 12 ++++ 4 files changed, 141 insertions(+) diff --git a/GlitchyEngine/src/Scripting/ScriptGlue.bf b/GlitchyEngine/src/Scripting/ScriptGlue.bf index ff20f7d..b6db8e3 100644 --- a/GlitchyEngine/src/Scripting/ScriptGlue.bf +++ b/GlitchyEngine/src/Scripting/ScriptGlue.bf @@ -300,6 +300,46 @@ static class ScriptGlue rotation = rigidBody.GetAngle(); } + [RegisterCall("ScriptGlue::Rigidbody2D_GetLinearVelocity")] + static void Rigidbody2D_GetLinearVelocity(UUID entityId, out float2 velocity) + { + Scene scene = ScriptEngine.Context; + Entity entity = scene.GetEntityByID(entityId); + + var rigidBody = entity.GetComponent(); + velocity = rigidBody.GetLinearVelocity(); + } + + [RegisterCall("ScriptGlue::Rigidbody2D_SetLinearVelocity")] + static void Rigidbody2D_SetLinearVelocity(UUID entityId, in float2 velocity) + { + Scene scene = ScriptEngine.Context; + Entity entity = scene.GetEntityByID(entityId); + + var rigidBody = entity.GetComponent(); + rigidBody.SetLinearVelocity(velocity); + } + + [RegisterCall("ScriptGlue::Rigidbody2D_GetAngularVelocity")] + static void Rigidbody2D_GetAngularVelocity(UUID entityId, out float velocity) + { + Scene scene = ScriptEngine.Context; + Entity entity = scene.GetEntityByID(entityId); + + var rigidBody = entity.GetComponent(); + velocity = rigidBody.GetAngularVelocity(); + } + + [RegisterCall("ScriptGlue::Rigidbody2D_SetAngularVelocity")] + static void Rigidbody2D_SetAngularVelocity(UUID entityId, in float velocity) + { + Scene scene = ScriptEngine.Context; + Entity entity = scene.GetEntityByID(entityId); + + var rigidBody = entity.GetComponent(); + rigidBody.SetAngularVelocity(velocity); + } + #endregion Rigidbody2D #region Physics2D diff --git a/GlitchyEngine/src/World/Components/Components.bf b/GlitchyEngine/src/World/Components/Components.bf index 9979e83..2fe7cf4 100644 --- a/GlitchyEngine/src/World/Components/Components.bf +++ b/GlitchyEngine/src/World/Components/Components.bf @@ -385,6 +385,9 @@ namespace GlitchyEngine.World private int _runtimeBody = 0; + //private float _linearDamping = 0.0f; + //private float _angularDamping = 0.01f; + protected internal b2Body* RuntimeBody { [Inline] @@ -392,6 +395,30 @@ namespace GlitchyEngine.World [Inline] set mut => _runtimeBody = (int)(void*)value; } + + /*public float LinearDamping + { + get => _linearDamping; + set mut + { + _linearDamping = value; + + if (RuntimeBody != null) + Box2D.Body.SetLinearDamping(RuntimeBody, _linearDamping); + } + } + + public float AngularDamping + { + get => _angularDamping; + set mut + { + _angularDamping = value; + + if (RuntimeBody != null) + Box2D.Body.SetAngularDamping(RuntimeBody, _angularDamping); + } + }*/ public float2 GetPosition() { @@ -436,6 +463,40 @@ namespace GlitchyEngine.World Box2D.Body.SetTransform(RuntimeBody, pos, angle); } } + + public float2 GetLinearVelocity() + { + if (RuntimeBody != null) + { + Box2D.b2Vec2 vec = Box2D.Body.GetLinearVelocity(RuntimeBody); + + return *(float2*)&vec; + } + + return .Zero; + } + + public void SetLinearVelocity(float2 velocity) + { + if (RuntimeBody != null) + Box2D.Body.SetLinearVelocity(RuntimeBody, velocity); + } + + public float GetAngularVelocity() + { + if (RuntimeBody != null) + { + return Box2D.Body.GetAngularVelocity(RuntimeBody); + } + + return 0; + } + + public void SetAngularVelocity(float velocity) + { + if (RuntimeBody != null) + Box2D.Body.SetAngularVelocity(RuntimeBody, velocity); + } } /*struct InternalBug diff --git a/ScriptCore/Components/RigidBody2D.cs b/ScriptCore/Components/RigidBody2D.cs index 272ef55..ed0255f 100644 --- a/ScriptCore/Components/RigidBody2D.cs +++ b/ScriptCore/Components/RigidBody2D.cs @@ -55,4 +55,32 @@ public class Rigidbody2D : Component } set => ScriptGlue.Rigidbody2D_SetRotation(Entity._uuid, value); } + + /// + /// Gets or sets the linear velocity of the rigidbody. + /// + public float2 LinearVelocity + { + get + { + ScriptGlue.Rigidbody2D_GetLinearVelocity(Entity._uuid, out float2 velocity); + + return velocity; + } + set => ScriptGlue.Rigidbody2D_SetLinearVelocity(Entity._uuid, value); + } + + /// + /// Gets or sets the angulara velocity of the rigidbody. + /// + public float AngularVelocity + { + get + { + ScriptGlue.Rigidbody2D_GetAngularVelocity(Entity._uuid, out float velocity); + + return velocity; + } + set => ScriptGlue.Rigidbody2D_SetAngularVelocity(Entity._uuid, value); + } } diff --git a/ScriptCore/ScriptGlue.cs b/ScriptCore/ScriptGlue.cs index c98175c..86cdfc3 100644 --- a/ScriptCore/ScriptGlue.cs +++ b/ScriptCore/ScriptGlue.cs @@ -58,6 +58,18 @@ internal static class ScriptGlue [MethodImpl(MethodImplOptions.InternalCall)] internal static extern void Rigidbody2D_GetRotation(UUID entityId, out float rotation); + + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern void Rigidbody2D_SetLinearVelocity(UUID entityId, in float2 velocity); + + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern void Rigidbody2D_GetLinearVelocity(UUID entityId, out float2 velocity); + + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern void Rigidbody2D_SetAngularVelocity(UUID entityId, in float velocity); + + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern void Rigidbody2D_GetAngularVelocity(UUID entityId, out float velocity); #endregion RigidBody2D