0 votes

When the ball hits a wall i run this code

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

The result in the console is

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

Any help is apreciated

Godot version 3.5.1
in Engine by (12 points)

Try normalizing the normal velocity.

1 Answer

0 votes

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())
by (19,272 points)

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> Main.gd:54 @ _ready()

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.