[Pong]Prevent a ball from hitting the same spot endlessly?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- BoopDeePoop
- Prole
- Posts: 39
- Joined: Sun Dec 30, 2012 4:15 am
[Pong]Prevent a ball from hitting the same spot endlessly?
I'm attempting to make a pong game and I can't figure out how I can stop this from happening.
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.
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 178 times
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: [Pong]Prevent a ball from hitting the same spot endlessl
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.
- BoopDeePoop
- Prole
- Posts: 39
- Joined: Sun Dec 30, 2012 4:15 am
Re: [Pong]Prevent a ball from hitting the same spot endlessl
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?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.
- 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
The paddle. When the balls hits it, make this:BoopDeePoop wrote: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?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.
Code: Select all
function ballhits(a)
local someintensifier = 1 --default
ball.angle = a + math.random()*someintensifier
end
- BoopDeePoop
- Prole
- Posts: 39
- Joined: Sun Dec 30, 2012 4:15 am
Re: [Pong]Prevent a ball from hitting the same spot endlessl
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.HugoBDesigner wrote:The paddle. When the balls hits it, make this:BoopDeePoop wrote: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?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.math.random, without arguments, gives you a number between 0 and 1, so you can multiply it and get more "randomness"...Code: Select all
function ballhits(a) local someintensifier = 1 --default ball.angle = a + math.random()*someintensifier end
- 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
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
- 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
You're doing the wrong thing with reflection anyway. The right code (with vectors rather than angles), would be:
And what you have now is equivalent to:
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.
Code: Select all
ball.dy = -ball.dy
Code: Select all
ball.dx = -ball.dx
ball.dy = -ball.dy
Help us help you: attach a .love.
- BoopDeePoop
- Prole
- Posts: 39
- Joined: Sun Dec 30, 2012 4:15 am
Re: [Pong]Prevent a ball from hitting the same spot endlessl
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?Robin wrote:You're doing the wrong thing with reflection anyway. The right code (with vectors rather than angles), would be:
And what you have now is equivalent to:Code: Select all
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.Code: Select all
ball.dx = -ball.dx ball.dy = -ball.dy
-
- Prole
- Posts: 44
- Joined: Sun Mar 31, 2013 11:55 am
Re: [Pong]Prevent a ball from hitting the same spot endlessl
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.BoopDeePoop wrote: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?Robin wrote:You're doing the wrong thing with reflection anyway. The right code (with vectors rather than angles), would be:
And what you have now is equivalent to:Code: Select all
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.Code: Select all
ball.dx = -ball.dx ball.dy = -ball.dy
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests