Sign in to follow this  
Durfy

Pong math question.

Recommended Posts

Durfy    109
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 this post


Link to post
Share on other sites
awesty    124
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 this post


Link to post
Share on other sites
JohnBolton    1372
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 this post


Link to post
Share on other sites
Drigovas    509
Quote:
Original post by Durfy
SHOULD 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 this post


Link to post
Share on other sites
Durfy    109
Quote:
Original post by JohnBolton
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 


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 this post


Link to post
Share on other sites
realcore    134
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 this post


Link to post
Share on other sites
Durfy    109
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 this post


Link to post
Share on other sites
Durfy    109
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 this post


Link to post
Share on other sites
Zahlman    1682
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 :/

Share this post


Link to post
Share on other sites
DvDmanDT    1941
Note: While 0 is right and 180 is left, 90 can very well be down.

sin(90) will give you +1. Expressed in screen coordinates, +1 would mean one pixel down. In math, +1 normally means up however, and that's also very common in 3d graphics. This caused quite some confusion for me accutally.

Share this post


Link to post
Share on other sites
Durfy    109
For me tooo up is 270 right 0/360 down 90 left 180 :-)
... also about the shoulda learned in highschool... i'm sure I did but it's been so long out of practice/ never used that I have all but forgotten it completely! I am going to be taking a refresher course this semeseter though in college :-)
-durfy

Share this post


Link to post
Share on other sites
Durfy    109
SUCCESS ZAHLMAN THANK YOU SOO MUCH... And i just picked up a math term! Theta is the angle right? Or is it the change in angle rofl? Either way IT BOUNCES YIPPEEE!
A NOTE TO OTHER PEOPLE TRYING THIS IT DOES DEFINATELY MATTER HOW MANY DECIMAL PLACES YOU GO WITH PI WHEN CONVERTING DEGREES TO RADIANS... I WAS USING 3.14 GIVING ME HORRIBLY OFF CALCULATIONS USE WHAT IS IN MATH.H AS M_PI
-durfy

Share this post


Link to post
Share on other sites
Quote:
Original post by Durfy
SUCCESS ZAHLMAN THANK YOU SOO MUCH... And i just picked up a math term! Theta is the angle right? Or is it the change in angle rofl? Either way IT BOUNCES YIPPEEE!
A NOTE TO OTHER PEOPLE TRYING THIS IT DOES DEFINATELY MATTER HOW MANY DECIMAL PLACES YOU GO WITH PI WHEN CONVERTING DEGREES TO RADIANS... I WAS USING 3.14 GIVING ME HORRIBLY OFF CALCULATIONS USE WHAT IS IN MATH.H AS M_PI
-durfy


You're going to severely hit our patience if you continue to use CAPS. So turn off your caps lock.

It makes the text a pain to read, and is generally considered as SHOUTING on the web. And shouting is impolite. So don't do it.

And theta is not a math term, it's a Greek letter which is often used to represent an angle in math equations (physician tend to use ω, especially when the angle is a function of time ω(t)). It's variation is often called dθ (the small 'd' is here to tell you that it's a derivative). If you want to be more precise about the variable that produce the change, you use the complete formal derivative form:
   d&theta(x)
----
dx
If θ is a function of more than one variable, the notation change to the partial derivative form:
   δ&theta(x,y,z)
--------
δx
But that's really not the point here, since you don't have to handle angle variations for a Pong game - unless you're doing weird stuff, such as the simulation of forces like in Plasma Pong.

Share this post


Link to post
Share on other sites
rabidbob    122
Just wanted to post a quick thanks for the help in this thread; I had a similar question myself regarding the plotting of objects on a 2d matrix and this has got me moving in the right direction (so to speak).

Thanks!!

Share this post


Link to post
Share on other sites
Durfy    109
Sorry for "SHOUTING" i was shouting for joy lol! Thanks for the explanation though Emmanual even though my skills in understanding it are zero... hopefully after my refresher math course I will understand a little better... I dont plan on stopping there... I'm going through all the way to calculus 2 before i get my AA.
Thanks,
-durfy

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this