Info and examples on Ray-casting2D - How to properly use it?

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

Hello friends,
Wanted to get more info and if possible, projects on this subject, as I am getting some difficulties making use of it.

Looked around the web and found a nice series but written for Unity and I was trying right now to grasp and apply those concepts, but I feel I’d just confuse myself trying to understand a concept and working with it in a different engine.

I already looked at the article at Ray-casting over the documentation, but as I said, I couldn’t grasp those concepts after trying to learn for 2 days by myself.

Thanks in advance.

It’s rather simple

  1. Specify two points in space (and some info about masks, exceptions, etc), begniing and end
  2. If the ray hit something, you get a dictionary with the collision information, the usual such as point, normal, object and shape, etc.
  3. If they ray didn’t collide, the returned dictionary is empty

Is there anything else that you need to know?

Juan Linietsky | 2016-02-23 17:36

Thanks, but yeah, as it is, it is hard to grasp its uses compared to, let’s say the kinematic2D demos in the examples.
It is still too abstract gaming-wise for me and some new guys that are trying to learn.

brunosxs | 2016-02-23 18:37

:bust_in_silhouette: Reply From: MrMonk

I’m working with some ray casting so this is some code I’m using:

var space_state = get_world_2d().get_direct_space_state()
var result =space_state.intersect_ray(initial_pos,final_pos.linear_interpolate(initial_pos,2.0) ,[myRigidBody] )
	if(result.size():
		interpolatedLine = result.position

So, your colliding object should be an object part of the physics world (static body, rigid body or so). It cannot be a simple sprite on the screen.
In the code above, the interpolated line will have the collision points if the line starting from initial_pos to final_pos has hit something.
This is how I use it from code. There is the option to use a RayCast node, but I have not used it yet.

Do you know a way to show those rays during gameplay?
It is only possible with the raycaste node…
What I am using is printing its info when it colides, but it would be nice to get a more visual representation.

brunosxs | 2016-02-23 21:40

I’m just drawing a line for now:
drawline(initial pos, interpolatedLine). The drawing has to be done from function _draw()

MrMonk | 2016-02-24 07:46

:bust_in_silhouette: Reply From: umfk

So MrMonk is using the version that is a little more complicated (because you go deeper into the Physics system). For simple things you don’t have to use the direct space state, here are a few lines from my code:

rc.set_cast_to(Vector2(beam_length,0))
if rc.is_colliding():
	has_collided = true
	var collision_point = rc.get_collision_point()
	var object_hit = rc.get_collider()

rc is a Raycast2D node and with the function set_cast_to I set the end of the ray. I then use is_colliding() to see if the ray is currently colliding with anything. If it is I can get the collision point and the object that was hit first through the functions above.

So, in what sittuations the way of using get_space_state would be benefitial?

brunosxs | 2016-02-24 09:31

Note that RayCast and RayCast2D won’t update their result until next frame, AFAIK.

Bojidar Marinov | 2016-02-24 13:02

Oh wow, really? So the example in my answer gives me the collision point and the collider with last frames endpoint still set? That is indeed very interesting…
In my application I’m only interested in the length of the ray since the rotation is controlled by the parent, so I guess I shouldn’t change the endpoint at all and instead compare the distance of the first collider to my desired length.

umfk | 2016-02-24 13:48

Godot 3.0 update: you can use force_raycast_update() to get immediate information without having to wait for the next frame to update.

Xrayez | 2018-01-04 17:46