Skip to content

Commit b3cbfb9

Browse files
authored
feat: Bepu, getter to retrieve the impulse and force applied by a given constraint (#2930)
1 parent 474a96b commit b3cbfb9

3 files changed

Lines changed: 75 additions & 0 deletions

File tree

sources/engine/Stride.BepuPhysics/Stride.BepuPhysics.Tests/BepuTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,43 @@ public static void ConstraintsTest()
124124
RunGameTest(game);
125125
}
126126

127+
[Fact]
128+
public static void ConstraintsForceTest()
129+
{
130+
var game = new GameTest();
131+
game.Script.AddTask(async () =>
132+
{
133+
game.ScreenShotAutomationEnabled = false;
134+
135+
var e1 = new BodyComponent { Collider = new CompoundCollider { Colliders = { new BoxCollider() } } };
136+
var c = new OneBodyLinearMotorConstraintComponent
137+
{
138+
A = e1,
139+
LocalOffset = Vector3.Zero,
140+
MotorMaximumForce = 3,
141+
MotorDamping = 1,
142+
};
143+
144+
Assert.Equal(0f, c.GetAccumulatedForceMagnitude());
145+
146+
game.SceneSystem.SceneInstance.RootScene.Entities.AddRange(new EntityComponent[] { e1, c }.Select(x => new Entity { x }));
147+
148+
Assert.Equal(0f, c.GetAccumulatedForceMagnitude());
149+
150+
do
151+
{
152+
await e1.Simulation!.AfterUpdate();
153+
154+
// Given current gravity, the constraint should pull the body in under 5 seconds,
155+
// otherwise something is wrong and this loop would likely continue indefinitely
156+
Assert.True(game.UpdateTime.Total.TotalSeconds < 5d);
157+
} while (c.GetAccumulatedForceMagnitude() < float.BitDecrement(c.MotorMaximumForce));
158+
159+
game.Exit();
160+
});
161+
RunGameTest(game);
162+
}
163+
127164
[Fact]
128165
public static void OnContactRemovalTest()
129166
{

sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Constraints/_ConstraintComponent.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,26 @@ internal void TryUpdateDescription()
126126
}
127127
}
128128

129+
/// <inheritdoc/>
130+
public override float GetAccumulatedImpulseMagnitude()
131+
{
132+
if (_bepuSimulation != null && Attached)
133+
return MathF.Sqrt(_bepuSimulation.Simulation.Solver.GetAccumulatedImpulseMagnitudeSquared(_cHandle));
134+
135+
return 0;
136+
}
137+
138+
/// <inheritdoc/>
139+
public override float GetAccumulatedForceMagnitude()
140+
{
141+
if (_bepuSimulation != null && Attached)
142+
{
143+
float impulses = GetAccumulatedImpulseMagnitude();
144+
return 1f / (float)_bepuSimulation.FixedTimeStep.TotalSeconds * _bepuSimulation.SolverSubStep * impulses;
145+
}
146+
147+
return 0;
148+
}
149+
129150
protected ConstraintComponent(int bodies) : base(bodies) { }
130151
}

sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Constraints/_ConstraintComponentBase.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ protected BodyComponent? this[int i]
5353
/// <remarks> May not be attached if it is not in a scene, when not <see cref="Enabled"/>, when any of its target is null, not in a scene or in a different simulation </remarks>
5454
public abstract bool Attached { get; }
5555

56+
/// <summary>
57+
/// Returns the squared sum of all impulses this constraint applied on the last tick
58+
/// </summary>
59+
/// <remarks>
60+
/// Impulses increase depending on <see cref="BepuSimulation.FixedTimeStep"/>, as well as the amount of <see cref="BepuSimulation.SolverSubStep"/>.
61+
/// You may want to use <see cref="GetAccumulatedForceMagnitude"/> instead.
62+
/// </remarks>
63+
public abstract float GetAccumulatedImpulseMagnitude();
64+
65+
/// <summary>
66+
/// Returns the squared sum of all forces this constraint applied on the last tick
67+
/// </summary>
68+
/// <remarks>
69+
/// This can be used to compare with a given motor constraints' MaximumForce property for example.
70+
/// </remarks>
71+
public abstract float GetAccumulatedForceMagnitude();
72+
5673
protected abstract void BodiesChanged();
5774

5875
internal abstract void Activate(BepuConfiguration bepuConfig);

0 commit comments

Comments
 (0)