PdMuscle: Passive damping floor (tissue viscosity); Drive no longer skips limp muscles, PdMuscle decides
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
dedf391699
commit
e3fb0a8582
3 changed files with 45 additions and 11 deletions
|
|
@ -127,11 +127,6 @@ namespace Lodur.Body.Ymir
|
|||
var v = _world.View;
|
||||
for (var m = 0; m < MuscleCount; m++)
|
||||
{
|
||||
if (!(strengths[m] > 0f))
|
||||
{
|
||||
continue; // limp (or NaN): no torque
|
||||
}
|
||||
|
||||
var j = _jointLane[m];
|
||||
var rotation = new Quat(v.JointRotationX[j], v.JointRotationY[j], v.JointRotationZ[j], v.JointRotationW[j]);
|
||||
PdMuscle.Torque(rotation, v.JointAngularVelocityX[j], v.JointAngularVelocityY[j], v.JointAngularVelocityZ[j],
|
||||
|
|
|
|||
|
|
@ -65,10 +65,17 @@ namespace Lodur
|
|||
{
|
||||
public readonly float SwingStiffness, SwingDamping, TwistStiffness, TwistDamping;
|
||||
|
||||
public MuscleGains(float swingStiffness, float swingDamping, float twistStiffness, float twistDamping)
|
||||
/// <summary>
|
||||
/// The fraction of damping kept whatever the strength, in [0, 1] (tissue viscosity): a limp joint with Passive 1
|
||||
/// still resists being swung, with no stiffness. Default 0, where limp is exactly zero torque.
|
||||
/// </summary>
|
||||
public readonly float Passive;
|
||||
|
||||
public MuscleGains(float swingStiffness, float swingDamping, float twistStiffness, float twistDamping, float passive = 0f)
|
||||
{
|
||||
SwingStiffness = swingStiffness; SwingDamping = swingDamping;
|
||||
TwistStiffness = twistStiffness; TwistDamping = twistDamping;
|
||||
Passive = passive > 0f ? (passive < 1f ? passive : 1f) : 0f;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -83,14 +90,18 @@ namespace Lodur
|
|||
/// Torque on the child, in the parent joint frame, that turns <paramref name="rotation"/> (child frame
|
||||
/// relative to parent frame) toward <paramref name="target"/> and damps <paramref name="angularVelocity"/>
|
||||
/// (child minus parent, in the parent frame). Strength scales every gain together, so the
|
||||
/// damping ratio does not change with it; strength 0 is a limp joint and returns exactly zero.
|
||||
/// damping ratio does not change with it. Damping is scaled by the larger of the strength and
|
||||
/// <see cref="MuscleGains.Passive"/>, stiffness by the strength alone: strength 0 is a limp joint, exactly zero torque
|
||||
/// when Passive is 0 and pure damping when it is not.
|
||||
/// The rotation error takes the shorter way round.
|
||||
/// </summary>
|
||||
public static void Torque(
|
||||
in Quat rotation, float wx, float wy, float wz, in Quat target, in MuscleGains gains, float strength,
|
||||
out float tx, out float ty, out float tz)
|
||||
{
|
||||
if (strength <= 0f)
|
||||
var stiff = strength > 0f ? strength : 0f; // negative or NaN strength is limp
|
||||
var damp = stiff > gains.Passive ? stiff : gains.Passive;
|
||||
if (damp <= 0f)
|
||||
{
|
||||
tx = ty = tz = 0f;
|
||||
return;
|
||||
|
|
@ -106,9 +117,18 @@ namespace Lodur
|
|||
var sin = MathF.Sqrt(e.X * e.X + e.Y * e.Y + e.Z * e.Z);
|
||||
// Rotation vector of the error: angle * axis, with the small-angle limit of 2 * vector part.
|
||||
var scale = sin < 1e-6f ? 2f : 2f * MathF.Atan2(sin, e.W) / sin;
|
||||
tx = strength * (gains.SwingStiffness * scale * e.X - gains.SwingDamping * wx);
|
||||
ty = strength * (gains.SwingStiffness * scale * e.Y - gains.SwingDamping * wy);
|
||||
tz = strength * (gains.TwistStiffness * scale * e.Z - gains.TwistDamping * wz);
|
||||
if (damp == stiff)
|
||||
{
|
||||
// The whole law at one strength, in the form every pinned replay was recorded with.
|
||||
tx = stiff * (gains.SwingStiffness * scale * e.X - gains.SwingDamping * wx);
|
||||
ty = stiff * (gains.SwingStiffness * scale * e.Y - gains.SwingDamping * wy);
|
||||
tz = stiff * (gains.TwistStiffness * scale * e.Z - gains.TwistDamping * wz);
|
||||
return;
|
||||
}
|
||||
|
||||
tx = stiff * gains.SwingStiffness * scale * e.X - damp * gains.SwingDamping * wx;
|
||||
ty = stiff * gains.SwingStiffness * scale * e.Y - damp * gains.SwingDamping * wy;
|
||||
tz = stiff * gains.TwistStiffness * scale * e.Z - damp * gains.TwistDamping * wz;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,6 +50,25 @@ public sealed class PdMuscleTests
|
|||
Assert.Equal((0f, 0f, 0f), Torque(Quat.Identity, (0.5f, 0f, 0f), target, 100f, 10f, -1f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Passive_damping_survives_limp_and_stiffness_does_not()
|
||||
{
|
||||
var target = Quat.FromAxisAngle(1f, 0f, 0f, 0.4f);
|
||||
var w = (1f, -2f, 3f);
|
||||
PdMuscle.Torque(Quat.Identity, w.Item1, w.Item2, w.Item3, target, new MuscleGains(100f, 5f, 100f, 2f, passive: 1f), 0f, out var x, out var y, out var z);
|
||||
Assert.Equal((-5f, 10f, -6f), (x, y, z)); // exactly -kd * omega per axis, nothing toward the target
|
||||
PdMuscle.Torque(Quat.Identity, w.Item1, w.Item2, w.Item3, target, new MuscleGains(100f, 5f, 100f, 2f, passive: 0f), 0f, out x, out y, out z);
|
||||
Assert.Equal((0f, 0f, 0f), (x, y, z));
|
||||
// A half passive floor under a quarter strength damps at the floor, stiffens at the strength.
|
||||
PdMuscle.Torque(Quat.Identity, w.Item1, 0f, 0f, target, new MuscleGains(100f, 5f, 100f, 2f, passive: 0.5f), 0.25f, out x, out y, out z);
|
||||
var stiffOnly = Torque(Quat.Identity, default, target, 100f, 5f, 0.25f).X;
|
||||
Assert.Equal(stiffOnly - 0.5f * 5f * 1f, x, 1e-3f);
|
||||
// Above the floor strength rules: the whole law, as without Passive.
|
||||
PdMuscle.Torque(Quat.Identity, w.Item1, w.Item2, w.Item3, target, new MuscleGains(100f, 5f, 100f, 2f, passive: 0.5f), 1f, out x, out y, out z);
|
||||
var plain = Torque(Quat.Identity, w, target, 100f, 5f, 1f, twistKp: 100f, twistKd: 2f);
|
||||
Assert.Equal((plain.X, plain.Y, plain.Z), (x, y, z));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_error_takes_the_shorter_way_round_even_when_the_quaternions_are_opposite_signs()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue