+1 vote

I'm trying to detect the x and zcoordinates of the point at which a projected line from the camera intersects with the 0 coordinate in y. My goal is to achieve something similar to this:
enter image description here(That cube isn't being dragged, it's being drawn at the appropriate coordinates, and its Y position is always 0)

How can I do this?

I understand how to work with raycasting, but I don't know if and how I can use it without a collider.

I can achieve that effect with an Area (with a plane shape) and an input event signal, but then it also intersects with other physics objects that are in the scene that also have input event signals, and that seems to interfere with it.

asked Aug 7, 2018 in Engine by woopdeedoo (185 points)
edited Aug 7, 2018 by woopdeedoo

2 Answers

0 votes


Will that be what you are looking for? Looks like a voxel waiting to be placed :-)

answered Aug 16, 2018 by sxkod (68 points)
0 votes

Check this Stack Overflow answer by Mateen Ulhaq to see how to solve a ray plane intersection. To use the code in Godot, we could do something like this:

func _input(ev):
if ev is InputEventMouseMotion:
    var camera = get_node("camera")
    var camera_from = camera.project_ray_origin(ev.position)
    var camera_to = camera.project_ray_normal(ev.position)

    var n = Vector3(0, 1, 0) # plane normal
    var p = camera_from # ray origin
    var v = camera_to # ray direction
    var d = 0 # distance of the plane from origin
    var t = - (n.dot(p) + d) / n.dot(v) # solving for plain/ray intersection

    var block_position = p + t * v

Assuming our camera node is named "camera". If we set the translation for block to the result, it will start following the mouse. To constrain the movement to individual cells, we can divide the result by cell size and round:

    var block_size = 2
    block_position.x = round(block_position.x / block_size) * block_size
    block_position.z = round(block_position.z / block_size) * block_size
    block.translation = block_position
answered Apr 13 by Lukaison (14 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.