Page 1 of 1

How to make my pong ball move.

Posted: Thu Feb 14, 2019 9:04 am
by AdityaSingh
Hey! I'm remaking Pong game with LOVE2D and Lua. Can someone tell me how can I make my ball move, give it a random angle and velocity ?
Github : https://github.com/imadityaksingh/Pong

Re: How to make my pong ball move.

Posted: Thu Feb 14, 2019 2:22 pm
by zorg
Hi and welcome to the forums!

Since your code is pretty clean, i feel like it'd be a disservice to you if i just give you code without me allowing you to actually code it (where's the fun in that / what's the point of that?) so i'll just mention a few key things:

- You'll probably want to store the position of the ball (whether it's the center coordinates or its top-left, shouldn't matter too much apart from some minor code changes)

- A simple way of doing the angle and speed would be to have a separate velocity vector, or in other words, two variables, one for the angle, one for the magnitude of the change in position.

- Your game uses the cartesian coordinate system, but the velocity vector has polar coordinates, so you'd need to convert those into cartesian values you could apply to the position of the ball each frame in update. (hint: calculate dx and dy)

Extra hint, only look at it if you can't find the conversion formula you need:
dx = speed * math.cos(angle) and dy = speed * math.sin(angle)

(Also, you'll probably want to code in wall collisions and those changing the angle of the ball; that's an excercise for the reader.) :3

Re: How to make my pong ball move.

Posted: Thu Feb 14, 2019 4:28 pm
by AdityaSingh
I stored the position of ball in ballX and ballY. But I don't know how to calculate dx and dy ?

Re: How to make my pong ball move.

Posted: Fri Feb 15, 2019 12:11 pm
by zorg
Copy the tiny extra hint line, it tells you how to.

Re: How to make my pong ball move.

Posted: Thu Feb 21, 2019 5:35 pm
by Noobly324
Try giving the ball a direction like ball.xdir and ball.ydir
Than give the x and y a velocity variable like
ball.xvel and ball.yvel
Than set it to the direction you want it to be moving in like
ball.xdir = 'left' and ball.ydir = 'up'
Than write a condition for each combo (there are only 4) like for instance:
If ball.ydir == 'up' and ball.xdir == 'right' then
ball.x = ball.x + ball.xvel
ball.y = ball.y - ball.yvel
elseif (the other 3 conditions)