It's not so much geometry as trigonometry that would benefit you here... Trigonometry is extremely useful for programming video games (and/or guided missiles), as it teaches you to move in particular directions, to target objects and to measure distance.
Geometry (Euclidean geometry) is more concerned with logic & proof.
Let's look at the code:
code:
onClipEvent (enterFrame) {
//DECLARATIONS
degrees = this._rotation;
radians = (degrees*(Math.PI/180));
//ROTATION
if (Key.isDown(Key.LEFT)) {
_rotation -= 4;
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 4;
}
//INCREASE SPEED
if (Key.isDown(Key.UP)) {
speed += .2;
}
if (Key.isDown(Key.DOWN)) {
speed -= .2;
}
//MAX SPEED
if (Math.abs(speed)>50) {
speed *= .94;
}
// FRICTION
speed *= .97;
//CALCULATES VECTORS
speedx = Math.sin(radians)*speed;
speedy = Math.cos(radians)*speed*-1;
//CHANGES POSITION
_x += speedx;
_y += speedy;
}
Okay, basically what this script does is get the car to travel in the direction that the movie is pointing.
Forgetting the right triangle for a moment, it's helpful to know that you can draw a circle like this:
code:
// center of circle
cx = 100;
cy = 100;
// radius (width from center to rim) of circle
rad = 50;
moveTo(cx+rad,cy);
for (a = 0; a < 2*Math.PI; a += .1)
{
x = cx + Math.sin(a)*rad;
y = cy - Math.cos(a)*rad;
lineTo(x,y);
}
So sin and cos basically describe a circle around a center point. Why do they do this? Read a book on Trig. For now, let's treat them as magic 'circle-making' functions.
The code converts the current rotation of the movie (which goes from 0 to 360) into a value which goes from 0 to 2*PI
(which is what sin and cosine want).
code:
degrees = this._rotation;
radians = (degrees*(Math.PI/180));
So we are converting from degrees (which go from 0-360) to radians (which go from 0 to 2pi).
By the way, it's interesting to know that if the radius of a circle is 1, it's circumfrence happens to be 2*PI. So radians are a kind of 'natural' unit for functions that draw circles, like sin and cosine.
The rotation is modified if the key is held down.
code:
//ROTATION
if (Key.isDown(Key.LEFT)) {
_rotation -= 4;
}
etc.
The speed is modified as well. I'll skip that bit and move on to the part with sin/cos.
We treat the car as if it is the center of a circle. Note that 0,0 is the center of the car.
We find the point on the circle directly in front of the car (based on it's angle of rotation in radians).
code:
speedx = Math.sin(radians)*speed;
speedy = Math.cos(radians)*speed*-1;
Note that this is equivalent to my circle drawing code, in which cx,cy are 0,0.
code:
x = 0 + Math.sin(a)*rad;
y = 0 - Math.cos(a)*rad;
Then we go in that direction.
code:
_x += speedx;
_y += speedy;
If you manage to get a little more comfortable with sin and cos (and playing with drawing circles is a good way to start), then I suggest you visit my website (link below). The section 'Circles and Kaleidoscopes' contains a number of movies which exploit these functions to make pretty pictures.