dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: help with trignometry in flash

  1. #1
    Senior Member
    Join Date
    Jan 2001
    Posts
    101

    Exclamation

    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

  2. #2
    Senior Member ironmallet's Avatar
    Join Date
    Feb 2001
    Posts
    252
    onClipEvent(mouseMove){
    this._rotation =
    57*(Math.atan2((_parent._ymouse-this._y),(_parent._xmouse-this._x)));}

    [swf width="200" height="200" background="#eeeeee"]http://www.ironmallet.com/flashkit/arrow.swf[/swf]arrow.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.



  3. #3
    Senior Member
    Join Date
    Jan 2001
    Posts
    101
    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)));}

    [swf width="200" height="200" background="#eeeeee"]http://www.ironmallet.com/flashkit/arrow.swf[/swf]arrow.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.



  4. #4
    Senior Member ironmallet's Avatar
    Join Date
    Feb 2001
    Posts
    252
    _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;

    -----

  5. #5
    Senior Member
    Join Date
    Jan 2001
    Posts
    101
    thanks for your speedy reply i will try that code and let u know if i get any problems or questions
    thanks again
    harnoor

  6. #6
    Senior Member ironmallet's Avatar
    Join Date
    Feb 2001
    Posts
    252
    Did that work for you?


  7. #7
    Senior Member
    Join Date
    Jan 2001
    Posts
    101
    nope did'nt get the time to try it will tell ya when i try
    thanks for the concern

    harnoor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center