ZFGC

Projects => Discussion => Topic started by: Mamoruanime on March 11, 2010, 05:29:03 am

Title: Can some one please break down SIN COS and TAN?
Post by: Mamoruanime on March 11, 2010, 05:29:03 am
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 :(
Title: Re: Can some one please break down SIN COS and TAN?
Post by: Cassyblanca on March 11, 2010, 06:05:32 am
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:
Title: Re: Can some one please break down SIN COS and TAN?
Post by: Mamoruanime on March 11, 2010, 06:08:16 am
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...
Title: Re: Can some one please break down SIN COS and TAN?
Post by: FictitiousSpoon on March 11, 2010, 07:15:13 am
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.
Title: Re: Can some one please break down SIN COS and TAN?
Post by: Mamoruanime on March 11, 2010, 07:47:05 am
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?
Title: Re: Can some one please break down SIN COS and TAN?
Post by: FictitiousSpoon on March 11, 2010, 07:47:50 am
You could indeed do that
Title: Re: Can some one please break down SIN COS and TAN?
Post by: Zaeranos on March 11, 2010, 08:26:24 am
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.
Title: Re: Can some one please break down SIN COS and TAN?
Post by: Wasabi on March 11, 2010, 08:45:17 am
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
Title: Re: Can some one please break down SIN COS and TAN?
Post by: Mamoruanime on March 11, 2010, 08:47:55 am
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
Title: Re: Can some one please break down SIN COS and TAN?
Post by: Wasabi on March 11, 2010, 08:50:31 am
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
Title: Re: Can some one please break down SIN COS and TAN?
Post by: Zaeranos on March 11, 2010, 08:51:40 am
The boomerang doesn't use the direction, at least not for the rotation formula.
Title: Re: Can some one please break down SIN COS and TAN?
Post by: Mamoruanime on March 11, 2010, 08:53:09 am
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:
Title: Re: Can some one please break down SIN COS and TAN?
Post by: Wasabi on March 11, 2010, 08:59:23 am
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.
Title: Re: Can some one please break down SIN COS and TAN?
Post by: hawthorneluke on March 11, 2010, 12:10:32 pm
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.
Title: Re: Can some one please break down SIN COS and TAN?
Post by: sjegtp on March 11, 2010, 12:54:44 pm
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.
Title: Re: Can some one please break down SIN COS and TAN?
Post by: Zaeranos on March 11, 2010, 01:25:15 pm
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.

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