Origin Shift

Modified on Sun, 19 Dec 2021 at 09:30 AM

Origin Shift is a mechanism that prevents floating point issues when objects reach distant positions due to 32-bit floating point limitations in Unity. When enabled, Voxel Play will move the camera or player back to zero position when it surpasses certain distance. It also shifts everything else as well (world, mobs, etc.).


To enable this option, go to Voxel Play Environment and enable the "Origin Shift" checkbox under the Quality & Effects section. Then select the distance at which you want the shift to occur. A value of 1000 usually works great as it ensures the shift only occur when necessary.


What's shifted?

  • The distanceAnchor gameobject (usually the character or camera's root) is shifted.
  • The VoxelPlayWorld gameobject (root of all chunks) is shifted.
  • Any gameobject with a VoxelPlayBehaviour with the "Use Origin Shift" option enabled, is also shifted.


Considerations


Internally, Voxel Play uses a 64 bit precision (double floating point type) coordinate system. Instead of Vector3, it uses a 64-bit backed class named Vector3d (d = double precision).


All positions passed to Voxel Play API (ie. GetVoxelIndex(position)) is automatically converted to Vector3d. During this conversion, the coordinate shift occurs automatically (if origin shift is enabled). This design assumes that:


- When passing a Vector3 position to the API, Voxel Play understand it refers to a world space coordinate so it shifts according to the current world pivot.

- When passing a Vector3d, Voxel Play understands that the function is receiving an already shifted value so it doesn't apply shifting.


In practice you shouldn't need to make any change to your code: always pass world-space positions as usual to the API. Voxel Play will take care of any conversion and shifting if necessary.


However, if you have coded custom terrain or detail generators, these functions will now receive a Vector3d position instead of a Vector3 so minor changes maybe required so instead of a Vector3 you use a Vector3d inside the generator code.



API support


In most cases, origin shift (when enabled) occurs automatically when player goes beyond the maximum distance set in the origin shift section. The world, distance anchor, camera and any other gameobject with the VoxelPlayBehaviour component with origin shift enabled on it, will be shifted so the world root position is kept around 0 position.


Voxel Play exposes the following methods and events for advanced or special cases:


public void RegisterOriginShiftTransform (Transform t);

Registers a transform of an object that should be shifted when world root position moves. Note that if your gameobject has the Voxel Play Behaviour component attached to it, this component already exposes a property to make the object position compatible with origin shift updates.


public void UnregisterOriginShiftTransform (Transform t);

Detaches a transform from the origin shift system.


public void OriginShiftApply(Vector3 pivot);

Manually applies a world position change where the new origin is given in the "pivot" parameter.


public event OriginShiftPreEvent OnOriginPreShift;

Event that occurs when an origin shift is being applied. You can return false from the event delegate to cancel the world pivot change.


public event OriginShiftPostEvent OnOriginPostShift;

Event that occurs when an origin shift is performed.






Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article