Trigonometry for absolute newbies
Author: | Muz
|
Submitted: | 22nd February, 2009
|
Views: | 5431
| Rated: |
|
|
There are much more advanced articles on this. But I'm sure those of you who are fresh out of school, or didn't pay much attention in class are all confused about it. So, here's a very simple, very quick guide to trigonometry. It might not help much with your game, but it's a start.
Radians and degrees
I'm sure most of you know degrees. If 0 degrees is right, then 90 degrees is up, 180 is left, 270 is down.
Radians are easier to work with. Everything revolves around pi( π )
0π = 0 degrees
1π = 180 degrees
2π = 360 degrees
This means that up is (1/2)*π and down is (3/2)*π. Everything in between is even easier, all you have to do is use a fraction. You want that angle exactly between up and left? That's (1/4)π. If you want it leaning more to the left, you use something lower than (1/4)π and more than 0.
Radians are much easier to work with than degrees once you get used to it. It's like Fahrenheit and Celsius - Celsius actually makes sense. Don't try to convert degrees to radians, and then convert it back to degrees.. it's a lot more confusing. Just learn to use radians.
But as far as I recall, MMF2 uses degrees for angles, so I'm going to stick with that for the rest of this articles.
Sines, Cosines
Sines and cosines are the easiest way to handle angles.
Let's say that you have an bullet that moving at 70 degrees, at the speed of 10 pixels per second? How many pixels up and how many pixels left does it move?
It's an easy one, but I want you to figure this out yourself.. it'll help you more in making your game and when this stuff pops up in class.
Simply put, sines work vertically and cosines work horizontally. Note that I'm using degrees here:
sin(0) = 0 cos(0) = 1
sin(90)= 1 cos(90) = 0
sin(180) = 0 cos(180) = -1
sin(270) = -1 cos(270) = 0
Conclusion...
X = cos(angle)
Y = sin(angle)
Now let's go back to that question? How do we check the speed of that bullet in terms of X and Y? Think about it a moment, then scroll down....
It's easy!
X = Speed*cos(angle)
= 10*cos(70)
= 3.42 pixels/second
Y = Speed*sin(angle)
= 10*sin(70)
= 9.40 pixels/second
Ah, but there's a catch there.
sin(90)= 1 ; Not -1.
With graphs, Up is a higher number. With computers and monitors, Down is a higher number. So, it's inverted there, see.
To convert it to your computer, just change the formula to..
Y = -Speed*sin(angle)
= -9.40 pixels/second
You get an easy, simple, 360 degree bullet code right there
Have fun! If you want further information on trigonometry, there are plenty of other articles on TDC and on the rest of the Internet.
|
|
|