A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Trig Math Brainfart...

  1. #1
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244

    Trig Math Brainfart...

    Okay I have a rotation and an offset from (0,0). What is the formula for finding the (x,y)?

    For instance a point is at (20,20), and if the world is rotated 45 degrees...where is the point at now?

  2. #2
    Style Through Simplicity alillm's Avatar
    Join Date
    Mar 2004
    Location
    Wales
    Posts
    1,988
    Well the distance to your original point is

    Code:
    var dist = Math.sqrt(20*20+20*20);
    and you know the rotation, 45 degrees which needs to be converted into radians.

    Code:
    var rot = 45;
    var rads = rot*(Math.PI/180);
    so now we can get the new position using basic trig.

    Code:
    var x = Math.cos(rads)*dist;
    var y = Math.sin(rads)*dist;
    Hope that helps.

    Ali

  3. #3
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244
    That would only work if the Y offset was at 0.

  4. #4
    Style Through Simplicity alillm's Avatar
    Join Date
    Mar 2004
    Location
    Wales
    Posts
    1,988
    Not to contradict you but I'm fairly sure it works.

    Ali

  5. #5
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244
    n/m I figured it out...this works
    Code:
    box.rotation += 5;
    var cos:Number = Math.cos( box.rotation * (Math.PI / 180));
    var sin:Number = Math.sin( box.rotation * (Math.PI / 180));
    var x1:Number = cos * offset - sin * offset;
    var y1:Number = cos * offset + sin * offset;
    box.x = x1 + boxX;
    box.y = y1 + boxY;

  6. #6
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    alillim's solution works, except you need to do:

    var orig:Number = Math.atan2( 20, 20);

    And then after you get his rads, do:

    rads = rads + orig;

    And then cosine/sine it to get the coordinates.


    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  7. #7
    Style Through Simplicity alillm's Avatar
    Join Date
    Mar 2004
    Location
    Wales
    Posts
    1,988
    That's assuming the position of the point is the origin rotation though, I was assuming that was just a point and we were rotating the whole "stage" 45 degrees. But yea, if that's what was meant then you just add on the original rotation.

    Ali

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