Rigidbody2D Get/Set Linear and Angular Velocity

This commit is contained in:
Simon Lübeß
2023-09-05 23:15:33 +02:00
parent cd333f4f02
commit 7bffd01401
4 changed files with 141 additions and 0 deletions
+40
View File
@@ -300,6 +300,46 @@ static class ScriptGlue
rotation = rigidBody.GetAngle(); 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<Rigidbody2DComponent>();
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<Rigidbody2DComponent>();
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<Rigidbody2DComponent>();
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<Rigidbody2DComponent>();
rigidBody.SetAngularVelocity(velocity);
}
#endregion Rigidbody2D #endregion Rigidbody2D
#region Physics2D #region Physics2D
@@ -385,6 +385,9 @@ namespace GlitchyEngine.World
private int _runtimeBody = 0; private int _runtimeBody = 0;
//private float _linearDamping = 0.0f;
//private float _angularDamping = 0.01f;
protected internal b2Body* RuntimeBody protected internal b2Body* RuntimeBody
{ {
[Inline] [Inline]
@@ -392,6 +395,30 @@ namespace GlitchyEngine.World
[Inline] [Inline]
set mut => _runtimeBody = (int)(void*)value; 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() public float2 GetPosition()
{ {
@@ -436,6 +463,40 @@ namespace GlitchyEngine.World
Box2D.Body.SetTransform(RuntimeBody, pos, angle); 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 /*struct InternalBug
+28
View File
@@ -55,4 +55,32 @@ public class Rigidbody2D : Component
} }
set => ScriptGlue.Rigidbody2D_SetRotation(Entity._uuid, value); set => ScriptGlue.Rigidbody2D_SetRotation(Entity._uuid, value);
} }
/// <summary>
/// Gets or sets the linear velocity of the rigidbody.
/// </summary>
public float2 LinearVelocity
{
get
{
ScriptGlue.Rigidbody2D_GetLinearVelocity(Entity._uuid, out float2 velocity);
return velocity;
}
set => ScriptGlue.Rigidbody2D_SetLinearVelocity(Entity._uuid, value);
}
/// <summary>
/// Gets or sets the angulara velocity of the rigidbody.
/// </summary>
public float AngularVelocity
{
get
{
ScriptGlue.Rigidbody2D_GetAngularVelocity(Entity._uuid, out float velocity);
return velocity;
}
set => ScriptGlue.Rigidbody2D_SetAngularVelocity(Entity._uuid, value);
}
} }
+12
View File
@@ -58,6 +58,18 @@ internal static class ScriptGlue
[MethodImpl(MethodImplOptions.InternalCall)] [MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Rigidbody2D_GetRotation(UUID entityId, out float rotation); 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 #endregion RigidBody2D