RayCast Bounce Not Showing Line

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tjdwlgns612

Hey guys,

I’m using a raycast with a line2d to show my ball’s predicted trajectory.

Sometimes it works flawlessly:
Imgur

Other times, the reflected line does not show, but raycast does:
Imgur

I’ve printed out the coordinates for when the collision happens, and it all checks out. The line just does not get drawn at random angles.

Here is my section of code for bouncing raycast and drawing the line:

	# Ray 1 collision (first bounce)
if lineCast.is_colliding():
	line.add_point(pos)
	var lineCastColPoint = lineCast.get_collision_point()
	var lineCastColNormal = lineCast.get_collision_normal()
	var incoming_direction =  lineCastColPoint - lineCast.position
	var outgoing_direction = incoming_direction.bounce(lineCastColNormal)
	line.add_point(lineCastColPoint)
	
	# Ray 2 collision (second bounce)
	lineCast2.position = lineCastColPoint
	lineCast2.cast_to = outgoing_direction
	if lineCast2.is_colliding():
		var lineCast2ColPoint = lineCast2.get_collision_point()
		line.add_point(lineCast2ColPoint)
	else:
		line.add_point(lineCastColPoint + outgoing_direction)

Any suggestions? Thanks in advance!!

Looks like that full, second segment only gets drawn when there’s a collision with the second raycast. In the second image you posted, there is no collision, so I guess I wouldn’t expect that second segment.

I do see that you add an additional point to your line in the case where there is no collision, but I’m not sure what the segment length is in that case (not sure what the magnitude of outgoing_direction is). Do you maybe just have a very short second segment in that case?

jgodfrey | 2022-02-11 21:05

So even if the second ray is not colliding, it still works:
Imgur: The magic of the Internet

But then I move it just a little bit more, it disappears. And then reappears and so forth and so forth. The length of the second segment seems to be fine

tjdwlgns612 | 2022-02-11 21:33

I wonder… If print the second segment’s length right there where you create it - do you see anything odd when it ‘disappears’?

jgodfrey | 2022-02-11 21:45

Nope, tried printing the point where the line should end up and it’s 100% correct, but the line is only halfway there… weird

tjdwlgns612 | 2022-02-11 21:57

I don’t see where you call line.clear_points() in the above code (I assume it’s elsewhere?). What if you print the number of points in the line. Anything odd there? Yeah, really just grasping at straws now… :frowning:

Related, I see this, but it’s relatively old…

jgodfrey | 2022-02-11 22:09

Still the same 3 points, disappeared or not…
Tried increasing that buffer size but it didn’t fix it either.
Thank you so much for trying though, hopefully I can figure it out soon it’s driving me crazy!

tjdwlgns612 | 2022-02-11 22:23

Happy to look at the project if it’s available somewhere. Otherwise, good luck and let us know what you find…

jgodfrey | 2022-02-11 22:40