-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoordinatesShiftingExtension.cs
39 lines (36 loc) · 1.41 KB
/
CoordinatesShiftingExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using Improbable.Math;
using UnityEngine;
namespace FriendsOfSpatial.FloatingOrigin
{
/// <summary>
/// Extension class for the Vector3 and Coordinates class to shift from global
/// Coordinates to a local Vector3.
/// </summary>
public static class CoordinatesShiftingExtension {
/// <summary>
/// Unshifts a local Vector3 back to global Coordinates.
/// </summary>
public static Coordinates Unshift(this Vector3 floatingPosition) {
return new Coordinates(
floatingPosition.x - FloatingOrigin.Origin.X,
floatingPosition.y - FloatingOrigin.Origin.Y,
floatingPosition.z - FloatingOrigin.Origin.Z
);
}
/// <summary>
/// Shifts the global Coordinates to a local Vector3 position.
/// <para>
/// Technically there could precision loss with the following but we assume that due
/// to the way Floating Origins work that the Doubles below always fall in the range of
/// a float.
/// </para>
/// </summary>
public static Vector3 Shift(this Coordinates globalPosition) {
return new Vector3(
(float)(globalPosition.X + FloatingOrigin.Origin.X),
(float)(globalPosition.Y + FloatingOrigin.Origin.Y),
(float)(globalPosition.Z + FloatingOrigin.Origin.Z)
);
}
}
}