Topic:   Trigonometry   (Read 6892 times)


0 Members and 1 Guest are viewing this topic.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Trigonometry
« on: September 24, 2009, 12:49:11 PM »
Hey guys, I'm finally learning Trig and I gotta say that it's so much fun.
There are so many neat things you can do in your games with just a few bits of knowledge of sin/cos/tan...

Do any of you guys understand anything about Trigonometry or know some neat things you can do with it?


-Gandolf

Silverwind


  • ^ This guy is amazing.

  • ****


  • Posts: 2805

  • For the glory of my maker
Re: Trigonometry
« Reply #1 on: September 24, 2009, 01:57:40 PM »
Nope. I've never learned anything about trigonometry, so speak on oh mathemagician! Anything awesome I can do with sprites?
I survived the spammage of 2007

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Trigonometry
« Reply #2 on: September 26, 2009, 06:25:33 PM »
Quote
speak on oh mathemagician!
Hardly, though I can give some tasty bits of knowledge.

Trigonometry is so elegant. Take for instance SOHCAHTOA - Pronounced Sow-Caw-Toe-Ah.
SOHCAHTOA means Sin(Theta) = Opposite/Hypotenuse, Cos(Theta)= Adjacent/Hypontenuse, Tan(Theta) = Opposite/Adjacent.

Now don't get freaked out yet, save that for the next part. Here are a few explanations:
Theta - Theta means the rotation/angle.
Sin - Sin is short for Sine, not the number of evil things you have done.
Cos - Cos is short for Cosine.
Tan - Tan is short for Tangent.
Opposite - This means the opposite leg from Theta(The angle/rotation)
Adjacent - This means the neighbor leg from Theta(The angle/rotation) that isn't the hypotenuse.
Hypotenuse - Line that goes from leg 1 to leg 2, it is always the largest leg.

Now just for the record, I am specifically only talking about right triangles. I don't have enough knowledge for any other.

Alright, so now you know the definitions. Now what? I'll show you, in song.(Kidding instead I will use a picture)


So now you may understand a little more, though how do you actual use these neat tools for your projects?
Easily, for example what if you only have the angle(which is 34 degrees) and the adjacent leg(which is 5 meters) of the triangle?
Just look through your 3 equations, pick the right one and plug 'er in. Like so:


You can also reverse engineer it if you have the adjacent and opposite but no angle:
(1) Tan(Theta/Angle) = 3.3725 / 5
(2) Theta/Angle = Tan-1(3.3725 / 5)
(3) Theta/Angle = Tan-1(.6745)
(4) Theta/Angle = 34 Degrees
So what's Tan-1? Tan-1 is the ArcTangent, it is basically the opposite and undoes what Tangent does. Like an evil clone.
You can find the Tan-1(.6745) in your handy dandy Calculator application by typing .6745, holding the shift key, and clicking tan-1.

Voila! Now you know all this neat info on finding angles and legs with these unique little equations though... how in the world do you apply this to an application? Or how about, a game? With sprites?
Easily! Glad you asked, I'll show you how.

Let's say that you are a Ranger going through the woods at night. You have this neat ability called "Tracking". It's where somehow you just know that a monster is near by.
Suddenly! Your senses go wild and you detect a monster close by, you pull your sword from your sheath and look around. Though... the programmer of the game has no trigonometry knowledge, you have no idea in which direction the monster is. Suddenly you hear a growl, something jumps on your back and everything goes black...
Well, that bites. I guess you wouldn't be dead if the cruddy programmer had just decided to learn a little about angles and such. Then you would be able to know at what angle the monster is.

So let's do just that. There's a ranger and a monster. Ranger's coordinates are 0,0 and the monster's coordinates are 25, 25. Let's save this ranger and show him what angle he should face.



Suddenly! Your senses go wild and you detect a monster close by, you pull your sword from your sheath and look around. Your nose detects a putrid smell, you notice tracks on the ground, (Immediately the game finds the angle in which you should face), and you turn toward the oncoming monster right as it jumps to attack...

So Silver, if Al's GameMaker has a rotate sprite function, I can help you with some code to allow a sprite to rotate towards something. It's very useful in puzzle, rpg, shooting(very useful), and arcade games.
If you want, I can help conjure up some code to do whatever your need may be that involves with angles and trigonometry.

Hope I didn't bore you guys to death, and it would be very awesome if some of you could apply this to your games.


-Gandolf

Gnome


  • GMG Extraordinaire

  • ***


  • Posts: 1073
Re: Trigonometry
« Reply #3 on: September 26, 2009, 07:02:54 PM »
Quote
Hey guys, I'm finally learning Trig and I gotta say that it's so much fun.



[size=24]DOES NOT COMPUTE[/size]
This Cannot be, NOOOOOOOO!!!!

-Gnomes Cry when the McRib was discontinued again.

WarHampster


  • GMG Extraordinaire

  • ***


  • Posts: 1501

  • The People's Moderator
    • Arcade of the Absurd
Re: Trigonometry
« Reply #4 on: September 26, 2009, 08:48:41 PM »
Wow Gan, how long did it take to write all of that?

My teacher used the SOHCAHTOA abbreviation as well :)

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Trigonometry
« Reply #5 on: September 26, 2009, 09:05:24 PM »
Quote
Wow Gan, how long did it take to write all of that?
Only took around 2 hours, I was having so much fun that it only felt like 15 minutes though.

Quote
My teacher used the SOHCAHTOA abbreviation as well :)
Yeah, great thanks to whoever made it up. It's quite an ingenious way of remembering something so complex in such a simplistic way.

Later I'll post another add-on to the tut that will focus on sin/cos functions with a different use for trig than what I showed here. You won't need to rotate a sprite so I think it'll be more useful than the first ranger example as I don't believe Gm has sprite rotation capabilities.


-Gandolf
« Last Edit: September 26, 2009, 10:33:23 PM by Gandolf »

Silverwind


  • ^ This guy is amazing.

  • ****


  • Posts: 2805

  • For the glory of my maker
Re: Trigonometry
« Reply #6 on: September 27, 2009, 03:29:01 AM »
Nice! Looks useful alright. I don't understand how you're getting 45 as the angle's value from "25, 25" but I'll appreciate that it works, hehe. :)
I survived the spammage of 2007

Al Staffieri


  • GMG-er

  • **

  • no avatar

  • Posts: 452

  • I love GameMaker
Re: Trigonometry
« Reply #7 on: September 27, 2009, 08:25:05 AM »

The square of the hypotenuse of a right triangle is equal to the sum of the squares of the two other sides.

That's about as much as I know... but it's better than the scarecrow in the Wizard of Oz. he says it wrong.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Trigonometry
« Reply #8 on: September 27, 2009, 09:13:46 AM »
Quote
Nice! Looks useful alright. I don't understand how you're getting 45 as the angle's value from "25, 25" but I'll appreciate that it works, hehe. :)
Angle = Unknown
Opposite leg = 25
Adjacent leg = 25

Plug it into the equation:
Tan(Angle) = Opposite/Adjacent
(1) Tan(Angle) = 25/25
(2) Tan(Angle) = 1
(3) Angle = Tan-1(1)  //Flip the tangent sign over and make it an arctangent
(4) Angle = 45
Very easy, just plug it in and it spits back what you need.

Alright, new scenario. There is Earth and the Moon. You're making a program that shows the Moon rotating around the Earth in a perfect circle. How do you do this?

Let's state our variables:
MoonX = 0
MoonY = 50
MoonRotation = 90
MoonEarthRadius = 50
EarthX = 0
EarthY = 0

Now according to the Moon's rotation, we will change it's X/Y values to be on the right place on the circle.
In our timer method, we will add 1 to MoonRotation each time.
Now we have MoonRotation changing, how do we move the moon to the right places? Just like this:


Now you know exactly what to do, let's put it together and compile.

Here is the Moon and Earth rotation Trig game:
http://web.mac.com/avisaria/MoonEarthTrigExample.zip
Press any of the buttons, see what happens.


-Gandolf

Silverwind


  • ^ This guy is amazing.

  • ****


  • Posts: 2805

  • For the glory of my maker
Re: Trigonometry
« Reply #9 on: September 27, 2009, 09:54:28 AM »
Quote
The square of the hypotenuse of a right triangle is equal to the sum of the squares of the two other sides.

That's about as much as I know... but it's better than the scarecrow in the Wizard of Oz. he says it wrong.
lol! ;D Quote of the Month!
I survived the spammage of 2007

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Trigonometry
« Reply #10 on: September 27, 2009, 07:01:55 PM »
Hey guys, on a scale of 1 to 10; how complicated was my little tutorial on trigonometry? (10 = Most complicated, 1 = Least complicated)


-Gandolf

Silverwind


  • ^ This guy is amazing.

  • ****


  • Posts: 2805

  • For the glory of my maker
Re: Trigonometry
« Reply #11 on: September 28, 2009, 03:53:45 AM »
I don't think it's particularly complicated, and it's certainly written is a friendly manner, I'm just no good at keeping track of the numbers in the sum. If I had a desperate need of trig for a game I'd pick it up fast, hehe.
I survived the spammage of 2007

Redd


  • GMG-er

  • **


  • Posts: 118

  • Silent, but very, very deadly.
Re: Trigonometry
« Reply #12 on: September 28, 2009, 02:55:32 PM »
... I did all this when I was 13...

Gnome


  • GMG Extraordinaire

  • ***


  • Posts: 1073
Re: Trigonometry
« Reply #13 on: September 28, 2009, 04:25:22 PM »
me too, it still hurts...  :'(


nothing personal gan, geometry was the worst year of math i had.
This Cannot be, NOOOOOOOO!!!!

-Gnomes Cry when the McRib was discontinued again.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Trigonometry
« Reply #14 on: September 28, 2009, 06:26:58 PM »
Strange. I don't remember learning any of this in Geometry, it wasn't till 3 years later in Pre-Calc+Trig that I even found out what Trigonometry was.
The toughest part of Trig and the largest part that they don't teach you in school is how to apply it to programming/real life.
It took me a long time to figure out how to make any good use of it, though once you know how; you can make some amazing things.
Posted the tutorial because I'd love to see games made with some adv. trig.


-Gandolf
« Last Edit: September 28, 2009, 06:27:37 PM by Gandolf »