#How do I make a line to show the trajectory of the physics bounce?(top-down)

1 messages · Page 1 of 1 (latest)

potent tree
#

current code:
@tool
extends Node2D

@export var bounces : int = 2
@export var angle : int = 0

@onready var ray_cast: RayCast2D = $RayCast2D
@onready var line_2d: Line2D = $Line2D

func _process(delta: float) -> void:
if Engine.is_editor_hint():
draw_preview()

func draw_preview():
var points = []
var current_pos = Vector2.ZERO
var dir = Vector2.RIGHT.rotated(deg_to_rad(angle))

points.append(current_pos)

for i in bounces:
    ray_cast.position = current_pos
    if ray_cast.is_colliding():
        var collider = ray_cast.get_collider()
        dir = dir.bounce(collider.normal)
        
        points.append(collider.position)
    else:
        points.append(ray_cast.target_position)

line_2d.points = points
zenith zinc
#

Alr so i believe for the collision we would need the law of reflection. I am going to look into it on google. As for drawing, Line2D should be fine but we would probably need to create our ray casts dynamically depending on how many bounces you want to allow.

potent tree
#

Mh, i was thinking that I could you use a single raycast that I would then repositionate starting from the collision point

#

Also i need to add to the code some lines to redirectionate the raycast and resetting current_pos to the collision point cause I forgot

zenith zinc
#

Next question is how long should the raycast be? Too long and it could lag the game pretty hard.

potent tree
#

I can put an export variable

#

I don't think it could make the game lag? I mean it doesn't look that "hard"

zenith zinc
#

Well I’m talking about the raycast length, as I believe even if it collides early it is still checking its entire length.

#

Not 100% sure

potent tree
#

Should I put the target position to the direction * said length?

potent tree
zenith zinc
#

I think if I understand the law of reflection right, making the direction negative should reflect it?

#

Depends if you are using radians or degrees

potent tree
#

Sorry I have to leave now, if you find anything write it here and I can check tonight

#

thank you :)

potent tree
zenith zinc
#

Ofc, and ill make a test project later

zenith zinc
potent tree
#

Btw I found this very helpful video!
https://youtu.be/Mry6FdWnN7I?si=4LjnABRENuGtf4cH

I show you how to calculate the trajectory of an object taking into account collision to draw it using a Line2D. I also show you how to texture the line and animate it using a shader!
MADE FOR GODOT 3.5

🎓 Learn how to make JUICY games 👇
https://www.udemy.com/course/learn-how-to-make-a-game-juicy-in-godot-4/?referralCode=1652C74B848551E05DAE

💻...

▶ Play video
#

It has more stuff (gravity etc.) but I think it could work for me

#

I haven't tried it yet though

potent tree
#

I did it btw

#

For future reference here's the code I used:
@tool
extends Line2D
class_name Preview

@export var angle : int = 0:
set(x):
angle = x
update_trajectory()
@export var speed : int = 500:
set(x):
speed = x
update_trajectory()
@export var max_points = 200:
set(x):
max_points = x
update_trajectory()

@onready var test_collision: CharacterBody2D = $TestCollision

func update_trajectory():
if Engine.is_editor_hint():
clear_points()
var current_pos = Vector2.ZERO
var dir = Vector2.RIGHT.rotated(deg_to_rad(angle)) * speed
add_point(current_pos)
for i in max_points:
add_point(current_pos)

        if test_collision:
            var collision = test_collision.move_and_collide(dir * get_physics_process_delta_time(), false, true, true)
            if collision:
                dir = dir.bounce(collision.get_normal())
        
        current_pos += dir * get_physics_process_delta_time()
        if test_collision:
            test_collision.position = current_pos
else:
    clear_points()
    hide()