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);
core.OnPoseChanged += PoseChanged;
core.OnHierarchyChanged += ParentChanged;
}
public static Thing Create(RoboidControl.Thing core) {
@ -59,6 +60,7 @@ namespace RoboidControl.Unity {
this.transform.localRotation = core.orientation.ToQuaternion();
core.OnPoseChanged += this.PoseChanged;
core.OnHierarchyChanged += ParentChanged;
}
/// <summary>
@ -72,18 +74,11 @@ namespace RoboidControl.Unity {
Vector3 direction = Quaternion.AngleAxis(core.linearVelocity.direction.horizontal, Vector3.up) * Vector3.forward;
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) {
// Vector3 angularVelocity = core.angularVelocity.ToVector3();
// Debug.Log(angularVelocity);
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("."));
@ -98,7 +93,14 @@ namespace RoboidControl.Unity {
this.gameObject.name = core.name;
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() {
@ -109,6 +111,14 @@ namespace RoboidControl.Unity {
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() {
UnityWebRequest request = UnityWebRequestTexture.GetTexture(core.modelUrl);
yield return request.SendWebRequest();

View File

@ -94,27 +94,6 @@ namespace RoboidControl {
#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;
public override void Update(ulong currentTimeMS = 0) {
if (currentTimeMS == 0) {
@ -139,6 +118,11 @@ namespace RoboidControl {
Thing thing = this.things[ix];
if (thing == null || thing.parent != null)
continue;
if (thing.hierarchyChanged && !(this.isIsolated || this.networkId == 0)) {
ThingMsg thingMsg = new(this.networkId, thing);
this.Send(this.remoteSite, thingMsg);
}
thing.Update(currentTimeMS, true);
if (this.isIsolated || this.networkId == 0)
@ -172,10 +156,6 @@ namespace RoboidControl {
}
public virtual void Publish() {
this.Publish(new ParticipantMsg(this.networkId));
}
#endregion Update
#region Send
@ -199,6 +179,10 @@ namespace RoboidControl {
return true;
}
public virtual void Publish() {
this.Publish(new ParticipantMsg(this.networkId));
}
public void PublishThingInfo(Thing thing) {
// Console.WriteLine("Publish thing info");
this.Publish(new ThingMsg(this.networkId, thing));
@ -239,6 +223,27 @@ namespace RoboidControl {
#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) {
byte msgId = data[0];
if (msgId == 0xFF) {

View File

@ -81,7 +81,11 @@ namespace RoboidControl {
if (parentThing == null)
Console.WriteLine($"Could not find parent [{msg.networkId}/{msg.parentId}]");
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 {
value.AddChild(this);
OnParentChanged?.Invoke();
}
OnHierarchyChanged?.Invoke();
this.hierarchyChanged = true;
}
}
/// <summary>
/// Event which is triggered when the parent changes
/// </summary>
public event ChangeHandler OnParentChanged = delegate { };
public event ChangeHandler OnHierarchyChanged = delegate { };
/// <summary>
/// Add a child Thing to this Thing
@ -219,6 +220,11 @@ namespace RoboidControl {
return null;
}
/// <summary>
/// Indicator that the hierarchy of the thing has changed
/// </summary>
public bool hierarchyChanged = true;
/// <summary>
/// The children of this thing
/// </summary>
@ -368,7 +374,8 @@ namespace RoboidControl {
this.positionUpdated = false;
this.orientationUpdated = false;
this.linearVelocityUpdated = false;
this.angularVelocityUpdated = false;
this.angularVelocityUpdated = false;
//this.hierarchyChanged = false;
// should recurse over children...
if (recursively) {