Im trying to make a ball bounce but vector2.bounce returns -0,-0. Why is this happening and how can i fix it.

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

When the ball hits a wall i run this code

func _on_wall_ball_bounce(normalvelocity):
    velocity =  velocity.bounce(normalvelocity)

The result in the console is

(1, 1)(0.000092, 15)
(-0, -0)

Any help is apreciated

Try normalizing the normal velocity.

magicalogic | 2022-12-14 18:14

:bust_in_silhouette: Reply From: jgodfrey

Hmmm… I don’t see any mention of it in the docs, the normalizing that input vector in the bounce call seems to return more expected results…

var new = velocity.bounce(n_velocity.normalized())

Ah, after looking at the source, this IS the problem. And, you should have an error in the Debug window as follows if your vector is NOT normalized:

E 0:00:01.191 reflect: The normal Vector2 must be normalized. <C++ Error> Condition "!p_normal.is_normalized()" is true. Returned: Vector2() <C++ Source> core/math/vector2.cpp:193 @ reflect() <Stack Trace> @ _ready()

jgodfrey | 2022-12-14 18:37