Skip to content

Commit

Permalink
Adds event for mass and angular inertia changes. (#5286)
Browse files Browse the repository at this point in the history
* Adds MassDataChangedEvent to physics
This event is raised in response to changes to an entities innate mass/angular inertia/center of mass

* Use properties to fetch data from component

* Comp1

* Vector2

* I sure love an analyzer that doesn't work half the time
  • Loading branch information
TemporalOroboros authored Jul 6, 2024
1 parent ad329a6 commit b7cc0ec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Robust.Shared/Physics/Events/MassChangedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Physics.Components;
using System.Numerics;

namespace Robust.Shared.Physics.Events;

/// <summary>
/// By-ref directed event raised when the mass or angular inertia or center of mass of a physics body changes.
/// </summary>
/// <param name="Entity">The physics body that changed.</param>
/// <param name="OldMass">The old mass of the physics body.</param>
/// <param name="OldInertia">The old angular inertia of the physics body.</param>
/// <param name="OldCenter">The old (local) center of mass of the physics body.</param>
[ByRefEvent]
public readonly record struct MassDataChangedEvent(
Entity<PhysicsComponent, FixturesComponent> Entity,
float OldMass,
float OldInertia,
Vector2 OldCenter
)
{
public float NewMass => Entity.Comp1._mass;
public float NewInertia => Entity.Comp1._inertia;
public Vector2 NewCenter => Entity.Comp1._localCenter;
public bool MassChanged => NewMass != OldMass;
public bool InertiaChanged => NewInertia != OldInertia;
public bool CenterChanged => NewCenter != OldCenter;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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), oldMass, oldInertia, oldCenter);
RaiseLocalEvent(uid, ref ev);
}

public bool SetAngularVelocity(EntityUid uid, float value, bool dirty = true, FixturesComponent? manager = null, PhysicsComponent? body = null)
Expand Down

0 comments on commit b7cc0ec

Please sign in to comment.