[Pong]Prevent a ball from hitting the same spot endlessly?

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
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

[Pong]Prevent a ball from hitting the same spot endlessly?

Post by BoopDeePoop »

I'm attempting to make a pong game and I can't figure out how I can stop this from happening.
Image
I feel like I'm doing a lot of things wrong with this game, it's working to say the least though. How would I go about fixing this small thing?
Here's my .love if you need it.
Attachments
pong.love
(16.75 KiB) Downloaded 173 times
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: [Pong]Prevent a ball from hitting the same spot endlessl

Post by DaedalusYoung »

It should be fixed by adding a tiny random value to the bouncing angle. Then it will never bounce back the same twice and is not likely to hit the exact same spot over and over.
User avatar
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

Re: [Pong]Prevent a ball from hitting the same spot endlessl

Post by BoopDeePoop »

DaedalusYoung wrote:It should be fixed by adding a tiny random value to the bouncing angle. Then it will never bounce back the same twice and is not likely to hit the exact same spot over and over.
I've been trying that and I've been having no luck so far. Do I add that value to when it hits the paddle, or the walls?
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: [Pong]Prevent a ball from hitting the same spot endlessl

Post by HugoBDesigner »

BoopDeePoop wrote:
DaedalusYoung wrote:It should be fixed by adding a tiny random value to the bouncing angle. Then it will never bounce back the same twice and is not likely to hit the exact same spot over and over.
I've been trying that and I've been having no luck so far. Do I add that value to when it hits the paddle, or the walls?
The paddle. When the balls hits it, make this:

Code: Select all

function ballhits(a)
    local someintensifier = 1 --default
    ball.angle = a + math.random()*someintensifier
end
math.random, without arguments, gives you a number between 0 and 1, so you can multiply it and get more "randomness"...
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

Re: [Pong]Prevent a ball from hitting the same spot endlessl

Post by BoopDeePoop »

HugoBDesigner wrote:
BoopDeePoop wrote:
DaedalusYoung wrote:It should be fixed by adding a tiny random value to the bouncing angle. Then it will never bounce back the same twice and is not likely to hit the exact same spot over and over.
I've been trying that and I've been having no luck so far. Do I add that value to when it hits the paddle, or the walls?
The paddle. When the balls hits it, make this:

Code: Select all

function ballhits(a)
    local someintensifier = 1 --default
    ball.angle = a + math.random()*someintensifier
end
math.random, without arguments, gives you a number between 0 and 1, so you can multiply it and get more "randomness"...
So do I need to change my collision function completely then? Atm I'm reversing the x and y velocities and then multiplying them by math.random() numbers when the ball collides with the paddle.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: [Pong]Prevent a ball from hitting the same spot endlessl

Post by HugoBDesigner »

The main problem is that you're getting collisions as "left" and "right" only (based on red and green sides). I made the collision more random, but it doesn't change it much. If you can, make it so you get the angle based on where on the platform-thing the ball hit. Also, are you randomizing it on wall collisions? Because that'd make a BIG difference :)
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: [Pong]Prevent a ball from hitting the same spot endlessl

Post by Robin »

You're doing the wrong thing with reflection anyway. The right code (with vectors rather than angles), would be:

Code: Select all

ball.dy = -ball.dy
And what you have now is equivalent to:

Code: Select all

ball.dx = -ball.dx
ball.dy = -ball.dy
I can't be bothered to figure out what that means with angles exactly, but it should be reflected along one of the axes instead of rotated by 180 degrees.
Help us help you: attach a .love.
User avatar
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

Re: [Pong]Prevent a ball from hitting the same spot endlessl

Post by BoopDeePoop »

Robin wrote:You're doing the wrong thing with reflection anyway. The right code (with vectors rather than angles), would be:

Code: Select all

ball.dy = -ball.dy
And what you have now is equivalent to:

Code: Select all

ball.dx = -ball.dx
ball.dy = -ball.dy
I can't be bothered to figure out what that means with angles exactly, but it should be reflected along one of the axes instead of rotated by 180 degrees.
I'm not quite sure I know what you mean by that. Instead of the ball's velocity being updated each frame, I should update it's dx and dy? What do those vars mean?
mikeisinlove
Prole
Posts: 44
Joined: Sun Mar 31, 2013 11:55 am

Re: [Pong]Prevent a ball from hitting the same spot endlessl

Post by mikeisinlove »

BoopDeePoop wrote:
Robin wrote:You're doing the wrong thing with reflection anyway. The right code (with vectors rather than angles), would be:

Code: Select all

ball.dy = -ball.dy
And what you have now is equivalent to:

Code: Select all

ball.dx = -ball.dx
ball.dy = -ball.dy
I can't be bothered to figure out what that means with angles exactly, but it should be reflected along one of the axes instead of rotated by 180 degrees.
I'm not quite sure I know what you mean by that. Instead of the ball's velocity being updated each frame, I should update it's dx and dy? What do those vars mean?
d is frequently used in place of delta (uppercase greek delta: Δ) used most often in math as the symbol for change in a value, the change in x being the speed in the x direction it's moving at. Right now you're reversing the x value, you should just reverse the y value and keep the position that the ball hits the paddle and the direction/speed of the paddle as a smaller influence on the deflection. You can add a pinch of random too if you want, though if you decide to then you should keep it hardly noticeable or it will frustrate players as it detracts from the feeling of using pure skill to master something.
Post Reply

Who is online

Users browsing this forum: No registered users and 103 guests