I am updating Remote Network Entities for my new built in Colyseus Multiplayer Support.
The toolkit now has a Colyseus Network Entity script component that is like the UNET NetworkTransform in Unity.
Is has support to auto sync the remote network entity position and rotation…
LERP has a bit of EASING at the end of the LERP… To be expected when using the current position as the starting point for the LERP.
So i was looking for more of a Unity Style Move Towards function like they have in Unity to smoothly move around the remote network entities in the scene.
So i made a few Unity Style Move Towards helper functions in the babylon.manager.js utility class:
public static MoveTowardsVector2(current:BABYLON.Vector2, target:BABYLON.Vector2, maxDistanceDelta:number):BABYLON.Vector2 {
const result:BABYLON.Vector2 = new BABYLON.Vector2(0.0, 0.0);
BABYLON.Utilities.MoveTowardsVector2ToRef(current, target, maxDistanceDelta, result);
return result;
}
public static MoveTowardsVector2ToRef(current:BABYLON.Vector2, target:BABYLON.Vector2, maxDistanceDelta:number, result:BABYLON.Vector2):void {
const toVector_x:number = target.x - current.x;
const toVector_y:number = target.y - current.y;
const sqDist:number = toVector_x * toVector_x + toVector_y * toVector_y;
if (sqDist == 0 || (maxDistanceDelta >= 0 && sqDist <= maxDistanceDelta * maxDistanceDelta)) {
result.set(target.x, target.y);
} else {
const dist:number = Math.sqrt(sqDist);
result.set((current.x + toVector_x / dist * maxDistanceDelta), (current.y + toVector_y / dist * maxDistanceDelta));
}
}
public static MoveTowardsVector3(current:BABYLON.Vector3, target:BABYLON.Vector3, maxDistanceDelta:number):BABYLON.Vector3 {
const result:BABYLON.Vector3 = new BABYLON.Vector3(0.0, 0.0, 0.0);
BABYLON.Utilities.MoveTowardsVector3ToRef(current, target, maxDistanceDelta, result);
return result;
}
public static MoveTowardsVector3ToRef(current:BABYLON.Vector3, target:BABYLON.Vector3, maxDistanceDelta:number, result:BABYLON.Vector3):void {
const toVector_x:number = target.x - current.x;
const toVector_y:number = target.y - current.y;
const toVector_z:number = target.z - current.z;
const sqDist:number = toVector_x * toVector_x + toVector_y * toVector_y + toVector_z * toVector_z;
if (sqDist == 0 || (maxDistanceDelta >= 0 && sqDist <= maxDistanceDelta * maxDistanceDelta)) {
result.set(target.x, target.y, target.z);
} else {
const dist:number = Math.sqrt(sqDist);
result.set((current.x + toVector_x / dist * maxDistanceDelta), (current.y + toVector_y / dist * maxDistanceDelta), (current.z + toVector_z / dist * maxDistanceDelta));
}
}
public static MoveTowardsVector4(current:BABYLON.Vector4, target:BABYLON.Vector4, maxDistanceDelta:number):BABYLON.Vector4 {
const result:BABYLON.Vector4 = new BABYLON.Vector4(0.0, 0.0, 0.0, 0.0);
BABYLON.Utilities.MoveTowardsVector4ToRef(current, target, maxDistanceDelta, result);
return result;
}
public static MoveTowardsVector4ToRef(current:BABYLON.Vector4, target:BABYLON.Vector4, maxDistanceDelta:number, result:BABYLON.Vector4):void {
const toVector_x:number = target.x - current.x;
const toVector_y:number = target.y - current.y;
const toVector_z:number = target.z - current.z;
const toVector_w:number = target.w - current.w;
const sqDist:number = (toVector_x * toVector_x + toVector_y * toVector_y + toVector_z * toVector_z + toVector_w * toVector_w);
if (sqDist == 0 || (maxDistanceDelta >= 0 && sqDist <= maxDistanceDelta * maxDistanceDelta)) {
result.set(target.x, target.y, target.z, target.w);
} else {
const dist:number = Math.sqrt(sqDist);
result.set((current.x + toVector_x / dist * maxDistanceDelta), (current.y + toVector_y / dist * maxDistanceDelta), (current.z + toVector_z / dist * maxDistanceDelta), (current.w + toVector_w / dist * maxDistanceDelta));
}
}
In case anyone needs to SMOOTH LERP without the easing like in Unity 