OK here is a quick (kinda) lesson on trig and the wonders of Triangles! Unplanned, so its coming before collisions
************
Trigonometry NOTE: put your calc in degrees mode!
************
Trig is one of those things that can be very usefull in games, but is actualy fairly
difficult to figure out without some good instruction. That is where this tutorial
is going to try to help.
trigonometry is the mathematics of trianlges. Take this triangle for instance
|\
| \
a | \ C (hypotenuse)
(leg) | \
| \
+-----
b (leg)
It is a right triangle because the angle between side A and side B is 90 degrees. This is
the only type of trignale we will discuss in this tutorial, and usualy it is the only one you
will need.
In addition to having 3 sides, there are also 3 angles. AB, AC, and BC. You can also call them
by the sides that are *opposite* to that angle, so angle AB would be angle C. We already
established that angle AB is 90 degrees. This triangle is a RIGHT triangle, because it has one
right (90 degree) angle, and Right Triangles are your friend
For starters, here is the pythagorean theorem:
A^2 + B^2 = C^2
Hmmm interesting, for all Right Trignales, the sum of each leg squared, equals the hypotenuse squared.
(remember, the legs are the two sides that make the right angle) What this means for us is that
for any right triangle, if we are given 2 sides, we can find the other with this equation. This
is a way to calculate the distance to a point, as you can use the 2 legs as the X and Y distances
to a point.
B
/|
/ | The distance from A to B is the hypotenuse of a right triangle, with X and Y
/ | as the legs.
/ | Y
/ | Distance^2 = X^2 + Y^2 Take the square root, and we have:
/ |
A------+ Distance = SQRT(X^2 + Y^2)
X
Now, we have worked with the sides, what about the angles? The angles between two sides is the ammount
of rotation between them:
|
| \ /
| \ /
| \ /
| 90 degrees \ 135 degrees / 45 degrees
| \ /
+--------- -------- ---------
Somtimes we want to rotate a sprite or an object around a point. Like this:
ZStandard
ZInteger
AxesOff
ClrDraw
For(F,0,360,5
Pt-On(29cos(F),29sin(F
End
What??? How did i just do that? Well the answer lies in trig, and in right triangles. You may not think
that triangles have much to do with circles, but in fact, they have everything to do with eachother.
A circle contains all the points a certain radius from a center point. So given a radius, there are
different X,Y points that lie onto a circle. Does this look farmiliar? Look again at the distance formula:
Distance = SQRT(X^2 + Y^2)
If we say that Distance is the radius, this distance equation becomes the equation of a circle! :O
It gives us an euqation, where if we have a radius (distance) and an X or Y value, we can find the
other coordinate.
Try solving the distance equation for Y
Distance = SQRT(X^2 + Y^2)
Distance^2 = X^2 + Y^2
Distance^2 - X^2 = Y^2
SQRT(Distance^2 - X^2) = Y
Y = SQRT(Distance^2 - X^2)
Try putting this into Y1 in your calculator, set Distance to 5 lets say, and graph it! You get a circle!
(or half of one anyway... stupid functions...)
Wow, thats really cool, we can get a circle without doing any trig at all!
Now comes the final challenge, using the above equation, we can find all of the points on a circle, given
the radius, but what if we want a singe point? We already have the radius, but we do not have any other
variables
to determine which point to use. We need another, and that is the angle. A circle goes accross 360 degrees
or angle, starting at the rightmost point at 0 degrees and rotating accross the circle counterclockwise until
it gets to 360 degrees, where it started.
Now it is time to introduce Sin, Cos, and Tan. These functions pertain to triangles and their angles, and are
all very usefull. For right triangles, you only take the sin, cos, or tan of angles that are NOT the Right
Angle
So for this triable they would be AC and BC
|\ Sin of an angle equals the opposite leg over the hypotenuse
| \ Sin(BC) = A/C Sin(AC) = B/C
a | \ C (hypotenuse) Cos of an angle equals the adjecent leg over the hypotenuse
(leg) | \ Cos(BC) = B/C Sin(AC) = A/C
| \ Tan of an angle equals the opposite over the adjecent
+----- Tan(BC) = A/B Tan(AC) = B/A
b (leg)
These trigonometric equations give us some interesting power. If we know Hypotenuse C and Angle BC,
we can find leg A.
Sin(BC) = A/C
Sin(BC)*C = A
A = Sin(BC)*C
And we can also find leg B as well
Cos(BC) = B/C
Cos(BC)*C = B
B = Cos(BC)*C
Do you know what this meants? Given a distance, and an angle, we can find the coordinates of that point!
B
/|
/ |
/ |
/ | Y
/ |
/ |
A------+
X
Given the angle at A, and the distance AB, we can calculate the distances X and Y, which are the coordinates of
Point B.
X = R*Cos(A)
Y = R*Sin(B)
its that simple! And for all angles, point B will be a distance of R form the center, which is the
*exact* definition of a circle! Given an angle and a radius, using these two equations we can find a
specific point on a circle.
This program shows how the triangles fit into the circle as it travels around the circle:
ZStandard
ZInteger
AxesOff
ClrDraw
Degree
0->F
Circle(0,0,30
While 1
Line(0,0,A,B,0 //hypotenuse
Line(0,0,A,0,0 //right leg
Line(A,0,A,B,0 //left leg
29Cos(F)->A //find the coordinates of the next point on the circle
29Sin(F)->B
Line(0,0,A,B
Line(0,0,A,0
Line(A,0,A,B
F+5->F
End