So, I've got this big top-down 2D space and in this space I have a space ship, and a few planets. These planets are separated by huge distances (e.g. one is ~800,000 units away).
I'm aware of floating point issues when you start to deal with objects that far away. Particularly object jitter.
So, to tackle this I implemented a floating origin (more specifically, a continuous floating origin... I think... there's actually precious little about continuous floating origin). This worked fine for a bit.
That is until I decided I wanted to be able to "jump" to the location I want. By jump, I mean that I shift the world around me by the number of units needed to get to my destination. However, when I translate the world so that the destination point is 0x0, the object at that position is off by a noticeable amount (it should be at 0x0, but ends up in slightly different locations each time).
Is there a special way to deal with just repositioning and floating origins that I'm missing? Is there a better way to do this?
Thanks in advance.
For reference, this is the code I'm using for my floating origin:
/// <summary>
/// Function to assign the origin offset used to readjust the entities.
/// </summary>
public void MoveWorld(Vector2 offset) => _offset = offset;
/// <summary>
/// Function to make the specified entity's position the origin of the map.
/// </summary>
/// <param name="entity">Entity to use.</param>
public void MakeOrigin(Entity entity)
{
_offset = entity.Transform.Position.ToVector2();
Update(entity);
// Reset the transform to 0 because we are now the origin.
entity.Transform.LocalPosition = new Vector3(Vector2.Zero, entity.Transform.LocalPosition.Z);
}
/// <summary>Function to update the world around the primary entity.</summary>
/// <param name="originEntity">The entity considered the origin of the scene.</param>
/// <remarks>
/// <para>
/// This should be called during the <see cref="CodeBehindComponent.FinalUpdate(float)"/> method.
/// </para>
/// </remarks>
public void Update(Entity originEntity)
{
foreach (Entity entity in _map.Entities)
{
if ((entity == originEntity) || (entity.Transform.Parent is not null) || (entity is MapLayer))
{
continue;
}
entity.Transform.LocalPosition = new Vector3(entity.Transform.LocalPosition.X - _offset.X, entity.Transform.LocalPosition.Y - _offset.Y, entity.Transform.LocalPosition.Z);
}
_x = (decimal)-_offset.X;
_y = (decimal)-_offset.X;
Distance += -_offset;
}