Godot raycast7/14/2023 ![]() With that, we can write the rest of the code: if direction.y = 0: That is, if the ray is parallel to the plane (in which case there is no intersection), so you want to check for that. Note that this code would be a division by zero when direction.y = 0. Solve for distance: distance = -origin.y/direction.y That is: origin.y + direction.y * distance = 0 We can have a parametric form of the ray like this: (origin + direction * distance).y = 0 We want to find a point along the ray, that satisfy that ( y = 0). The plane will be defined by an coordinate set to a constant, in this case y = 0. We can assume that direction is a unit vector (a vector of length one) so that distance gives you the length from origin. Var direction = camera.project_ray_normal(mouse_pos)Ĭonceptually, we define a parametric form of the ray like this: origin + direction * distance Var origin = camera.project_ray_origin(mouse_pos) We need to define our ray, and our plane.įor the ray part, the code is basically what we did for the Ra圜ast node above: var mouse_pos = get_viewport().get_mouse_position() If you want to intersect with an axis aligned plane instead of some object, we can handle it with some vector algebra. Note: make sure the Ra圜ast is enabled, it is disabled by default. You can also read get_collider which will give you the object the Ra圜ast collided with (which you can check if it is the board). You would then check $Ra圜ast.is_colliding() (which is a way to detect if the pointer is out of the board, for example), and read get_collision_point to get the position. On the other hand, project_ray_origin should give you the origin for the ray, which is the position of the Camera when the camera projection is perspective, but the method also supports orthogonal projection.Ĭalling force_update_transform and force_raycast_update is only necessary if you are moving the Ra圜ast multiple times in the same physics frame. Should give you the direction for the ray that goes toward the mouse_pos, given the camera projection and Viewport size. Pay attention to project_ray_origin and project_ray_normal. $Ra圜ast.cast_to = camera.project_ray_normal(mouse_pos) * ray_length $Ra圜 = camera.project_ray_origin(mouse_pos) Var mouse_pos = get_viewport().get_mouse_position() To make it follow the pointer, you can do this: var ray_length = 100 # some large number If you can't - or rather not - depend on pickable objects, but still have something to collide with, you can use a Ra圜ast node. Then connect the input_event signal or override the _input_event method, where you will get - among other parameters - the position in 3D of the pointer when it is over the 3D object. the board), you can set input_ray_pickable to true on it. I'm not sure I understand your limitations, so I will give you a few approaches, and hopefully this gets you to the solution along the way. I'm very new to game development and the techniques overall, any help and pointers how to best achieve the task are greatly appreciated! The Camera's project_position ( Vector2 screen_point, float z_depth ) method doesn't seem to be a viable choice either, as my camera should be able to rotate around the board freely and not be locked in the top-down view (otherwise this method would be the perfect solution, to my understanding?). I was unable to find a Node type to use as an infinite plane for ray collision. ![]() I can't, however, rely on the board, as the game board itself is floating in a "void" and coordinates around the board should be detectable (and game-wise illegal). Because the entire game happens on a plane, I'm not concerned with the Y axis - I just need to raycast from my camera, through the mouse cursor, onto the world plane and acquire the (x, 0, z) coordinates. As such, I need to frequently transform my viewport's mouse coordinates into world coordinates to move the pieces. finding a position on a texture) or the problem was caused by something else and the solution didn't apply. I've been trying to find a solution to this task and despite finding answers to similar issues, they were always somewhat overcomplicated (e.g.
0 Comments
Leave a Reply. |