Page 1 of 1
[Pong]Prevent a ball from hitting the same spot endlessly?
Posted: Sun Apr 06, 2014 4:10 pm
by BoopDeePoop
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.
Re: [Pong]Prevent a ball from hitting the same spot endlessl
Posted: Sun Apr 06, 2014 4:13 pm
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.
Re: [Pong]Prevent a ball from hitting the same spot endlessl
Posted: Sun Apr 06, 2014 4:17 pm
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?
Re: [Pong]Prevent a ball from hitting the same spot endlessl
Posted: Sun Apr 06, 2014 4:22 pm
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"...
Re: [Pong]Prevent a ball from hitting the same spot endlessl
Posted: Sun Apr 06, 2014 4:50 pm
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.
Re: [Pong]Prevent a ball from hitting the same spot endlessl
Posted: Sun Apr 06, 2014 7:36 pm
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
Re: [Pong]Prevent a ball from hitting the same spot endlessl
Posted: Sun Apr 06, 2014 9:48 pm
by Robin
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.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.
Re: [Pong]Prevent a ball from hitting the same spot endlessl
Posted: Mon Apr 07, 2014 1:50 am
by BoopDeePoop
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.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?
Re: [Pong]Prevent a ball from hitting the same spot endlessl
Posted: Mon Apr 07, 2014 3:02 am
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:
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.