[Minor Feature] screen_to_world() only intersects Y=0 plane #245

Closed
opened 2026-02-06 02:56:07 +00:00 by john · 0 comments
Owner

Description

Viewport3D.screen_to_world(x, y) casts a ray from the camera and intersects it with the Y=0 ground plane. This works for flat terrain but becomes inaccurate for terrain with significant height variation, since the actual terrain surface may be at Y != 0.

Current Behavior

result = viewport.screen_to_world(160, 120)
# Returns intersection with Y=0 plane
# If terrain is at Y=3.0 at that XZ position, the returned X,Z coordinates
# will be offset from where the user actually clicked on the visible terrain

Suggested Improvement

Intersect the ray with the actual terrain mesh or heightmap surface instead of (or in addition to) the Y=0 plane. Options:

  1. Sample the heightmap at the intersection point and iteratively refine
  2. Ray-march along the ray checking against heightmap values
  3. Provide an optional y_plane parameter: screen_to_world(x, y, y_plane=0.0)

Impact

Click-to-move on hilly terrain targets slightly wrong grid coordinates. For moderate terrain (Y_SCALE < 4) the error is small, but for exaggerated terrain it becomes noticeable. The village demo uses Y_SCALE=6 which causes mild inaccuracy on hills.

Found during village integration demo development.

## Description `Viewport3D.screen_to_world(x, y)` casts a ray from the camera and intersects it with the Y=0 ground plane. This works for flat terrain but becomes inaccurate for terrain with significant height variation, since the actual terrain surface may be at Y != 0. ## Current Behavior ```python result = viewport.screen_to_world(160, 120) # Returns intersection with Y=0 plane # If terrain is at Y=3.0 at that XZ position, the returned X,Z coordinates # will be offset from where the user actually clicked on the visible terrain ``` ## Suggested Improvement Intersect the ray with the actual terrain mesh or heightmap surface instead of (or in addition to) the Y=0 plane. Options: 1. Sample the heightmap at the intersection point and iteratively refine 2. Ray-march along the ray checking against heightmap values 3. Provide an optional `y_plane` parameter: `screen_to_world(x, y, y_plane=0.0)` ## Impact Click-to-move on hilly terrain targets slightly wrong grid coordinates. For moderate terrain (Y_SCALE < 4) the error is small, but for exaggerated terrain it becomes noticeable. The village demo uses Y_SCALE=6 which causes mild inaccuracy on hills. Found during village integration demo development.
john closed this issue 2026-02-09 12:47:41 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
john/McRogueFace#245
No description provided.