0 votes

I'm trying to implement a simple tile-based editor using RayCast. What I'm doing is simply performing a raycast from the mouse, and then flooring down the result:

func _physics_process(_delta):

  var mouse = get_viewport().get_mouse_position()

  translation = camera.project_ray_origin(mouse)
  cast_to = camera.project_ray_normal(mouse) * ray_length

  if is_colliding():
    target_position = get_collision_point().floor()
    target_normal = get_collision_normal().round()
    ghost.translation = target_position

The problem is that due to float imprecisions, the floored raycast result ends up jerking around a lot, and it's generally quite annoying to place blocks as sometimes the result ends up being inside of my floor cube mesh.

Is there any fairly clean way I can solve this?

Godot version 3.2.3.stable
in Engine by (24 points)

(moved to answers)

1 Answer

0 votes
Best answer

Alright, I figured it out:

target_position = (target_position + target_normal * epsilon).floor()

where epsilon is some small value like 0.01. This biases the selection a bit towards the normal of the selection, thus any fluctuations due to floating point error are mitigated.

by (24 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.