How to make my pong ball move.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
AdityaSingh
Prole
Posts: 3
Joined: Thu Feb 14, 2019 8:57 am

How to make my pong ball move.

Post 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
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to make my pong ball move.

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
AdityaSingh
Prole
Posts: 3
Joined: Thu Feb 14, 2019 8:57 am

Re: How to make my pong ball move.

Post by AdityaSingh »

I stored the position of ball in ballX and ballY. But I don't know how to calculate dx and dy ?
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to make my pong ball move.

Post by zorg »

Copy the tiny extra hint line, it tells you how to.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Noobly324
Prole
Posts: 3
Joined: Thu Feb 21, 2019 5:20 pm

Re: How to make my pong ball move.

Post 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)
Post Reply

Who is online

Users browsing this forum: No registered users and 233 guests