How to use linear_velocity.reflect or bounce

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

I have searched google and the forums but can’t find out what you use linear_velocity.reflect() or linear_velocity.bounce() for.

Could someone help me?

:bust_in_silhouette: Reply From: Diet Estus

bounce() and reflect() are methods of the Vector2 and Vector3 classes. They are used, respectively, to bounce and reflect vectors off of surfaces.

Information about them can be found in the docs. (See Vector2 and Vector3.)

Linear velocity is usually represented as a vector, and it is normal to call these methods on it in “collision response” code, that is, code describing what to do with velocity after a collision.

Is there a quick example of code that shows an object with linear velocity hitting the screen edge and reflecting off?

ondesic | 2018-03-16 16:25

Use StaticBody2D with collision shapes for walls at the edge of the screen and try something like this:

var collision = collide_and_move(linear_velocity)
if collision:
    var normal = collision.normal
    var remaining_vel = collision.remainder

I’m not at my computer though and haven’t checked to see if this works though.

Diet Estus | 2018-03-16 16:41