From 0c93ed43f7825e6a55ad48252a18be2696ee18dc Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Fri, 5 Jul 2024 00:54:44 -0700 Subject: [PATCH 1/5] Adds MassDataChangedEvent to physics This event is raised in response to changes to an entities innate mass/angular inertia/center of mass --- .../Physics/Events/MassChangedEvent.cs | 31 +++++++++++++++++++ .../Systems/SharedPhysicsSystem.Components.cs | 9 ++++++ 2 files changed, 40 insertions(+) create mode 100644 Robust.Shared/Physics/Events/MassChangedEvent.cs diff --git a/Robust.Shared/Physics/Events/MassChangedEvent.cs b/Robust.Shared/Physics/Events/MassChangedEvent.cs new file mode 100644 index 00000000000..dcd2e4639bc --- /dev/null +++ b/Robust.Shared/Physics/Events/MassChangedEvent.cs @@ -0,0 +1,31 @@ +using Robust.Shared.GameObjects; +using Robust.Shared.Physics.Components; +using System.Numerics; + +namespace Robust.Shared.Physics.Events; + +/// +/// By-ref directed event raised when the mass or angular inertia or center of mass of a physics body changes. +/// +/// The physics body that changed. +/// The new mass of the physics body. +/// The new angular inertia of the physics body. +/// The new (local) center of mass of the physics body. +/// The old mass of the physics body. +/// The old angular inertia of the physics body. +/// The old (local) center of mass of the physics body. +[ByRefEvent] +public readonly record struct MassDataChangedEvent( + Entity Entity, + float NewMass, + float NewInertia, + Vector2 NewCenter, + float OldMass, + float OldInertia, + Vector2 OldCenter +) +{ + public readonly bool MassChanged = NewMass != OldMass; + public readonly bool InertiaChanged = NewInertia != OldInertia; + public readonly bool CenterChanged = NewCenter != OldCenter; +} diff --git a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Components.cs b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Components.cs index 73e1b891f04..e4754ab28e0 100644 --- a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Components.cs +++ b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Components.cs @@ -256,6 +256,9 @@ public void ResetMassData(EntityUid uid, FixturesComponent? manager = null, Phys if (!_fixturesQuery.Resolve(uid, ref manager)) return; + var oldMass = body._mass; + var oldInertia = body._inertia; + body._mass = 0.0f; body._invMass = 0.0f; body._inertia = 0.0f; @@ -314,6 +317,12 @@ public void ResetMassData(EntityUid uid, FixturesComponent? manager = null, Phys // Update center of mass velocity. body.LinearVelocity += Vector2Helpers.Cross(body.AngularVelocity, localCenter - oldCenter); Dirty(uid, body); + + if (body._mass == oldMass && body._inertia == oldInertia && oldCenter == localCenter) + return; + + var ev = new MassDataChangedEvent((uid, body, manager), body._mass, body._inertia, localCenter, oldMass, oldInertia, oldCenter); + RaiseLocalEvent(uid, ref ev); } public bool SetAngularVelocity(EntityUid uid, float value, bool dirty = true, FixturesComponent? manager = null, PhysicsComponent? body = null) From 06ae650517a75b1cc3d42818cff4c3369331920f Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Fri, 5 Jul 2024 23:38:22 -0700 Subject: [PATCH 2/5] Use properties to fetch data from component --- Robust.Shared/Physics/Events/MassChangedEvent.cs | 15 ++++++--------- .../Systems/SharedPhysicsSystem.Components.cs | 2 +- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/Robust.Shared/Physics/Events/MassChangedEvent.cs b/Robust.Shared/Physics/Events/MassChangedEvent.cs index dcd2e4639bc..2094f5d22e8 100644 --- a/Robust.Shared/Physics/Events/MassChangedEvent.cs +++ b/Robust.Shared/Physics/Events/MassChangedEvent.cs @@ -8,24 +8,21 @@ namespace Robust.Shared.Physics.Events; /// By-ref directed event raised when the mass or angular inertia or center of mass of a physics body changes. /// /// The physics body that changed. -/// The new mass of the physics body. -/// The new angular inertia of the physics body. -/// The new (local) center of mass of the physics body. /// The old mass of the physics body. /// The old angular inertia of the physics body. /// The old (local) center of mass of the physics body. [ByRefEvent] public readonly record struct MassDataChangedEvent( Entity Entity, - float NewMass, - float NewInertia, - Vector2 NewCenter, float OldMass, float OldInertia, Vector2 OldCenter ) { - public readonly bool MassChanged = NewMass != OldMass; - public readonly bool InertiaChanged = NewInertia != OldInertia; - public readonly bool CenterChanged = NewCenter != OldCenter; + public float NewMass => Entity.Comp._mass; + public float NewInertia => Entity.Comp._inertia; + public float NewCenter => Entity.Comp._localCenter; + public bool MassChanged => NewMass != OldMass; + public bool InertiaChanged => NewInertia != OldInertia; + public bool CenterChanged => NewCenter != OldCenter; } diff --git a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Components.cs b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Components.cs index e4754ab28e0..adecf575f70 100644 --- a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Components.cs +++ b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Components.cs @@ -321,7 +321,7 @@ public void ResetMassData(EntityUid uid, FixturesComponent? manager = null, Phys if (body._mass == oldMass && body._inertia == oldInertia && oldCenter == localCenter) return; - var ev = new MassDataChangedEvent((uid, body, manager), body._mass, body._inertia, localCenter, oldMass, oldInertia, oldCenter); + var ev = new MassDataChangedEvent((uid, body, manager), oldMass, oldInertia, oldCenter); RaiseLocalEvent(uid, ref ev); } From eaabb17580d14f9e9c1c96caa6587cd956ade1b4 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Fri, 5 Jul 2024 23:41:53 -0700 Subject: [PATCH 3/5] Comp1 --- Robust.Shared/Physics/Events/MassChangedEvent.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Robust.Shared/Physics/Events/MassChangedEvent.cs b/Robust.Shared/Physics/Events/MassChangedEvent.cs index 2094f5d22e8..2c6030661e3 100644 --- a/Robust.Shared/Physics/Events/MassChangedEvent.cs +++ b/Robust.Shared/Physics/Events/MassChangedEvent.cs @@ -19,9 +19,9 @@ public readonly record struct MassDataChangedEvent( Vector2 OldCenter ) { - public float NewMass => Entity.Comp._mass; - public float NewInertia => Entity.Comp._inertia; - public float NewCenter => Entity.Comp._localCenter; + public float NewMass => Entity.Comp1._mass; + public float NewInertia => Entity.Comp1._inertia; + public float NewCenter => Entity.Comp1._localCenter; public bool MassChanged => NewMass != OldMass; public bool InertiaChanged => NewInertia != OldInertia; public bool CenterChanged => NewCenter != OldCenter; From 31e22e7a15238d735dc62fbcc15a6442742bc5ac Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Fri, 5 Jul 2024 23:57:59 -0700 Subject: [PATCH 4/5] Vector2 --- Robust.Shared/Physics/Events/MassChangedEvent.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Robust.Shared/Physics/Events/MassChangedEvent.cs b/Robust.Shared/Physics/Events/MassChangedEvent.cs index 2c6030661e3..b7324290466 100644 --- a/Robust.Shared/Physics/Events/MassChangedEvent.cs +++ b/Robust.Shared/Physics/Events/MassChangedEvent.cs @@ -21,7 +21,7 @@ Vector2 OldCenter { public float NewMass => Entity.Comp1._mass; public float NewInertia => Entity.Comp1._inertia; - public float NewCenter => Entity.Comp1._localCenter; + public Vector2 NewCenter => Entity.Comp1._localCenter; public bool MassChanged => NewMass != OldMass; public bool InertiaChanged => NewInertia != OldInertia; public bool CenterChanged => NewCenter != OldCenter; From 970a30dfbf86ac2990adbfe4ddf10aa57dd2b452 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Fri, 5 Jul 2024 23:58:41 -0700 Subject: [PATCH 5/5] I sure love an analyzer that doesn't work half the time