Help: Advice on how to make my ball in Pong project "bounce"

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Help: Advice on how to make my ball in Pong project "bounce"

Post by nice »

Hello everyone!
I'm currently working on trying to replicate Pong to learn how Löve2D/Lua works and I think I've come to the point where I want the ball in my game to bounce around. As you can see in the gif that I've attached that the Ball just moves to the left and doesn't bounce back against the Paddle.

I think that I've managed to break down on how the Ball works (which you can find in "Ball Behaviour.rtf") but I would like to start with the Ball at least bounce back against the Paddle.
And because I'm trying to learn Löve2D/lua I would appreciate if you could give me hints on it could be done without revealing to much.

I have also attached my project if you want to view what I'm doing.
Thanks!
Attachments
Pong.zip
(4.1 KiB) Downloaded 52 times
PaddleBallExample.gif
PaddleBallExample.gif (5.7 KiB) Viewed 5012 times
Ball Behaviour.rtf
(1.59 KiB) Downloaded 53 times
:awesome: Have a good day! :ultraglee:
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Help: Advice on how to make my ball in Pong project "bounce"

Post by raidho36 »

That's the field of collision and response. You need to detect collision and issue appropriate response action. You could use existing physics libraries for that, or do all the math yourself.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Help: Advice on how to make my ball in Pong project "bounce"

Post by Jasoco »

Look into Bump. It'll make it easier.

Otherwise you'll have to do the math yourself. A bunch of if checks on the position of the ball and the paddle.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Help: Advice on how to make my ball in Pong project "bounce"

Post by airstruck »

The math shouldn't be all that much trouble, really. We can assume the ball only collides with the right edge of the paddle, so if the ball's x-velocity is negative (it's traveling left), and the ball's position is close enough to the paddle's position that they overlap, it should bounce.

To determine if two objects are close enough to overlap, you just need to compare the distance between their centers to their sizes. For example, if two circles each have a radius of 2 units and their centers are 3 units apart, they overlap.

To make the ball bounce you can just reverse its x-velocity (multiply it by -1).
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: Help: Advice on how to make my ball in Pong project "bounce"

Post by nice »

Jasoco wrote: Wed Mar 08, 2017 1:31 am Look into Bump. It'll make it easier.

Otherwise you'll have to do the math yourself. A bunch of if checks on the position of the ball and the paddle.
I'll take a look at Bump but right now I think my problem is to know when my ball and paddle collide with eachother.
I'm thinking something like "if ball image collides with paddle image then do this".
:awesome: Have a good day! :ultraglee:
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: Help: Advice on how to make my ball in Pong project "bounce"

Post by nice »

raidho36 wrote: Tue Mar 07, 2017 11:22 pm That's the field of collision and response. You need to detect collision and issue appropriate response action. You could use existing physics libraries for that, or do all the math yourself.
Yeah I figured that collision detection will be the problem and Jasoco recommended that I look up Bump.

I feel that I would like to do the math myself but as I mentioned I need to know when my two "objects" collide with each other..
:awesome: Have a good day! :ultraglee:
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Help: Advice on how to make my ball in Pong project "bounce"

Post by airstruck »

As I mentioned about 20 minutes ago, they collide when they're close enough to overlap. If your paddle is 5 units from its center to its edge, and the ball's radius is 8 units, then any distance along x of 13 or less means they overlap along x. Do the same for y, if they overlap along both x and y then their AABBs overlap and you can consider them to be colliding (except you probably only want to do this when the ball is traveling left so it won't get stuck in the paddle).
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Help: Advice on how to make my ball in Pong project "bounce"

Post by raidho36 »

Thinking of everything that goes on in the computer as mathematical formulas will get you across much faster. Don't try to solve your problem in XYZ way, solve it mathematically. Grab a whiteboard and write down some equations, then punch them into your program.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 84 guests