Click to See Complete Forum and Search --> : Calculating Polar Offsets


SpectreReturns
04-21-2005, 12:00 AM
Alright, here's the deal. I'm writing a flash game, and I want to have a polar offset system so that I can create units at like 270° 25 pixels away from a certain point.

In polar offsets, 0° is east, 90° is north, 180° is west and 270° is south.

Anyway, I'm not too great at trigonometry, so I'm coming here for help. Here's what I have so far:


// uses three arguments, the first is an object of origin, the second is the angle, and the third is the offset of the angle
function polarOffset(ob,angle,offset) {
x = Math.cos(angle)*offset; // x= cosine of angle times offset
y = Math.sin(angle)*offset; // y= sine of angle times offset
num = random(10000); // generate a random number with which to attach the tracer
_root.attachMovie('temp','temp'+num,100+num); // attach the tracer
_root['temp'+num]._x = ob._x+x; // set the xpos of the tracer to the origin xpos plus x
_root['temp'+num]._y = ob._y+y; // set the ypos of the tracer to the origin ypos plus y
}

// run the function 4 times, using a pure direction each time
polarOffset(_root.ob,0,25);
polarOffset(_root.ob,90,25);
polarOffset(_root.ob,180,25);
polarOffset(_root.ob,270,25);


Here's the problem however. Angle 0 works perfectly, 90 is about 1/3 of the way around the origin, 180 is 2/3, and 270 is a bit past 0.

Attached is a picture of what is rendered, as well as the angles of the tracers and the origin.

Please help me figure this out.

scragar
04-21-2005, 08:46 AM
your angles must be in radians.

angle = angle * (Math.PI / 180);


note this is what my test show anyway...

SpectreReturns
04-21-2005, 10:10 PM
Oh jeez. I love you. Thanks!

[EDIT] It works, but the Y position is backwards, so I multiplied it by -1, and it works. Thanks again.