Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Can some one please break down SIN COS and TAN?  (Read 3098 times)

0 Members and 1 Guest are viewing this topic.

Mamoruanime

@Mamoruanime
Can some one please break down SIN COS and TAN?
« on: March 11, 2010, 05:29:03 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
I've never been the greatest at math. There's a lot of stuff that blows my mind. In particular is the practical application of Sine, Cosine, and Tangent. I'm in now way versed enough in it to make, for example, an object that will move in a circular motion, or even pretend to try to understand how or why it works.

If someone could please break it down, it'd be greatly appreciated :(
Logged
Re: Can some one please break down SIN COS and T...
« Reply #1 on: March 11, 2010, 06:05:32 am »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
They're just useful trigonometric functions that are particularly useful in three-dimensional programming. I haven't touched them all that much, to be honest. D:
Logged
Quote
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.

But if my friends and I gather, wear black robes, and say  "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
  • Development Blog

Mamoruanime

@Mamoruanime
Re: Can some one please break down SIN COS and T...
« Reply #2 on: March 11, 2010, 06:08:16 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
Yeah me either... Like; I know what I need them for, but I haven't the clue how to use them.

I succeeded in making a wave beam using a sinusoid, but that's about it...
Logged
Re: Can some one please break down SIN COS and T...
« Reply #3 on: March 11, 2010, 07:15:13 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 466
Refer to the diagram for Trig Ratios

So... as for their practical applications... If, in a game, you wanted to move in a specific direction with a specific speed (assuming that direction is on angle) you would have to move your character both with an x velocity and a y velocity. Both the x velocity and y velocity act as the two straight lines in the right angle triangle (opposite and adjacent) so if you knew that you wanted to move 3 units with an angle of 30 degrees you would have
H=3
θ=30

so sin 30° = O(the unknown)/3

so sin 30°(3) = O
and O will equal one of your two components required to move 3 units at 30 degrees (in this case 1.5 units)

do the same with cos to get the other component.

Not sure if this is clear at all, I'm not always so good at explaining things.
Logged
I made a thing! Please try the thing, it's what the kids are calling a "vidja game" for the iOS Splashy Bucket
  • Fictitious Spoon Productions

Mamoruanime

@Mamoruanime
Re: Can some one please break down SIN COS and T...
« Reply #4 on: March 11, 2010, 07:47:05 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
Refer to the diagram for Trig Ratios

So... as for their practical applications... If, in a game, you wanted to move in a specific direction with a specific speed (assuming that direction is on angle) you would have to move your character both with an x velocity and a y velocity. Both the x velocity and y velocity act as the two straight lines in the right angle triangle (opposite and adjacent) so if you knew that you wanted to move 3 units with an angle of 30 degrees you would have
H=3
θ=30

so sin 30° = O(the unknown)/3

so sin 30°(3) = O
and O will equal one of your two components required to move 3 units at 30 degrees (in this case 1.5 units)

do the same with cos to get the other component.

Not sure if this is clear at all, I'm not always so good at explaining things.

So then for things like circular movement I'd increment the θ to widen the angle, right?
Logged
Re: Can some one please break down SIN COS and T...
« Reply #5 on: March 11, 2010, 07:47:50 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 466
You could indeed do that
Logged
I made a thing! Please try the thing, it's what the kids are calling a "vidja game" for the iOS Splashy Bucket
  • Fictitious Spoon Productions
Re: Can some one please break down SIN COS and T...
« Reply #6 on: March 11, 2010, 08:26:24 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Sin, cos, tan can be used for various things, like creating a wave or using it for calculating a rotation or just circular coordinates. Depending on what you want I can probably give an explanation or help you find the right answers.

For calculating a rotation by adding an angle, without saving the complete angle I suggest you take a look at the magical boomerang script I created for the CP (http://www.zfgc.com/forum/index.php?topic=35894.0) an explanation about the 2D rotation I used can be found http://en.wikipedia.org/wiki/Rotation_matrix. You might say: "Hey that is about matrices!" And your right but sin and cos are used in rotational matrices.

The formula for 2D rotation is:
x' = x cos θ - y sin θ
y' = x sin θ + y cos θ

where <x,y> is the current position, θ is the added angle and <x',y'> is the new calculated position. The rotation is done around <0,0> if you want to do it around a different point then you need to translate first and then back again.
« Last Edit: March 11, 2010, 08:28:38 am by Niek »
Logged
Re: Can some one please break down SIN COS and T...
« Reply #7 on: March 11, 2010, 08:45:17 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
I made a little example of how to use the sine function for circular motion, if you want to take a look.

Wait sorry, fogot I blocked access... here's a working link:
http://www.box.net/shared/zck1livh3t
« Last Edit: March 11, 2010, 08:50:13 am by Darklight »
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.

Mamoruanime

@Mamoruanime
Re: Can some one please break down SIN COS and T...
« Reply #8 on: March 11, 2010, 08:47:55 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
http://frostfireaudio.com/junk/sine%20example.gmk
I made a little example of how to use the sine function for circular motion, if you want to take a look.

Downloading now :P

But if it uses the direction function at all that GM comes prepacked with, it doesn't really help (most examples I've seen done in GM use it, which essentially defeats the purpose of learning how to apply trig to x/y values for me) :( Either way; I'll look at it now :P

EDIT:

D: Passworded
Logged
Re: Can some one please break down SIN COS and T...
« Reply #9 on: March 11, 2010, 08:50:31 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
http://frostfireaudio.com/junk/sine%20example.gmk
I made a little example of how to use the sine function for circular motion, if you want to take a look.

Downloading now :P

But if it uses the direction function at all that GM comes prepacked with, it doesn't really help (most examples I've seen done in GM use it, which essentially defeats the purpose of learning how to apply trig to x/y values for me) :( Either way; I'll look at it now :P

EDIT:

D: Passworded
fixed, here:
http://www.box.net/shared/zck1livh3t
and no it doesn't, I've purposely used only coordinates and sin :P
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Can some one please break down SIN COS and T...
« Reply #10 on: March 11, 2010, 08:51:40 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
The boomerang doesn't use the direction, at least not for the rotation formula.
Logged

Mamoruanime

@Mamoruanime
Re: Can some one please break down SIN COS and T...
« Reply #11 on: March 11, 2010, 08:53:09 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
http://frostfireaudio.com/junk/sine%20example.gmk
I made a little example of how to use the sine function for circular motion, if you want to take a look.

Downloading now :P

But if it uses the direction function at all that GM comes prepacked with, it doesn't really help (most examples I've seen done in GM use it, which essentially defeats the purpose of learning how to apply trig to x/y values for me) :( Either way; I'll look at it now :P

EDIT:

D: Passworded
fixed, here:
http://www.box.net/shared/zck1livh3t
and no it doesn't, I've purposely used only coordinates and sin :P

XD one more bother--- could I talk you into saving it as a GM7 gmk instead of GM8? D:
Logged
Re: Can some one please break down SIN COS and T...
« Reply #12 on: March 11, 2010, 08:59:23 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
GM8 won't let me save them as 7 :/ Here, I'll just post the code that I used:
Create event of the object for motion, I just used a black box test object:
Code: [Select]
X = 0;
Y = 1;
Z = 0;
A = 64;
XX = 0;
YY = 1;
ZZ = (pi/2);
AA = 64;
/*
The function has the form x = O+A*sin(Y*X+Z)
where A is the amplitude of the wave
X is the time
Y is the number of oscillations per time interval,
(the interval frequency is pi,
therefore whenever X = n multiple of pi, n revolutions have passed)
Z is the phase, so pi/2 is halfway along the phase, pi/4 is a quarter, etc
O is the offset for the object, which is the point the object
will revolve around

reverse direction by setting Y and YY to -1
by setting YY to 2, you get an interesting bobbing effect
that could work well for flying enemies with a reduced AA, or a swinging effect when AA = 16
*/

Step event of same object:
Code: [Select]
X += 0.1;
XX += 0.1;
x = 320+A*sin(Y*X+Z);
y = 240+AA*sin(YY*XX+ZZ);

place an object in the default sized room and it should circle around the centre.
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Can some one please break down SIN COS and T...
« Reply #13 on: March 11, 2010, 12:10:32 pm »
  • 笑い男
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 2124
For example, I made a function to have text "waving" across the screen in my game.
Just worked out its vertical offset by "sin-ing" (lol for terminology) it with its current position (between start and end point) while it moves horizontally across the screen.
That way I get a number between -1 and 1 relative to its position. So as its horizontal position changes as it moves across the screen, so does the its vertical offset (worked out by using sin), in a wave like pattern, which is then multiplied by whatever (to make it more of a drastic movement if you want). Of course you then just + the offset you get to the text's vertical position and there you go.

One use of using the sine wave anyway.

Another one that always comes up is moving 360 degrees, top down view, such as in a top down 2D car game.
It's next x and y position (of the car or whatever) is worked out by multiplying its speed by some calculation between sin/cos (depending on if you're working out the next X position or Y position) and the car's direction.
In gm you'd probably just use.... hmm "lengthdir_x/y", was it? Which just does that calculation for you though.

Of course that sort of calculation is very useful when working with 3D too.
Logged

この世に悪があるとすれば、それは人の心だ
  • .hack//The World
Re: Can some one please break down SIN COS and T...
« Reply #14 on: March 11, 2010, 12:54:44 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
Analytic geometry in 3D can be quite complex if you keep thinking on sines and cosines. I prefer using rotation matrices for this. (Okay, I think GM doesn't support matrices, but usually 3D engines for C++ come with matrix classes)

So, if you want to rotate a vector (ex.: the speed of the player) in t degrees in the XY plane, you have to multiply it by a matrix

      [cos(t)  -sin(t)    0]
Q = [sin(t)   cos(t)    0]
      [0          0        1]

This rotation is recursive, so for instance - if you want to rotate it in 2t degrees you have to multiply it by Q*Q. If you want to rotate it on a specific plane, you do an orthogonal base change, multiply it by Q, and revert your base change, so your rotation matrix is B*Q*(B^-1). Anyway, there are many things you can do with matrices that are easier than dealing directly with sines and cosines, you just have to learn the proper methods.



But for 2D sines and cosines don't get so complicated so you don't need to recur to matrices. If you have a vector
     [ x]
v = [ y]
the length is r = sqrt(x^2 + y^2)
the angle is t = atan2(y,x)
and the sine, cosine and tangent of the angle are respectively x/r, y/r and y/x.
Also,
x = r*cost
y = r*sint

So if you have x and y you can get t and r, and if you have t and r you can get x and y. You just have to apply the formulae.
Logged
Re: Can some one please break down SIN COS and T...
« Reply #15 on: March 11, 2010, 01:25:15 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
GM8 won't let me save them as 7 :/ Here, I'll just post the code that I used:
Create event of the object for motion, I just used a black box test object:
Code: [Select]
X = 0;
Y = 1;
Z = 0;
A = 64;
XX = 0;
YY = 1;
ZZ = (pi/2);
AA = 64;
/*
The function has the form x = O+A*sin(Y*X+Z)
where A is the amplitude of the wave
X is the time
Y is the number of oscillations per time interval,
(the interval frequency is pi,
therefore whenever X = n multiple of pi, n revolutions have passed)
Z is the phase, so pi/2 is halfway along the phase, pi/4 is a quarter, etc
O is the offset for the object, which is the point the object
will revolve around

reverse direction by setting Y and YY to -1
by setting YY to 2, you get an interesting bobbing effect
that could work well for flying enemies with a reduced AA, or a swinging effect when AA = 16
*/

Step event of same object:
Code: [Select]
X += 0.1;
XX += 0.1;
x = 320+A*sin(Y*X+Z);
y = 240+AA*sin(YY*XX+ZZ);

place an object in the default sized room and it should circle around the centre.
Hmm, interesting that you use sin for both X and Y. Usually for circular movement one use cos for x and sin for y. Because the one is always a Pi/2 phase ahead of the other. You don't need the phase discriminators Z and ZZ. You could do with the following code to create a circular move:
Code: [Select]
X += 0.1;
x = 320+A*cos(X);
y = 240+AA*sin(X);

You could make Y*X and YY*X for bobbing effects and the A and AA is for the type of circle. A = AA makes a circle, while A <> AA makes an ellipse.
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.212 seconds with 66 queries.

anything