Public Group

# Pong math question.

This topic is 4025 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a ball travelling in a direction at a certain speed. My direction is an angle from 0 - 360... How can I determine the next X,Y based on the speed and direction... (yes i need to learn more math :'( i'm in college again so i'll learn in a year or so all of it) I have: x,y, speed, direction and I need to know the next x, y My problem I think is taking degrees speed and current location and figuring out the next location... Can anyone help me i'm stupid. AM I THINKING OF SPEED IN THE WRONG SENSE? SHOULD I BE THINKING OF SPEED IN TERMS OF XSPEED YSPEED? -durfy [Edited by - Durfy on July 7, 2007 10:04:05 PM]

##### Share on other sites
If the ball hits a wall on its left or right multiply its X speed by -1, so it then moves in the opposite direction. Same for Y.

There is probably a better way but this works fine.

##### Share on other sites
If an object is traveling at s units/second, then the distance it will travel in the X and Y directions in t seconds is:
    x = st cos θ    y = st sin θ
The code would location something like this:
    x += s * t * cos( angle ); // Note: In C, angles are in radians, not degrees    y += s * t * sin( angle );

In 2D, using angles to represent direction works well; however, in 3D you would use a vector to represent direction. Generally, you would use a vector to represent velocity, too. Here is how it would look using vectors. Here, X is the current position, X0 is the original position, V is the velocity. All are vectors. t is the elapsed time.
    X = X0 + Vt

##### Share on other sites
Quote:
 Original post by DurfySHOULD I BE THINKING OF SPEED IN TERMS OF XSPEED YSPEED?
The way you're thinking of speed will work, or you can think of speed in XSpeed and YSpeed. The two spaces are completely independant of eachother [something's x position is not a function of it's y position], and any time you have these independant variables, you can consider them completely seperately.

Often times vectors [not STL vectors in, but mathematical vectors] are used to represent these sorts of values for convenience [as seen in the bottom of JohnBolton's post]

##### Share on other sites
Quote:
 Original post by JohnBoltonIf an object is traveling at s units/second, then the distance it will travel in the X and Y directions in t seconds is:  x = st cos θ y = st sin θ The code would location something like this:  x += s * t * cos( angle ); // Note: In C, angles are in radians, not degrees y += s * t * sin( angle ); In 2D, using angles to represent direction works well; however, in 3D you would use a vector to represent direction. Generally, you would use a vector to represent velocity, too. Here is how it would look using vectors. Here, X is the current position, X0 is the original position, V is the velocity. All are vectors. t is the elapsed time. X = X0 + Vt

Thanks a lot john bolton. I will attempt using your methods they look mathmatically sound to a mathmatical mad man :-) ... Also I will take into consideration the use of vectors when I shift my game over to use 3d. I would rather have taken some basic math courses dealing with vectors before getting too wrapped up in them.

I have a question...
when i try x+=cos(180) y+=sin(180)
i get x+= (-1) y += (0)
so if i start out at 0,0
that puts me at (-1,0)
i wouldn't think an angle of 180 would put me to the left i thought 180 would be to the right?

I think i may be thinking of the angles in the wrong perspective
i was thinking of them in a clockwize motion... I believe the way it is working is counter-clockwize (0, 90, 180 , 270, 360)

Thanks so much,
durfy

[Edited by - Durfy on July 8, 2007 10:16:01 AM]

##### Share on other sites
First off, angles in math start out pointing out to the right, and you add to them in a counterclockwise direction. That is: 0deg == right, 90deg == up, 180 == left 270 == down and 360 == right again. Also, in the functions sin() and cos(), the computer usually expects radians. 360 deg == 2pi rad.

Second, even though you can represent the speed with an angle, you simply use that to find a vector, so why not use it directly. Much more easy that way.

Hope this helps.

##### Share on other sites
Thanks realcore i figured that was how the angles worked... Also I am converting my degrees to radians. double radians = degrees * PI/180

Now say my ball is to hit a wall or paddle at an angle... To calculate the reflection how would I do that? (keeping in mind that a paddle may also have velocity)
Thanks,
durfy

##### Share on other sites
Question is this the way I calculate the bounce?
if (collision) {
ball.setAngle(360 - ball.getAngle)
}

EDIT: Crap this doesnt work for all angles.. when it hit the bottom at a 45 deg angle it bounced off at 270deg the correct one.... but when it hit the side wall at 270deg it would bounce off at 90 :-/ i need to know how to determine basically the opposite of an angle?
thanks,
durfy

##### Share on other sites
crap my whole angle system is wrong ughh :'(
-durfy

##### Share on other sites
1) If you want to use trigonometric functions, you are going to want to use the same conventions for angles that mathematicians use: 0 degrees = 360 degrees = right, 90 degrees = up, 180 degrees = left, 270 degrees = down.

2) To reflect horizontally, change theta to 180 degrees - theta. For "downward" angles you get a negative number, but remember that adding or subtracting 360 degrees (a full circle) makes no difference to the direction. Similarly, to reflect vertically, change theta to 360 degrees - theta.

3) Don't give the ball 'setAngle' and 'getAngle' functionality. That's not how OO is supposed to work. Give it 'reflectVertically' and 'reflectHorizontally' instead. (Inside those, you manipulate theta directly.) Calling code isn't supposed to care whether the ball is remembering its direction with a theta-value. Then, if you change to use X and Y speed, you can modify those directly in reflectVertically and reflectHorizontally, and the calling code doesn't have to change at all.

4) Keep in mind that library functions like sin() etc. generally expect an angle in radians rather than degrees.

5)
Quote:
 (yes i need to learn more math :'( i'm in college again so i'll learn in a year or so all of it)

You really were supposed to learn it in high school :/

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631706
• Total Posts
3001833
×