0 votes

Hey guys,

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

Sometimes it works flawlessly:
https://imgur.com/2wGUvTT

Other times, the reflected line does not show, but raycast does:
https://imgur.com/94Jpuyt

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!!

in Engine by (27 points)

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?

So even if the second ray is not colliding, it still works:
https://imgur.com/92o61IX

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

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

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

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... :(

Related, I see this, but it's relatively old...

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!

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

Please log in or register to answer this question.

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.