Ray-casting from the camera in 3D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DimitriyPS
:warning: Old Version Published before Godot 3 was released.

I’m learning Godot to develop in 3D. I want to build a RayCast from the camera as stated here:link

# use global coordinates, not local to node
var result = space_state.intersect_ray( Vector3(x1,y1,z1), Vector3(x2,y2,z2) )
  1. Please tell me how to get it: Vector3(x1,y1,z1) and Vector3(x2,y2,z2) in global space?

  2. Vector3(x2,y2,z2) - only forms a direction or limits the length of the beam (cut)?

At the end of that article there is a code example, did you check it?

Bojidar Marinov | 2016-03-24 09:23

how Bojidar Marinov said, there is a code example:

const ray_length = 1000

func _input(ev):
    if ev.type==InputEvent.MOUSE_BUTTON and ev.pressed and ev.button_index==1:

          var camera = get_node("camera")
          var from = camera.project_ray_origin(ev.pos)
          var to = from + camera.project_ray_normal(ev.pos) * ray_length

swipis | 2016-03-24 09:41

I am not able to test the code (I’m not at my PC). Just have some free time and I’m studying theory. In this case, I beg to treat with understanding, I do not know English. Reading the documentation helps me Gogle. However, the result of his translation is very difficult to perceive.

In GDScript I’m a beginner, maybe that’s why I don’t understand how this code is consistent with the method:

result = space_state.intersect_ray

I correctly understand what “var from” this is what I in the first post as noted Vector3(x1,y1,z1)? And “var to” Vector3(x2,y2,z2)?

Can I limit the length of the RayCast (to make it portion)?

DimitriyPS | 2016-03-24 10:09

Im new here as well, so I just did this in my project as well, “var from” ray “start” position and “var to” direction.
here is code I’m using:

	var directState = PhysicsServer.space_get_direct_state(cam_node.get_world().get_space())
var result = directState.intersect_ray(from, to, [self])

and ray length is var so you can do what ever length you want.

swipis | 2016-03-24 22:04

How did you get the var to (direction)?

RpgGOD | 2016-03-25 07:17

:bust_in_silhouette: Reply From: DimitriyPS

I have a few questions. I built the beam, found that there is an intersection…

  1. How do I find out how many intersections with scene nodes?
  2. How do I get to link to the nearest node which crossed the ray?
:bust_in_silhouette: Reply From: unfa

Here’s a script I made to make flares hide if they are not directly visible to the camera

extends MeshInstance # use whatever floats your boat

func _physics_process(delta):

	# get current camera
	var camera = get_viewport().get_camera()
	
	# intersect a ray betwee nthe camera and the self
	var result = get_world().direct_space_state.intersect_ray(camera.global_transform.origin, self.global_transform.origin, [self])
	
	# if we hit nothing - the flare is visible, so we show it
	if result.empty():
		self.show()
	else: # othwerwise we hide it
		self.hide()

As you can see, you can get global location of any object like this:

 var pos = node.global_transform.origin # returns a global position of the node in Vector3