Steps toward hierarchy changes

This commit is contained in:
Pascal Serrarens 2025-04-23 12:49:14 +02:00
parent be167722e7
commit 6e85ef163d
4 changed files with 63 additions and 37 deletions

View File

@ -35,6 +35,7 @@ namespace RoboidControl.Unity {
} }
siteServer.site.Add(thing); siteServer.site.Add(thing);
core.OnPoseChanged += PoseChanged; core.OnPoseChanged += PoseChanged;
core.OnHierarchyChanged += ParentChanged;
} }
public static Thing Create(RoboidControl.Thing core) { public static Thing Create(RoboidControl.Thing core) {
@ -59,6 +60,7 @@ namespace RoboidControl.Unity {
this.transform.localRotation = core.orientation.ToQuaternion(); this.transform.localRotation = core.orientation.ToQuaternion();
core.OnPoseChanged += this.PoseChanged; core.OnPoseChanged += this.PoseChanged;
core.OnHierarchyChanged += ParentChanged;
} }
/// <summary> /// <summary>
@ -72,18 +74,11 @@ namespace RoboidControl.Unity {
Vector3 direction = Quaternion.AngleAxis(core.linearVelocity.direction.horizontal, Vector3.up) * Vector3.forward; Vector3 direction = Quaternion.AngleAxis(core.linearVelocity.direction.horizontal, Vector3.up) * Vector3.forward;
this.transform.Translate(core.linearVelocity.distance * Time.deltaTime * direction, Space.Self); this.transform.Translate(core.linearVelocity.distance * Time.deltaTime * direction, Space.Self);
} }
// else if (core.positionUpdated)
// this.transform.localPosition = core.position.ToVector3();
if (core.angularVelocity != null && core.angularVelocity.distance != 0) { if (core.angularVelocity != null && core.angularVelocity.distance != 0) {
// Vector3 angularVelocity = core.angularVelocity.ToVector3();
// Debug.Log(angularVelocity);
Vector3 axis = core.angularVelocity.direction.ToVector3(); Vector3 axis = core.angularVelocity.direction.ToVector3();
this.transform.localRotation *= Quaternion.AngleAxis(core.angularVelocity.distance * Time.deltaTime, axis); 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) { if (!string.IsNullOrEmpty(core.modelUrl) && this.modelUrl == null) {
string extension = core.modelUrl.Substring(core.modelUrl.LastIndexOf(".")); string extension = core.modelUrl.Substring(core.modelUrl.LastIndexOf("."));
@ -98,7 +93,14 @@ namespace RoboidControl.Unity {
this.gameObject.name = core.name; this.gameObject.name = core.name;
core.nameChanged = false; core.nameChanged = false;
} }
if (core.hierarchyChanged) {
Debug.Log("Parent changed");
if (core.parent == null)
this.transform.SetParent(null, true);
else
this.transform.SetParent(core.parent.component.transform, true);
core.hierarchyChanged = false;
}
} }
private void PoseChanged() { private void PoseChanged() {
@ -109,6 +111,14 @@ namespace RoboidControl.Unity {
this.transform.localRotation = core.orientation.ToQuaternion(); this.transform.localRotation = core.orientation.ToQuaternion();
} }
private void ParentChanged() {
// if (core.parent == null)
// this.transform.SetParent(null, true);
// else
// this.transform.SetParent(core.parent.component.transform, true);
}
private IEnumerator LoadJPG() { private IEnumerator LoadJPG() {
UnityWebRequest request = UnityWebRequestTexture.GetTexture(core.modelUrl); UnityWebRequest request = UnityWebRequestTexture.GetTexture(core.modelUrl);
yield return request.SendWebRequest(); yield return request.SendWebRequest();

View File

@ -94,27 +94,6 @@ namespace RoboidControl {
#region Update #region Update
protected void ReceiveUDP(IAsyncResult result) {
// UnityEngine.Debug.Log("received");
if (this.udpClient == null) // || this.endPoint == null)
return;
byte[] data = this.udpClient.EndReceive(result, ref endPoint);
// This does not yet take multi-packet messages into account!
if (endPoint == null)
return;
// We can receive our own publish (broadcast) packages. How do we recognize them????
// It is hard to determine our source port
string ipAddress = endPoint.Address.ToString();
Participant remoteParticipant = GetParticipant(ipAddress, endPoint.Port);
remoteParticipant ??= AddParticipant(ipAddress, endPoint.Port);
ReceiveData(data, remoteParticipant);
udpClient.BeginReceive(new AsyncCallback(callbackResult => ReceiveUDP(callbackResult)), null);
}
protected ulong nextPublishMe = 0; protected ulong nextPublishMe = 0;
public override void Update(ulong currentTimeMS = 0) { public override void Update(ulong currentTimeMS = 0) {
if (currentTimeMS == 0) { if (currentTimeMS == 0) {
@ -140,6 +119,11 @@ namespace RoboidControl {
if (thing == null || thing.parent != null) if (thing == null || thing.parent != null)
continue; continue;
if (thing.hierarchyChanged && !(this.isIsolated || this.networkId == 0)) {
ThingMsg thingMsg = new(this.networkId, thing);
this.Send(this.remoteSite, thingMsg);
}
thing.Update(currentTimeMS, true); thing.Update(currentTimeMS, true);
if (this.isIsolated || this.networkId == 0) if (this.isIsolated || this.networkId == 0)
continue; continue;
@ -172,10 +156,6 @@ namespace RoboidControl {
} }
public virtual void Publish() {
this.Publish(new ParticipantMsg(this.networkId));
}
#endregion Update #endregion Update
#region Send #region Send
@ -199,6 +179,10 @@ namespace RoboidControl {
return true; return true;
} }
public virtual void Publish() {
this.Publish(new ParticipantMsg(this.networkId));
}
public void PublishThingInfo(Thing thing) { public void PublishThingInfo(Thing thing) {
// Console.WriteLine("Publish thing info"); // Console.WriteLine("Publish thing info");
this.Publish(new ThingMsg(this.networkId, thing)); this.Publish(new ThingMsg(this.networkId, thing));
@ -239,6 +223,27 @@ namespace RoboidControl {
#region Receive #region Receive
protected void ReceiveUDP(IAsyncResult result) {
// UnityEngine.Debug.Log("received");
if (this.udpClient == null) // || this.endPoint == null)
return;
byte[] data = this.udpClient.EndReceive(result, ref endPoint);
// This does not yet take multi-packet messages into account!
if (endPoint == null)
return;
// We can receive our own publish (broadcast) packages. How do we recognize them????
// It is hard to determine our source port
string ipAddress = endPoint.Address.ToString();
Participant remoteParticipant = GetParticipant(ipAddress, endPoint.Port);
remoteParticipant ??= AddParticipant(ipAddress, endPoint.Port);
ReceiveData(data, remoteParticipant);
udpClient.BeginReceive(new AsyncCallback(callbackResult => ReceiveUDP(callbackResult)), null);
}
public void ReceiveData(byte[] data, Participant sender) { public void ReceiveData(byte[] data, Participant sender) {
byte msgId = data[0]; byte msgId = data[0];
if (msgId == 0xFF) { if (msgId == 0xFF) {

View File

@ -83,6 +83,10 @@ namespace RoboidControl {
else else
thing.parent = parentThing; thing.parent = parentThing;
} }
else {
Console.Write($"Dropped {thing.id}");
thing.parent = null;
}
} }
} }
} }

View File

@ -145,14 +145,15 @@ namespace RoboidControl {
} }
else { else {
value.AddChild(this); value.AddChild(this);
OnParentChanged?.Invoke();
} }
OnHierarchyChanged?.Invoke();
this.hierarchyChanged = true;
} }
} }
/// <summary> /// <summary>
/// Event which is triggered when the parent changes /// Event which is triggered when the parent changes
/// </summary> /// </summary>
public event ChangeHandler OnParentChanged = delegate { }; public event ChangeHandler OnHierarchyChanged = delegate { };
/// <summary> /// <summary>
/// Add a child Thing to this Thing /// Add a child Thing to this Thing
@ -219,6 +220,11 @@ namespace RoboidControl {
return null; return null;
} }
/// <summary>
/// Indicator that the hierarchy of the thing has changed
/// </summary>
public bool hierarchyChanged = true;
/// <summary> /// <summary>
/// The children of this thing /// The children of this thing
/// </summary> /// </summary>
@ -369,6 +375,7 @@ namespace RoboidControl {
this.orientationUpdated = false; this.orientationUpdated = false;
this.linearVelocityUpdated = false; this.linearVelocityUpdated = false;
this.angularVelocityUpdated = false; this.angularVelocityUpdated = false;
//this.hierarchyChanged = false;
// should recurse over children... // should recurse over children...
if (recursively) { if (recursively) {