From fdfd496d9e3d01fbbe2e7381d110a901e8b678b1 Mon Sep 17 00:00:00 2001 From: Chris Elion Date: Wed, 10 Jun 2020 18:39:21 -0700 Subject: [PATCH 1/4] store hit GameObject in raycast output --- .../Runtime/Sensors/RayPerceptionSensor.cs | 9 +++++- .../Editor/Sensor/RayPerceptionSensorTests.cs | 32 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs b/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs index 036e090c29..2d343026c2 100644 --- a/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs +++ b/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs @@ -161,6 +161,12 @@ public struct RayOutput /// public float HitFraction; + /// + /// The hit GameObject (or null if there was no hit). + /// + public GameObject HitGameObject; + + /// /// Writes the ray output information to a subset of the float array. Each element in the rayAngles array /// determines a sublist of data to the observation. The sublist contains the observation data for a single cast. @@ -456,7 +462,8 @@ out DebugDisplayInfo.RayInfo debugRayOut HasHit = castHit, HitFraction = hitFraction, HitTaggedObject = false, - HitTagIndex = -1 + HitTagIndex = -1, + HitGameObject = hitObject }; if (castHit) diff --git a/com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs b/com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs index 51e86d09ed..4e3307c26a 100644 --- a/com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs +++ b/com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs @@ -44,18 +44,22 @@ void SetupScene() var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = new Vector3(0, 0, 10); cube.tag = k_CubeTag; + cube.name = "cube"; var sphere1 = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere1.transform.position = new Vector3(-5, 0, 5); sphere1.tag = k_SphereTag; + sphere1.name = "sphere1"; var sphere2 = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere2.transform.position = new Vector3(5, 0, 5); // No tag for sphere2 + sphere2.name = "sphere2"; var sphere3 = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere3.transform.position = new Vector3(0, 0, -10); sphere3.tag = k_SphereTag; + sphere3.name = "sphere3"; Physics.SyncTransforms(); } @@ -296,5 +300,33 @@ public void TestRayZeroLength() Assert.LessOrEqual(outputBuffer[2], 1.0f); } } + + [Test] + public void TestStaticPerceive() + { + SetupScene(); + var obj = new GameObject("agent"); + var perception = obj.AddComponent(); + + perception.RaysPerDirection = 0; // single ray + perception.MaxRayDegrees = 45; + perception.RayLength = 20; + perception.DetectableTags = new List(); + perception.DetectableTags.Add(k_CubeTag); + perception.DetectableTags.Add(k_SphereTag); + + var radii = new[] { 0f, .5f }; + foreach (var castRadius in radii) + { + perception.SphereCastRadius = castRadius; + var castInput = perception.GetRayPerceptionInput(); + var castOutput = RayPerceptionSensor.Perceive(castInput); + + Assert.AreEqual(1, castOutput.RayOutputs.Length); + + // Expected to hit the cube + Assert.AreEqual("cube", castOutput.RayOutputs[0].HitGameObject.name); + } + } } } From d209518241ee0a2460b0dd83263a7023185d0d75 Mon Sep 17 00:00:00 2001 From: Chris Elion Date: Wed, 10 Jun 2020 18:45:34 -0700 Subject: [PATCH 2/4] changelog --- com.unity.ml-agents/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/com.unity.ml-agents/CHANGELOG.md b/com.unity.ml-agents/CHANGELOG.md index db3eb10e5a..5ca2dd894e 100755 --- a/com.unity.ml-agents/CHANGELOG.md +++ b/com.unity.ml-agents/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to ### Minor Changes #### com.unity.ml-agents (C#) +- `RayPerceptionSensor.Perceive()` now additionally store the GameObject that was hit by the ray. (#4111) #### ml-agents / ml-agents-envs / gym-unity (Python) ### Bug Fixes From 948e5d2bdd5f390a5f8eaad4ce04c2661bcf2311 Mon Sep 17 00:00:00 2001 From: Chris Elion Date: Wed, 10 Jun 2020 19:13:06 -0700 Subject: [PATCH 3/4] fix unit tests --- .../Tests/Editor/Sensor/RayPerceptionSensorTests.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs b/com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs index 4e3307c26a..503b238724 100644 --- a/com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs +++ b/com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs @@ -27,6 +27,15 @@ public class RayPerception3DTests const string k_CubeTag = "Player"; const string k_SphereTag = "Respawn"; + [TearDown] + public void RemoveGameObjects() + { + var objects = GameObject.FindObjectsOfType(); + foreach (var o in objects) { + UnityEngine.Object.DestroyImmediate(o); + } + } + void SetupScene() { /* Creates game objects in the world for testing. From 75a1fb975881efd9531db39254985b51b7e23cd8 Mon Sep 17 00:00:00 2001 From: Chris Elion Date: Thu, 11 Jun 2020 09:00:42 -0700 Subject: [PATCH 4/4] formatting --- com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs | 2 +- .../Tests/Editor/Sensor/RayPerceptionSensorTests.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs b/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs index 2d343026c2..418cb5825f 100644 --- a/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs +++ b/com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs @@ -340,7 +340,7 @@ public void Update() } /// - public void Reset() { } + public void Reset() {} /// public int[] GetObservationShape() diff --git a/com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs b/com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs index 503b238724..5f56fe321e 100644 --- a/com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs +++ b/com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs @@ -31,7 +31,8 @@ public class RayPerception3DTests public void RemoveGameObjects() { var objects = GameObject.FindObjectsOfType(); - foreach (var o in objects) { + foreach (var o in objects) + { UnityEngine.Object.DestroyImmediate(o); } }