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
How to make my pong ball move.
- zorg
- Party member
- Posts: 3468
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: How to make my pong ball move.
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.)
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.)
Me and my stuff True 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.
-
- Prole
- Posts: 3
- Joined: Thu Feb 14, 2019 8:57 am
Re: How to make my pong ball move.
I stored the position of ball in ballX and ballY. But I don't know how to calculate dx and dy ?
- zorg
- Party member
- Posts: 3468
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: How to make my pong ball move.
Copy the tiny extra hint line, it tells you how to.
Me and my stuff True 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.
Re: How to make my pong ball move.
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)
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)
Who is online
Users browsing this forum: Ahrefs [Bot] and 7 guests