+3 votes

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)?

in Engine by (355 points)

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

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

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)?

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.

How did you get the var to (direction)?

2 Answers

+2 votes

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?
by (355 points)
+1 vote

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
by (199 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.