PDA

View Full Version : trigonometry: finding the angle



zylum
May 7th, 2003, 06:32 PM
basically i have two movie clips. i know the x diff and the y diff, now how do i find the angle at which the movie clips are at?


v MC 2
/|
/ |
/ | < y diff
/ |
MC 1 > /__|
^ \
angle x diff


i hope that's clear enough

-mike

zylum
May 7th, 2003, 06:33 PM
ooops, the diagram got screwed up when i posted....

Jubba
May 7th, 2003, 06:34 PM
eh, i sort of fixed it for you...

zylum
May 7th, 2003, 06:36 PM
thanks jubba :)

davidchang
May 7th, 2003, 07:15 PM
rad_deg = Math.PI / 180;
angle = Math.atan2 (y_diff, x_diff);
angle /= rad_deg; //to convert to degrees

note: hope u know about CAST rule, cuz u need

maybe try reading about Math.atan also....

zylum
May 7th, 2003, 07:24 PM
i tried that and it didn't work. i probably didn't do it right. here's my script:

diffx = MC1._x - MC2._x
diffy = MC1._y - MC2._y
ang = Math.atan2(diffx, diffy);
this.dx = Math.cos(this.ang)*this.speed;
this.dy = Math.sin(this.ang)*this.speed;
this._x += this.dx;
this._y += this.dy;
this._rotation = this.ang*(180/Math.PI);

i want MC1 to move towards MC2. maybe my approach is totaly wrong...:-\

zylum
May 7th, 2003, 07:26 PM
oh, yeah, the above script is in an onEnterFrame function and all that it does is make MC1 move to the right horizontaly

davidchang
May 7th, 2003, 07:26 PM
ummm, do u know about the CAST rule?

zylum
May 7th, 2003, 07:28 PM
nope, is it important? if it is can you please give a brief explanation? thatnks

davidchang
May 7th, 2003, 07:33 PM
S | A
__|___
T | C

C: cosine
A: all
S: sine
T: tangent
these are the quadrant that are positive

zylum
May 7th, 2003, 07:41 PM
ahh... nevermind, simple scripting error, your way works. thanks a lot :)