Calculating Reflected Angles WITHOUT using slopes

RepInt

New member
Joined
Sep 2, 2012
Messages
2
I've recently been using a program to make small games, and I've been working out a version of PONG. There's just one problem: this program cannot use angles or slopes or do any easier method for finding a result. This is the issue:

-When the ball hits the paddle (either one), it needs to deflect off of the paddle at an inverse angle to the one it hit at. The only values I have to work with are the x-position, y-position, innumerable variables to set values to, and basic (and some advanced) operators (+, -, /, *, square root, trig functions...)

Here's the question: How, using what I have, can I find where the ball can go? (assuming it travels in the same direction until it hits something)

I've experimented with pythagorean theorem and other such things, but I'm not clear as to the mathematical boundaries of doing something more complex...Can anyone try to solve this out for me?

Thanks in advance,
RepInt
 
There should be some sort of variable (or 2) to keep track of the current motion of the ball, so say the ball is currently moving 1 pixel right and 3 down for every screen redraw. If ball hits wall change 3 down to 3 up. If ball hits paddle, changes 1 right to 1 left. Of course this becomes a very boring game like this so you might have to change these around using random functions and whatnot.
 
Unfortunately, the only variables I have pertaining to the motion of the ball are the x and y positions. So, all I can do to keep "track" of the motion is simply to have a near-constant sending of the x and y values...Point being, normallly I should just be able to take the y-position of the ball (if its touching a paddle) and multiply it by -1, then take the x position and multiply it by -1, too; but in the beginning of the game, the ball starts at (0,0) and gets sent to a random position incrementally, checking every change in position whether its touching either paddle or not. When it starts at (0,0) multiplying by -1 doesn't really do much...
 
the only variables I have
What language are you coding in where you can't create new variables? If you can't it's time to kick it to the curb. -_-

Lets assume the dimensions are 20*10 and the origin is the lower left corner of the pong rectangle.

When you say take the position multiplied by -1 it doesnt make sense. If the ball hits the paddle at (0,8) then just saying its new position is (0,-8) means the ball just teleported off to the bottom of the screen which isn't drawn.

Anyways if all you have are positions then you need something like currentPositionX, currentPositionY, lastPositionX, lastPositionY
movementX = (currentPositionX-lastPositionX)
movementY = (currentPositionY-lastPositionY)

Then do something like:
If lastPositionX=0 or 20 check for paddle, if true movementX=-movementX, if false game over.
If last position Y = 0 or 10 check for wall, If true movementY=-movementY
currentPositionX=lastPositionX+movementX
currentPositionY=lastPositionY+movementY
redraw screen
lastPosition=currentPosition
loop

This is all just very basic conceptual ideas given I don't know what language you are using. I'm sure there are much more elegant ways. For example, I'm sure you could tidy things up by reducing currentPositionX and currentPositionY to just a single 1*2 currentPosition array. You say WITHOUT using slopes, but (movementY/movementX) is essentially slope, it's kind of hard not to use it for a ball moving in straight lines.
 
Last edited:
Top