I got an intersect ray to bounce once but I could not figure out how to keep it bouncing. From internet I understand the concept, use for loop to place a new ray at bounced position and repeat. Sounds simple but when I tried to implement it I got totally stuck.
Here's what I have so far.
-Cast a first ray from shot position to forward
-If hits, get a new shot position from result and a bounce direction.
-Go into for loop and cast 2nd ray from new position/direction.
-If hits, get a new shot position from 2nd ray.
-??? This is where I'm stuck.
var max_reflections :int = 10
var cast_length = 100
var from : Vector3
var to : Vector3
var query : PhysicsRayQueryParameters3D
var result : Dictionary
### 1ST CAST
from = marker3d.global_position
to = marker3d.to_global(Vector3(0,0,-cast_length))
query = PhysicsRayQueryParameters3D.create(from, to)
result = space_state.intersect_ray(query) ### 1ST COLLSIION
if result:
line(from, result["position"], Color(1, 0, 0), 1) ### 1ST LINE
else:
line(from, to, Color(1, 0, 0), 1)
if !result:
return
### NEW DIRECTION
var result_position = result["position"]
var result_normal = result["normal"]
var shooting_direction = result_position - from
var target_direction = shooting_direction.bounce(result_normal)
line(result_position, result_position + target_direction * cast_length, Color(1, 0, 0), 1) ### 2ND LINE
for i in range(1, max_reflections):
##### 2ND CAST
from = result["position"]
to = result["position"] + target_direction.normalized() * cast_length
query = PhysicsRayQueryParameters3D.create(from, to)
result = space_state.intersect_ray(query) ### 2ND COLLSIION
if result:
_ball(result) # Placing a visible mesh ball for debugging
else:
line(from, to, Color(1, 0, 0), 1)
if !result:
line(from, to, Color(1, 0, 0), 1)
return
#### HOW TO SET NEW DIRECTION FOR LOOPING???
i += 1
if i >= max_reflections:
break