Created
December 28, 2019 19:57
-
-
Save regakakobigman/01877f8daab2b6c7dbd024b69d534a27 to your computer and use it in GitHub Desktop.
Bouncing rays in GDScript for lasers or beams of light
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Try passing in randf() * x for distance_from_surface or angle_adjust to get some slight randomness | |
func bounce_rays( | |
space: Physics2DDirectSpaceState, | |
initial_pos: Vector2, | |
initial_dir: Vector2, | |
count: int, | |
length: float, | |
exclude:=[], | |
distance_from_surface: float = 0.0, | |
angle_adjust: float = 0.0 | |
) -> PoolVector2Array: | |
var p1 := initial_pos | |
var p2 := initial_pos + initial_dir * length | |
var rays := PoolVector2Array([p1]) | |
for i in count: | |
var collision = space.intersect_ray(p1, p2, exclude) | |
if not collision: break | |
var bounce_dir = (collision.position - p1).normalized().bounce(collision.normal).rotated(angle_adjust) | |
p2 = bounce_dir * length + collision.position | |
p1 = collision.position + bounce_dir * epsilon + collision.normal * distance_from_surface | |
rays.append(p1) | |
return rays |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment