Skip to content

Commit fe7d147

Browse files
author
Chris Elion
authored
[MLA-1135] Physics sensors - optional reference body (#4276)
1 parent 086a869 commit fe7d147

File tree

19 files changed

+459
-140
lines changed

19 files changed

+459
-140
lines changed

Project/Assets/ML-Agents/Examples/Crawler/Prefabs/Crawler.prefab

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2742,6 +2742,7 @@ GameObject:
27422742
- component: {fileID: 4845971001715176662}
27432743
- component: {fileID: 4845971001715176663}
27442744
- component: {fileID: 4845971001715176660}
2745+
- component: {fileID: 4622120667686875944}
27452746
m_Layer: 0
27462747
m_Name: Crawler
27472748
m_TagString: Untagged
@@ -2779,7 +2780,7 @@ MonoBehaviour:
27792780
m_Name:
27802781
m_EditorClassIdentifier:
27812782
m_BrainParameters:
2782-
VectorObservationSize: 138
2783+
VectorObservationSize: 21
27832784
NumStackedVectorObservations: 1
27842785
VectorActionSize: 14000000
27852786
VectorActionDescriptions: []
@@ -2872,6 +2873,30 @@ MonoBehaviour:
28722873
m_Name:
28732874
m_EditorClassIdentifier:
28742875
debugCommandLineOverride:
2876+
--- !u!114 &4622120667686875944
2877+
MonoBehaviour:
2878+
m_ObjectHideFlags: 0
2879+
m_CorrespondingSourceObject: {fileID: 0}
2880+
m_PrefabInstance: {fileID: 0}
2881+
m_PrefabAsset: {fileID: 0}
2882+
m_GameObject: {fileID: 4845971001715176661}
2883+
m_Enabled: 1
2884+
m_EditorHideFlags: 0
2885+
m_Script: {fileID: 11500000, guid: df0f8be9a37d6486498061e2cbc4cd94, type: 3}
2886+
m_Name:
2887+
m_EditorClassIdentifier:
2888+
RootBody: {fileID: 4845971001588102145}
2889+
VirtualRoot: {fileID: 2270141184585723037}
2890+
Settings:
2891+
UseModelSpaceTranslations: 1
2892+
UseModelSpaceRotations: 1
2893+
UseLocalSpaceTranslations: 0
2894+
UseLocalSpaceRotations: 1
2895+
UseModelSpaceLinearVelocity: 1
2896+
UseLocalSpaceLinearVelocity: 0
2897+
UseJointPositionsAndAngles: 0
2898+
UseJointForces: 0
2899+
sensorName:
28752900
--- !u!1 &4845971001730692034
28762901
GameObject:
28772902
m_ObjectHideFlags: 0
@@ -3018,6 +3043,12 @@ PrefabInstance:
30183043
objectReference: {fileID: 0}
30193044
m_RemovedComponents: []
30203045
m_SourcePrefab: {fileID: 100100000, guid: 72f745913c5a34df5aaadd5c1f0024cb, type: 3}
3046+
--- !u!1 &2270141184585723037 stripped
3047+
GameObject:
3048+
m_CorrespondingSourceObject: {fileID: 2591864627249999519, guid: 72f745913c5a34df5aaadd5c1f0024cb,
3049+
type: 3}
3050+
m_PrefabInstance: {fileID: 4357529801223143938}
3051+
m_PrefabAsset: {fileID: 0}
30213052
--- !u!4 &2270141184585723026 stripped
30223053
Transform:
30233054
m_CorrespondingSourceObject: {fileID: 2591864627249999504, guid: 72f745913c5a34df5aaadd5c1f0024cb,
@@ -3030,7 +3061,7 @@ MonoBehaviour:
30303061
type: 3}
30313062
m_PrefabInstance: {fileID: 4357529801223143938}
30323063
m_PrefabAsset: {fileID: 0}
3033-
m_GameObject: {fileID: 0}
3064+
m_GameObject: {fileID: 2270141184585723037}
30343065
m_Enabled: 1
30353066
m_EditorHideFlags: 0
30363067
m_Script: {fileID: 11500000, guid: 771e78c5e980e440e8cd19716b55075f, type: 3}

Project/Assets/ML-Agents/Examples/Crawler/Prefabs/FixedPlatform.prefab

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ PrefabInstance:
392392
propertyPath: targetToLookAt
393393
value:
394394
objectReference: {fileID: 2673081981996998229}
395+
- target: {fileID: 4622120667686875944, guid: 0456c89e8c9c243d595b039fe7aa0bf9,
396+
type: 3}
397+
propertyPath: Settings.UseLocalSpaceLinearVelocity
398+
value: 1
399+
objectReference: {fileID: 0}
395400
- target: {fileID: 4845971000000621469, guid: 0456c89e8c9c243d595b039fe7aa0bf9,
396401
type: 3}
397402
propertyPath: m_ConnectedAnchor.x

Project/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgent.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,8 @@ public void CollectObservationBodyPart(BodyPart bp, VectorSensor sensor)
9191
//GROUND CHECK
9292
sensor.AddObservation(bp.groundContact.touchingGround); // Is this bp touching the ground
9393

94-
//Get velocities in the context of our orientation cube's space
95-
//Note: You can get these velocities in world space as well but it may not train as well.
96-
sensor.AddObservation(orientationCube.transform.InverseTransformDirection(bp.rb.velocity));
97-
sensor.AddObservation(orientationCube.transform.InverseTransformDirection(bp.rb.angularVelocity));
98-
99-
//Get position relative to hips in the context of our orientation cube's space
100-
sensor.AddObservation(orientationCube.transform.InverseTransformDirection(bp.rb.position - body.position));
101-
10294
if (bp.rb.transform != body)
10395
{
104-
sensor.AddObservation(bp.rb.transform.localRotation);
10596
sensor.AddObservation(bp.currentStrength / m_JdController.maxJointForceLimit);
10697
}
10798
}
@@ -111,9 +102,6 @@ public void CollectObservationBodyPart(BodyPart bp, VectorSensor sensor)
111102
/// </summary>
112103
public override void CollectObservations(VectorSensor sensor)
113104
{
114-
//Add body rotation delta relative to orientation cube
115-
sensor.AddObservation(Quaternion.FromToRotation(body.forward, orientationCube.transform.forward));
116-
117105
//Add pos of target relative to orientation cube
118106
sensor.AddObservation(orientationCube.transform.InverseTransformPoint(target.transform.position));
119107

Binary file not shown.
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# ML-Agents Extensions
22

33
This is a source-only package for new features based on ML-Agents.
4+
5+
More details coming soon.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("Unity.ML-Agents.Extensions.EditorTests")]

com.unity.ml-agents.extensions/Runtime/AssemblyInfo.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.ml-agents.extensions/Runtime/Sensors/ArticulationBodyPoseExtractor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ public ArticulationBodyPoseExtractor(ArticulationBody rootBody)
5454
parentIndices[i] = bodyToIndex[parentArticBody];
5555
}
5656

57-
SetParentIndices(parentIndices);
57+
Setup(parentIndices);
5858
}
5959

6060
/// <inheritdoc/>
61-
protected override Vector3 GetLinearVelocityAt(int index)
61+
protected internal override Vector3 GetLinearVelocityAt(int index)
6262
{
6363
return m_Bodies[index].velocity;
6464
}
6565

6666
/// <inheritdoc/>
67-
protected override Pose GetPoseAt(int index)
67+
protected internal override Pose GetPoseAt(int index)
6868
{
6969
var body = m_Bodies[index];
7070
var go = body.gameObject;

com.unity.ml-agents.extensions/Runtime/Sensors/PhysicsBodySensor.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ public class PhysicsBodySensor : ISensor
1818
/// <summary>
1919
/// Construct a new PhysicsBodySensor
2020
/// </summary>
21-
/// <param name="rootBody"></param>
21+
/// <param name="rootBody">The root Rigidbody. This has no Joints on it (but other Joints may connect to it).</param>
22+
/// <param name="rootGameObject">Optional GameObject used to find Rigidbodies in the hierarchy.</param>
23+
/// <param name="virtualRoot">Optional GameObject used to determine the root of the poses,
2224
/// <param name="settings"></param>
2325
/// <param name="sensorName"></param>
24-
public PhysicsBodySensor(Rigidbody rootBody, GameObject rootGameObject, PhysicsSensorSettings settings, string sensorName=null)
26+
public PhysicsBodySensor(
27+
Rigidbody rootBody,
28+
GameObject rootGameObject,
29+
GameObject virtualRoot,
30+
PhysicsSensorSettings settings,
31+
string sensorName=null
32+
)
2533
{
26-
var poseExtractor = new RigidBodyPoseExtractor(rootBody, rootGameObject);
34+
var poseExtractor = new RigidBodyPoseExtractor(rootBody, rootGameObject, virtualRoot);
2735
m_PoseExtractor = poseExtractor;
2836
m_SensorName = string.IsNullOrEmpty(sensorName) ? $"PhysicsBodySensor:{rootBody?.name}" : sensorName;
2937
m_Settings = settings;

0 commit comments

Comments
 (0)