Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions unity/Runtime/Components/Joints/MjJointSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public struct MjJointSettings {
Solver = SolverSettings.Default,
};

public void FromMjcf(XmlElement mjcf) {
public void FromMjcf(XmlElement mjcf, bool springReferenceIsAngular = true) {
Armature = mjcf.GetFloatAttribute("armature", 0.0f);
Spring.FromMjcf(mjcf);
Spring.FromMjcf(mjcf, springReferenceIsAngular);
Solver.FromMjcf(mjcf);
}

Expand Down Expand Up @@ -68,11 +68,14 @@ public void ToMjcf(XmlElement mjcf) {
mjcf.SetAttribute("stiffness", MjEngineTool.MakeLocaleInvariant($"{Stiffness}"));
}

public void FromMjcf(XmlElement mjcf) {
public void FromMjcf(XmlElement mjcf, bool referenceIsAngular = true) {
var springDamper = mjcf.GetVector2Attribute("springdamper", Vector2.zero);
TimeConstant = springDamper.x;
DampingRatio = springDamper.y;
EquilibriumPose = MjSceneImportSettings.AnglesInDegrees? mjcf.GetFloatAttribute("springref", 0.0f) : mjcf.GetFloatAttribute("springref", 0.0f) * Mathf.Rad2Deg;
EquilibriumPose = mjcf.GetFloatAttribute("springref", 0.0f);
if (referenceIsAngular && !MjSceneImportSettings.AnglesInDegrees) {
EquilibriumPose *= Mathf.Rad2Deg;
}
Damping = mjcf.GetFloatAttribute("damping", 0.0f);
Stiffness = mjcf.GetFloatAttribute("stiffness", 0.0f);
}
Expand Down Expand Up @@ -169,4 +172,4 @@ public void FromMjcf(XmlElement mjcf) {
Margin = mjcf.GetFloatAttribute("margin", defaultValue: 0.0f);
}
}
}
}
4 changes: 2 additions & 2 deletions unity/Runtime/Components/Joints/MjSlideJoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected override void OnParseMjcf(XmlElement mjcf) {

Configuration = mjcf.GetFloatAttribute("ref", 0.0f);

Settings.FromMjcf(mjcf);
Settings.FromMjcf(mjcf, springReferenceIsAngular: false);
}

protected override XmlElement OnGenerateMjcf(XmlDocument doc) {
Expand All @@ -88,4 +88,4 @@ protected override XmlElement OnGenerateMjcf(XmlDocument doc) {
return mjcf;
}
}
}
}
16 changes: 15 additions & 1 deletion unity/Tests/Editor/Components/Joints/MjSlideJointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,19 @@ public void ParsingJointLimitRangeSettings(
Assert.That(_joint.RangeLower, Is.EqualTo(expectedLow));
Assert.That(_joint.RangeUpper, Is.EqualTo(expectedHigh));
}

[Test]
public void ParsingSpringReferenceKeepsLinearUnitsInRadianModels() {
var previous = MjSceneImportSettings.AnglesInDegrees;
MjSceneImportSettings.AnglesInDegrees = false;
try {
var jointElement = (XmlElement)_doc.AppendChild(_doc.CreateElement("joint"));
jointElement.SetAttribute("springref", "0.25");
_joint.ParseMjcf(jointElement);
Assert.That(_joint.Settings.Spring.EquilibriumPose, Is.EqualTo(0.25f).Within(1e-6f));
} finally {
MjSceneImportSettings.AnglesInDegrees = previous;
}
}
}
}
}