From 3e5da90f4790d2398ab9bd8b2c1fcc3bcc1e3443 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 13 Feb 2025 12:57:05 +0100 Subject: [PATCH 1/4] Basic ant behaviour --- LinearAlgebra/Spherical.cs | 15 +++++++++++++-- Thing.cs | 2 ++ Unity/DistanceSensor.cs | 16 ++++++++++------ Unity/Thing.cs | 1 + 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/LinearAlgebra/Spherical.cs b/LinearAlgebra/Spherical.cs index 111a637..e9c2b5b 100644 --- a/LinearAlgebra/Spherical.cs +++ b/LinearAlgebra/Spherical.cs @@ -1,4 +1,4 @@ -using Vector3 = UnityEngine.Vector3; +using UnityEngine; namespace Passer.LinearAlgebra { public class Spherical { @@ -6,6 +6,7 @@ namespace Passer.LinearAlgebra { public Direction direction; public static Spherical zero = new(0, 0, 0); + public static Spherical forward = new(1, 0, 0); public Spherical(float distance, float horizontal, float vertical) { this.distance = distance; @@ -16,6 +17,17 @@ namespace Passer.LinearAlgebra { this.direction = direction; } + public static Spherical FromVector3(Vector3 v) { + float distance = v.magnitude; + if (distance == 0.0f) + return new Spherical(distance, 0, 0); + else { + float verticalAngle = (Mathf.PI / 2 - Mathf.Acos(v.y / distance)) * Mathf.Rad2Deg; + float horizontalAngle = Mathf.Atan2(v.x, v.z) * Mathf.Rad2Deg; + return new Spherical(distance, horizontalAngle, verticalAngle); + } + } + public Vector3 ToVector3() { float verticalRad = (UnityEngine.Mathf.PI / 2) - this.direction.vertical * UnityEngine.Mathf.Deg2Rad; float horizontalRad = this.direction.horizontal * UnityEngine.Mathf.Deg2Rad; @@ -30,7 +42,6 @@ namespace Passer.LinearAlgebra { Vector3 v = new Vector3(x, y, z); return v; - } } } \ No newline at end of file diff --git a/Thing.cs b/Thing.cs index 74fd51c..c78851a 100644 --- a/Thing.cs +++ b/Thing.cs @@ -7,6 +7,7 @@ namespace Passer.Control.Core { /// /// A thing is the basic building block /// + [Serializable] public class Thing { #region Types @@ -126,6 +127,7 @@ namespace Passer.Control.Core { public Spherical angularVelocity; #if UNITY_5_3_OR_NEWER + [NonSerialized] public Unity.Thing component; #endif diff --git a/Unity/DistanceSensor.cs b/Unity/DistanceSensor.cs index 1b7c9c4..c623c15 100644 --- a/Unity/DistanceSensor.cs +++ b/Unity/DistanceSensor.cs @@ -7,7 +7,7 @@ namespace Passer.Control.Unity { public class DistanceSensor : Thing { public new Core.DistanceSensor core { - get => (Core.DistanceSensor)base.core; + get => (Core.DistanceSensor)base.core; set => base.core = value; } @@ -29,12 +29,16 @@ namespace Passer.Control.Unity { IEnumerator MeasureDistance() { while (Application.isPlaying) { - if (Physics.Raycast(this.transform.position, this.transform.forward, out RaycastHit hitInfo, 10.0f)) { - core.distance = hitInfo.distance; - - // send distance to... - yield return new WaitForSeconds(1); + if (Physics.Raycast(this.transform.position, this.transform.forward, out RaycastHit hitInfo, 2.0f)) { + Thing thing = hitInfo.transform.GetComponentInParent(); + if (thing == null) { + Debug.Log($"collision {hitInfo.transform.name} {hitInfo.distance}"); + core.distance = hitInfo.distance; + } + else + core.distance = 0; } + yield return new WaitForSeconds(0.1f); } } diff --git a/Unity/Thing.cs b/Unity/Thing.cs index 2687f52..a280ca2 100644 --- a/Unity/Thing.cs +++ b/Unity/Thing.cs @@ -5,6 +5,7 @@ namespace Passer.Control.Unity { public class Thing : MonoBehaviour { + [field: SerializeField] public Core.Thing core {get; set; } protected void SetCoreThing(Core.Thing thing) { From 78aed40a9b4412527e024597ff707e983f477362 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 14 Feb 2025 09:08:08 +0100 Subject: [PATCH 2/4] Multiple ant support --- Participant.cs | 4 +++- RemoteParticipant.cs | 8 ++++++-- Unity/DistanceSensor.cs | 2 +- Unity/SiteServer.cs | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Participant.cs b/Participant.cs index ffecf5d..40f71ed 100644 --- a/Participant.cs +++ b/Participant.cs @@ -134,7 +134,9 @@ namespace Passer.Control.Core { this.nextPublishMe = currentTimeMS + this.publishInterval; } - foreach (Thing thing in this.things) { + int n = this.things.Count; + for (int ix = 0; ix < n; ix++) { + Thing thing = this.things[ix]; if (thing != null && thing.parent == null) // update only root things thing.Update(currentTimeMS); } diff --git a/RemoteParticipant.cs b/RemoteParticipant.cs index 500bc41..cc3c0ce 100644 --- a/RemoteParticipant.cs +++ b/RemoteParticipant.cs @@ -31,13 +31,17 @@ namespace Passer.Control.Core { Thing foundThing = Get(thing.networkId, thing.id); if (foundThing == null) { - // if (thing.id == 0) - // thing.id = (byte)(things.Count + 1); things.Add(thing); if (invokeEvent) Thing.InvokeNewThing(thing); // Console.Write($"Add thing {ipAddress}:{port}[{networkId}/{thing.id}]"); + } else { + if (thing != foundThing) { + // should be: find first non-existing id... + thing.id = (byte)this.things.Count; + things.Add(thing); + } } } diff --git a/Unity/DistanceSensor.cs b/Unity/DistanceSensor.cs index c623c15..6dc1fdf 100644 --- a/Unity/DistanceSensor.cs +++ b/Unity/DistanceSensor.cs @@ -22,7 +22,7 @@ namespace Passer.Control.Unity { GameObject distanceObj = new("Distance sensor"); DistanceSensor component = distanceObj.AddComponent(); if (parent != null && parent.component != null) - distanceObj.transform.SetParent(parent.component.transform); + distanceObj.transform.SetParent(parent.component.transform, false); return component; } diff --git a/Unity/SiteServer.cs b/Unity/SiteServer.cs index 628ab09..6431eb3 100644 --- a/Unity/SiteServer.cs +++ b/Unity/SiteServer.cs @@ -22,6 +22,7 @@ namespace Passer.Control.Unity { } public void HandleNewThing(Core.Thing thing) { + site.Add(thing, false); thingQueue.Enqueue(thing); } @@ -31,7 +32,6 @@ namespace Passer.Control.Unity { thing.CreateComponent(); } } - } } From a017c4de186c01c00470cddc7e5a5941221fe154 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 14 Feb 2025 10:56:44 +0100 Subject: [PATCH 3/4] Aded focus --- Participant.cs | 2 +- RemoteParticipant.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Participant.cs b/Participant.cs index 40f71ed..d95d39c 100644 --- a/Participant.cs +++ b/Participant.cs @@ -137,7 +137,7 @@ namespace Passer.Control.Core { int n = this.things.Count; for (int ix = 0; ix < n; ix++) { Thing thing = this.things[ix]; - if (thing != null && thing.parent == null) // update only root things + if (thing != null) // && thing.parent == null) // update only root things thing.Update(currentTimeMS); } } diff --git a/RemoteParticipant.cs b/RemoteParticipant.cs index cc3c0ce..15415a7 100644 --- a/RemoteParticipant.cs +++ b/RemoteParticipant.cs @@ -27,7 +27,7 @@ namespace Passer.Control.Core { } public void Add(Thing thing, bool invokeEvent = true) { - //Console.WriteLine($"added thing [{thing.networkId}/{thing.id}]"); + Console.WriteLine($"added thing [{thing.networkId}/{thing.id}]"); Thing foundThing = Get(thing.networkId, thing.id); if (foundThing == null) { @@ -41,6 +41,7 @@ namespace Passer.Control.Core { // should be: find first non-existing id... thing.id = (byte)this.things.Count; things.Add(thing); + Console.Write($"Add thing, updated thing id to [{thing.networkId}/{thing.id}]"); } } } From c5dc3b6bf5b8f39e91df49bdfb16fc9d68518679 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 14 Feb 2025 11:02:44 +0100 Subject: [PATCH 4/4] fixed double ToVector3 --- LinearAlgebra/Spherical.cs | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/LinearAlgebra/Spherical.cs b/LinearAlgebra/Spherical.cs index e755444..e9c2b5b 100644 --- a/LinearAlgebra/Spherical.cs +++ b/LinearAlgebra/Spherical.cs @@ -43,22 +43,5 @@ namespace Passer.LinearAlgebra { Vector3 v = new Vector3(x, y, z); return v; } - - public Vector3 ToVector3() { - float verticalRad = (UnityEngine.Mathf.PI / 2) - this.direction.vertical * UnityEngine.Mathf.Deg2Rad; - float horizontalRad = this.direction.horizontal * UnityEngine.Mathf.Deg2Rad; - float cosVertical = UnityEngine.Mathf.Cos(verticalRad); - float sinVertical = UnityEngine.Mathf.Sin(verticalRad); - float cosHorizontal = UnityEngine.Mathf.Cos(horizontalRad); - float sinHorizontal = UnityEngine.Mathf.Sin(horizontalRad); - - float x = this.distance * sinVertical * sinHorizontal; - float y = this.distance * cosVertical; - float z = this.distance * sinVertical * cosHorizontal; - - Vector3 v = new Vector3(x, y, z); - return v; - - } } } \ No newline at end of file