Skip to content

Add automatic world offset updates for character entities to maintain floating-point precision#35

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/fix-361f151b-2e0c-4690-916c-ea31c64f8bf4
Draft

Add automatic world offset updates for character entities to maintain floating-point precision#35
Copilot wants to merge 4 commits intomainfrom
copilot/fix-361f151b-2e0c-4690-916c-ea31c64f8bf4

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 3, 2025

Overview

This PR implements automatic world offset updates that trigger when a tracked character entity moves beyond a configurable distance from the Unity origin. This feature helps maintain floating-point precision and ensures consistent gameplay as characters travel far from the origin.

Problem

Currently, the world offset in World.cs does not update automatically when character entities move significant distances from the world origin. This can lead to:

  • Floating-point precision issues as Unity transform positions get very large
  • Degraded physics and rendering quality at extreme distances
  • Potential gameplay issues for open-world or large-scale environments

Solution

The implementation adds three key components to the World class:

1. Character Tracking

// Track which character entity to monitor
StraightFour.ActiveWorld.SetTrackedCharacterEntity(playerCharacter);

2. Configurable Threshold

// Distance threshold that triggers offset updates (default: 1000 units)
public float worldOffsetUpdateThreshold = 1000f;

// Enable/disable automatic updates (default: enabled)
public bool enableAutoWorldOffsetUpdate = true;

3. Automatic Recentering

The Update() method monitors the tracked character's Unity position and automatically recenters the coordinate system when the distance exceeds the threshold:

  • Character Unity position moves to (0, 0, 0)
  • Logical (world) positions are preserved for all entities
  • All entities' Unity positions are shifted to maintain relative positions

Technical Details

Offset Calculation: newOffset = oldOffset - unityPosition

This formula ensures that:

  • Unity coordinates stay close to origin (avoiding precision loss)
  • Logical world positions remain unchanged (no teleportation)
  • All entities maintain their relative spatial relationships

Example:

Before: Character at Unity (150, 0, 0), offset (0, 0, 0), logical (150, 0, 0)
After:  Character at Unity (0, 0, 0), offset (-150, 0, 0), logical (150, 0, 0) ✓

Changes

  • World.cs: Added tracking fields, SetTrackedCharacterEntity() method, and Update() implementation
  • WorldTests.cs: Comprehensive test coverage for all update scenarios
  • docs/configuration/README.md: Full documentation with examples and usage guidelines

Benefits

  • Improved Precision: Keeps Unity transforms near origin
  • Seamless Experience: Characters can travel unlimited distances
  • Automatic: No manual intervention required
  • Configurable: Threshold adjustable per world requirements
  • Backward Compatible: Feature can be disabled; no breaking changes

Testing

Added WorldTests_WorldOffsetUpdate() test that verifies:

  • Offset updates correctly when threshold is exceeded
  • Unity positions recenter while preserving logical positions
  • No updates occur when character stays within threshold
  • Multiple sequential updates work correctly
  • Feature can be disabled via configuration flag

Closes #[issue_number]

Original prompt

This section details on the original issue you should resolve

<issue_title>Update world offset when character entity moves away from origin</issue_title>
<issue_description>Is your feature request related to a problem? Please describe.
Currently, the world offset in StraightFour/Assets/StraightFour/World.cs does not update when a character entity moves a significant distance from the world origin. This can lead to issues with floating-point precision and gameplay experience as the character moves further from the origin.

Describe the solution you'd like
Implement logic to monitor the distance of a provided character entity from the world origin. When the entity moves beyond a certain threshold, automatically update the world offset in World.cs to recenter the world around the character. This will help maintain precision and consistent gameplay.

Describe alternatives you've considered

  • Manually triggering a world offset update via user input.
  • Updating the offset only during specific events (e.g., teleportation).

Additional context
This feature will ensure better precision and stability for entities that travel far from the origin, enhancing the overall game experience.</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #34

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits October 3, 2025 21:44
Co-authored-by: dyfios <16926525+dyfios@users.noreply.github.com>
Co-authored-by: dyfios <16926525+dyfios@users.noreply.github.com>
Co-authored-by: dyfios <16926525+dyfios@users.noreply.github.com>
Copilot AI changed the title [WIP] Update world offset when character entity moves away from origin Add automatic world offset updates for character entities to maintain floating-point precision Oct 3, 2025
Copilot AI requested a review from dyfios October 3, 2025 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update world offset when character entity moves away from origin

2 participants