PDA

Click to See Complete Forum and Search --> : help with trignometry in flash



harnoors
03-19-2002, 03:48 PM
hi thanks for reading my query
i am making a mousechaser in flash in the shape of a rocket and i require the pointe fact to turn towards the cursor i tried the following code for rotation, but it doesn't work too good plz, help!!

r=(Math.atan((ty)/(tx))/ra)*180/pi;
_rotation+=r

see if u could help me or provide a link to a suitable tutorial online

thanks
harnoor

ironmallet
03-19-2002, 03:56 PM
onClipEvent(mouseMove){
this._rotation =
57*(Math.atan2((_parent._ymouse-this._y),(_parent._xmouse-this._x)));}

http://www.ironmallet.com/flashkit/arrow.swfarrow.swf

getting it to fly in this direction isn't so bad either, reply to this post if you want a leg up on that.

harnoors
03-20-2002, 02:58 AM
thanks a lot for the code
could you explain why you used _parent instead of root.it works better than my code .yeah it would be really great of you if u could help me further in this regard.I tried using your rotaion code with my mousechaser code but does'nt look too good as the rotaion seems too jumpy and not smooth.

thanks
harnoor


Originally posted by ironmallet
onClipEvent(mouseMove){
this._rotation =
57*(Math.atan2((_parent._ymouse-this._y),(_parent._xmouse-this._x)));}

http://www.ironmallet.com/flashkit/arrow.swfarrow.swf

getting it to fly in this direction isn't so bad either, reply to this post if you want a leg up on that.

ironmallet
03-20-2002, 04:07 AM
_parent is usually preferable to _root because if you get an mc to work using only _parent, it's totally portable, you can stick it in anything. When it comes to the _xmouse and _ymouse, however, you are almost always talking about the stage proper, so _root might be better. _root is kindof lazy though, it's like using an absolute url for every getURL.

as far as flying toward the mouse position goes, treat it as a separate matter from the facing of the arrow (or in your case rocket)

Method 1
stick this in after the trig stuff.
-----

this._x+=.4*(_parent._xmouse-this._x);
this._y+=.4*(_parent._ymouse-this._y);

-----
easy and effective.


Method 2
for a gravitational effect.
-----

// make a shorthand
x=this._x;
y=this._y;
r=_parent._xmouse;
t=_parent._ymouse;

// which way is the mouse
theta=
Math.atan2((_parent._ymouse-this._y),(_parent._xmouse-this._x));

// how far away is the mouse
distsqrd=(x-r)*(x-r) + (y-t)*(y-t);

// describe the incremental acceleration as x and y comp
ddx=(5000/distsqrd)*Math.cos(theta);
ddy=(5000/distsqrd)*Math.sin(theta);

//this just catches the arrow at close range

if (distsqrd>200) {
dx += ddx;
dy +=ddy;}
else
{dx-=.7*dx; dy-=.7*dy;}

//this actually moves the arrow

this._x = this._x + dx;
this._y = this._y + dy;

-----

harnoors
03-20-2002, 07:50 AM
thanks for your speedy reply i will try that code and let u know if i get any problems or questions
thanks again
harnoor

ironmallet
03-22-2002, 03:08 AM
Did that work for you?

harnoors
03-27-2002, 01:13 PM
nope did'nt get the time to try it will tell ya when i try
thanks for the concern

harnoor