0 votes

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 (ballpos.x > screensize.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.

in Engine by (12 points)

1 Answer

0 votes

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)

by (14 points)
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 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 webmaster@godotengine.org with your username.