diff --git a/LinearAlgebra/src/Angle.cs b/LinearAlgebra/src/Angle.cs index ca03a50..c3dd711 100644 --- a/LinearAlgebra/src/Angle.cs +++ b/LinearAlgebra/src/Angle.cs @@ -12,8 +12,8 @@ namespace LinearAlgebra // public static float Rad2Deg = 360.0f / ((float)Math.PI * 2); // public static float Deg2Rad = ((float)Math.PI * 2) / 360.0f; - public const float Deg2Rad = 360.0f / ((float)Math.PI * 2); //0.0174532924F; - public const float Rad2Deg = ((float)Math.PI * 2) / 360.0f; //57.29578F; + public const float Rad2Deg = 360.0f / ((float)Math.PI * 2); //0.0174532924F; + public const float Deg2Rad = ((float)Math.PI * 2) / 360.0f; //57.29578F; /// /// Clamp the angle between the given min and max values diff --git a/LinearAlgebra/src/Direction.cs b/LinearAlgebra/src/Direction.cs index 258e27e..d16ac72 100644 --- a/LinearAlgebra/src/Direction.cs +++ b/LinearAlgebra/src/Direction.cs @@ -1,3 +1,8 @@ +using System; +#if UNITY_5_3_OR_NEWER +using Vector3Float = UnityEngine.Vector3; +#endif + namespace LinearAlgebra { @@ -32,6 +37,23 @@ namespace LinearAlgebra this.horizontal += 180; this.vertical = 180 - this.vertical; } + } + + public Vector3Float ToVector3() + { + float verticalRad = (Angle.pi / 2) - this.vertical * Angle.Deg2Rad; + float horizontalRad = this.horizontal * Angle.Deg2Rad; + float cosVertical = (float)Math.Cos(verticalRad); + float sinVertical = (float)Math.Sin(verticalRad); + float cosHorizontal = (float)Math.Cos(horizontalRad); + float sinHorizontal = (float)Math.Sin(horizontalRad); + + float x = sinVertical * sinHorizontal; + float y = cosVertical; + float z = sinVertical * cosHorizontal; + + Vector3Float v = new(x, y, z); + return v; } } diff --git a/LinearAlgebra/src/SwingTwist.cs b/LinearAlgebra/src/SwingTwist.cs index a7a73da..acf8978 100644 --- a/LinearAlgebra/src/SwingTwist.cs +++ b/LinearAlgebra/src/SwingTwist.cs @@ -1,3 +1,8 @@ +using System.Numerics; +#if UNITY_5_3_OR_NEWER +using Quaternion = UnityEngine.Quaternion; +#endif + namespace LinearAlgebra { @@ -27,6 +32,15 @@ namespace LinearAlgebra SwingTwist r = new SwingTwist(up, right, forward); return r; } + +#if UNITY_5_3_OR_NEWER + public Quaternion ToQuaternion() { + Quaternion q = Quaternion.Euler(-this.swing.vertical, + this.swing.horizontal, + this.twist); + return q; + } +#endif } } \ No newline at end of file diff --git a/LinearAlgebra/test/AngleTest.cs b/LinearAlgebra/test/AngleTest.cs index 75522f3..34b107a 100644 --- a/LinearAlgebra/test/AngleTest.cs +++ b/LinearAlgebra/test/AngleTest.cs @@ -1,3 +1,4 @@ +#if !UNITY_5_6_OR_NEWER using NUnit.Framework; namespace LinearAlgebra.Test @@ -166,4 +167,5 @@ namespace LinearAlgebra.Test } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/Unity/SiteServer.cs b/Unity/SiteServer.cs index 54b0ea9..deb237b 100644 --- a/Unity/SiteServer.cs +++ b/Unity/SiteServer.cs @@ -22,7 +22,7 @@ namespace RoboidControl.Unity { } public void HandleNewThing(RoboidControl.Thing thing) { - //Debug.Log("Handle New thing event"); + Debug.Log($"Handle New thing event for {thing}"); site.Add(thing, false); thingQueue.Enqueue(thing); } diff --git a/Unity/Thing.cs b/Unity/Thing.cs index 6e94294..aa4921e 100644 --- a/Unity/Thing.cs +++ b/Unity/Thing.cs @@ -58,18 +58,19 @@ namespace RoboidControl.Unity { if (core == null) return; - if (core.linearVelocity != null) { + if (core.linearVelocity != null && core.linearVelocity.distance != 0) { Vector3 direction = Quaternion.AngleAxis(core.linearVelocity.direction.horizontal, Vector3.up) * Vector3.forward; this.transform.Translate(core.linearVelocity.distance * Time.deltaTime * direction, Space.Self); - } - if (core.angularVelocity != null) { - Vector3 angularVelocity = core.angularVelocity.ToVector3(); - this.transform.localRotation *= Quaternion.Euler(angularVelocity * Time.deltaTime); - } - - if (core.hasPosition) + } else if (core.positionUpdated) this.transform.localPosition = core.position.ToVector3(); - //this.transform.localRotation = core.orientation.ToQuaternion(); + + if (core.angularVelocity != null && core.angularVelocity.distance != 0) { + Vector3 angularVelocity = core.angularVelocity.ToVector3(); + Vector3 axis = core.angularVelocity.direction.ToVector3(); + this.transform.localRotation *= Quaternion.AngleAxis(core.angularVelocity.distance * Time.deltaTime, axis); + //this.transform.localRotation *= Quaternion.Euler(angularVelocity * Time.deltaTime); + } else if (core.orientationUpdated) + this.transform.localRotation = core.orientation.ToQuaternion(); if (!string.IsNullOrEmpty(core.modelUrl) && this.modelUrl == null) { string extension = core.modelUrl.Substring(core.modelUrl.LastIndexOf(".")); diff --git a/src/Participant.cs b/src/Participant.cs index 6c94a19..b3d7d44 100644 --- a/src/Participant.cs +++ b/src/Participant.cs @@ -73,8 +73,8 @@ namespace RoboidControl { if (foundThing == null) { things.Add(thing); - if (invokeEvent) - Thing.InvokeNewThing(thing); + // if (invokeEvent) + // Thing.InvokeNewThing(thing); // Console.Write($"Add thing {ipAddress}:{port}[{networkId}/{thing.id}]"); } // else { diff --git a/src/Thing.cs b/src/Thing.cs index 42f1f92..2fa8862 100644 --- a/src/Thing.cs +++ b/src/Thing.cs @@ -42,20 +42,29 @@ namespace RoboidControl { #region Init /// - /// Create a new thing without communication abilities + /// Create a new thing for a participant /// + /// The participant owning the thing /// The type of thing - public Thing(byte thingType = (byte)Type.Undetermined) : this(LocalParticipant.Isolated(), thingType) { + public Thing(Participant owner, byte thingType = (byte)Type.Undetermined, bool invokeEvent = true) { + this.owner = owner; + this.type = thingType; + this.owner.Add(this); + if (invokeEvent) + InvokeNewThing(this); } - /// /// Create a new thing for a participant /// /// The participant owning the thing /// The type of thing - public Thing(Participant owner, byte thingType = (byte)Type.Undetermined) { - this.owner = owner; - this.type = thingType; + public Thing(Participant owner, Type thingType = Type.Undetermined, bool invokeEvent = true) : this(owner, (byte)thingType, invokeEvent) { + } + /// + /// Create a new thing without communication abilities + /// + /// The type of thing + public Thing(byte thingType = (byte)Type.Undetermined, bool invokeEvent = true) : this(LocalParticipant.Isolated(), thingType, invokeEvent) { } /// @@ -63,7 +72,7 @@ namespace RoboidControl { /// /// The parent thing /// The type of thing - public Thing(Thing parent, byte thingType = (byte)Type.Undetermined) : this(parent.owner, thingType) { + public Thing(Thing parent, byte thingType = (byte)Type.Undetermined, bool invokeEvent = true) : this(parent.owner, thingType, invokeEvent) { this.parent = parent; } @@ -304,7 +313,7 @@ namespace RoboidControl { set { if (_linearVelocity != value) { _linearVelocity = value; - linearVelocityUpdated = true; + hasLinearVelocity = _linearVelocity.distance != 0; OnLinearVelocityChanged?.Invoke(_linearVelocity); } } @@ -316,7 +325,7 @@ namespace RoboidControl { /// /// Boolean indicating the thing has an updated linear velocity /// - public bool linearVelocityUpdated = false; + public bool hasLinearVelocity = false; private Spherical _angularVelocity = Spherical.zero; /// @@ -327,7 +336,7 @@ namespace RoboidControl { set { if (_angularVelocity != value) { _angularVelocity = value; - angularVelocityUpdated = true; + hasAngularVelocity = _angularVelocity.distance != 0; OnAngularVelocityChanged?.Invoke(_angularVelocity); } } @@ -339,7 +348,7 @@ namespace RoboidControl { /// /// Boolean indicating the thing has an updated angular velocity /// - public bool angularVelocityUpdated = false; + public bool hasAngularVelocity = false; #if UNITY_5_3_OR_NEWER /// @@ -375,6 +384,8 @@ namespace RoboidControl { child.Update(currentTimeMs, recursively); } } + positionUpdated = false; + orientationUpdated = false; } /// diff --git a/src/Things/DifferentialDrive.cs b/src/Things/DifferentialDrive.cs index 0678ef1..768ad01 100644 --- a/src/Things/DifferentialDrive.cs +++ b/src/Things/DifferentialDrive.cs @@ -8,7 +8,7 @@ namespace RoboidControl { public DifferentialDrive() { } /// @brief Create a differential drive with networking support /// @param participant The local participant - public DifferentialDrive(LocalParticipant participant) : base(participant) { } + public DifferentialDrive(LocalParticipant participant) : base(participant, Type.Undetermined) { } /// @brief Configures the dimensions of the drive /// @param wheelDiameter The diameter of the wheels in meters diff --git a/src/Things/DistanceSensor.cs b/src/Things/DistanceSensor.cs index b47a813..ed66ada 100644 --- a/src/Things/DistanceSensor.cs +++ b/src/Things/DistanceSensor.cs @@ -13,7 +13,7 @@ namespace RoboidControl { /// Constructor for a new distance sensor /// /// The participant for which the sensor is needed - public DistanceSensor(Participant participant) : base(participant) { } + public DistanceSensor(Participant participant) : base(participant, Type.Undetermined) { } /// /// Create a distance sensor with the given ID /// diff --git a/src/Things/TouchSensor.cs b/src/Things/TouchSensor.cs index e4f0e05..630adea 100644 --- a/src/Things/TouchSensor.cs +++ b/src/Things/TouchSensor.cs @@ -12,7 +12,8 @@ namespace RoboidControl { /// /// The participant for with the sensor is needed /// True when the creation should trigger an event - public TouchSensor(Participant owner) : base(owner) { + public TouchSensor(Participant owner) : base(owner, Type.TouchSensor) { + Console.Write("TouchSensor constructor"); //touchedSomething = false; //thisParticipant = owner; }