Showing posts with label rotation. Show all posts
Showing posts with label rotation. Show all posts

Friday, April 20, 2007

Trigonometric Baby Steps

I've taken my first steps at learning trigonometry this week. I've been working on a game that uses pseudo-trajectory and mathematical collision detection. I thought it a good chance for me to dunk my head into the quicksand that is trig.

My most used function? Math.atan2. I can't recommend it enough for finding angles. The basics of it goes like this:

ObjectA has an X and a Y coordinate and ObjectB also has an X and a Y coordinate. Start with one object (I'll start with ObjectA) and subtract each axis from each other.

var nRun:Number = ObjectA._x - ObjectB._x;
var nRise:Number = ObjectA._y - ObjectB._y;

Math.atan2 takes these two numbers as arguments like so:

var nRadians:Number = Math.atan2(nRise,nRun);

There you have it. You now know, in radians, what angle ObjectA points at ObjectB. If you want to see it in degrees so you can rotate your movie clip:

var nDegrees:Number = nRadians / Math.PI * 180;