-
-
Notifications
You must be signed in to change notification settings - Fork 21.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
world_to_map() returns inconsistent values when using raycast #46561
Comments
The raycast hit position is not accurate enough to retrieve the corresponding cell by position, because the result is exactly at the edge between two cells, and mathematical approximations will give you inconsistent results. There's currently no easy way to get the proper cell that was hit by the raycast though, so that needs to be fixed in the As a workaround, in your case you can add a little bit of extra distance to your raycast hit position to make sure it's inside the box when a hit occurs, and it will give you consistent results:
|
Hey @pouleyKetchoupp , thanks for all information! The workaround works very well! |
With Godot 4.1.2 you can actually get the normal vector of the hit position to calculate which cell was hit and which cell was in front of that cell. Like this: func _input(event: InputEvent):
var crosshair_position = camera.get_window().size / 2 # cursor at the center, because I'm using MOUSE_MODE_CAPTURED
var ray_start = camera.project_ray_origin(crosshair_position)
var ray_dir = camera.project_ray_normal(crosshair_position)
var ray_end = ray_start + ray_dir * RAY_LENGTH
var intersect_ray_params = PhysicsRayQueryParameters3D.create(ray_start, ray_end, 0xFFFFFFFF, [get_rid()])
var result = get_world_3d().direct_space_state.intersect_ray(intersect_ray_params)
if not result or not is_instance_of(result.collider, GridMap):
return
var grid_map = (result.collider as GridMap)
var normal_offset = grid_map.cell_size / 2 * result.normal
# the cell that has been hit
var cell_world_position: Vector3 = result.position - normal_offset
var cell_position = grid_map.local_to_map(cell_world_position)
# the "empty" cell in front of the cell being hit
var empty_world_position: Vector3 = result.position + normal_offset
var empty_position = grid_map.local_to_map(empty_world_position) |
Godot version:
3.2.3-stable_x11.64 (Also tested in 3.1.2-stable_x11.64)
OS/device including version:
Manjaro 20.2.1 Nibia
Issue description:
The
world_to_map()
sometimes return inconsistent values when using theintersect_ray()
method. See the output from the image below:Looking the z axis from output you will see an inconsistent values between the first and second image.
I also tried to reproduce the
world_to_map
method, dividing theresult.position
by thecell_size
and then using thefloor()
method, but in both cases I get the same inconsistent value.Steps to reproduce:
result
from the ray.result.position
to theworld_to_map()
.Minimal reproduction project:
GridMapRaycastBug.zip
The text was updated successfully, but these errors were encountered: