The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

I have taken the demo code from https://docs.godotengine.org/en/stable/tutorials/physics/ray-casting.html and am trying to use it to figure out which node in the scene tree my Oculus Quest controller is pointing at.

var offset = Vector3(0,0,-100)
var space_state = get_world().direct_space_state
var xform = controller_node.global_transform
var start:Vector3 = xform.xform(Vector3(0,0,0))
var end = xform.xform(offset)
var result:Dictionary = space_state.intersect_ray(start, end)       
var rid = result["collider"]

typeof(rid) == 17, so it is an RID. I do not know how to get a Node from the RID, so I can not modify the node.

How can I find the first Node struck by an arbitrary ray?

Godot version v3.2.stable.custom_build (Ubuntu)
in Engine by (36 points)

1 Answer

+1 vote
Best answer

According to the docs, 17 is TYPE_OBJECT (as expected in your described case). A TYPE_RID is 16. Also, note the Dictionary that's returned contains a separaterid key, which should hold the RID value...

https://docs.godotengine.org/en/3.2/classes/[email protected]?highlight=GlobalScope#globalscope

(search the above for TYPE_RID).

by (22,674 points)
selected by

Yup, a quick test shows that result["collider"] does indeed hold a reference to the intersecting body

func _ready():
  var space_state = get_world().direct_space_state
  var result = space_state.intersect_ray(Vector3.ZERO, Vector3(5,0,0))
  var collider = result.collider
  print(collider)

prints StaticBody:[StaticBody:1264]

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 Frequently asked questions and 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 [email protected] with your username.