Pong Example Project

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bjimenez45
:warning: Old Version Published before Godot 3 was released.

I’m playing around with the Pong demo game. I’ve changed it so that the ball bounced back and fourth between the frame of the screen. Between 0 and screen_size.

The area where I’m puzzled is this

if (ball_pos.x > screen_size.x and direction.x > 0):
direction.x= -direction.x

The above works fine. and it seems that setting -direction.x will cause the ball to move left.
When I tried to add this code it didn’t work at first.

if(ball_pos.x < 0 and direction.x<0):
direction.x= direction.x

Now my thinking was that by making direction.x a positive the ball would again change directions, but this didn’t happen. I had to end up using -direction.x to get it to change direction.

Why is this? if the direction is already negative why does a negative need to be used to change it back? is there some math going on that I’m not seeing? where a negative * a negative is a positive maybe? This has me a little confused. I hope someone could clear it up for me.

:bust_in_silhouette: Reply From: mrspapaman

You need to make the direction.x positive, so you use abs

if(ball_pos.x < 0 and direction.x < 0):
    direction.x=abs(direction.x)