A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Help with getting proper rotation value

  1. #1
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194

    Help with getting proper rotation value

    Hi,

    I'm using this code to give me a rotation of a line.

    Code:
    var dy:Number = bounds.yMax-bounds.yMin;
    
    var dx:Number = bounds.xMax-bounds.xMin;
    
    var angle:Number = Math.atan2(dy, dx);
    
    rot = angle*180/Math.PI;
    Problem is it gives me the same rotation result regardless of how the line is orientated, i.e if the line is sloping from top right to bottom left gives the same result if its sloping from top left to bottom right. It always seems to give me positive results. How do I change it so it knows which way the line is facing and gives me the correct result in rot?
    FlashGameMaker.com - er..where I say stuff.

  2. #2
    Elvis...who tha f**k is Elvis? phreax's Avatar
    Join Date
    Feb 2001
    Posts
    1,836
    Well, from what you are showing, all I can tell is that angle and rot works. My guess is you derive your bounds from a static bounding rectangle...not really sure why you use the bounding rectangle at all!?
    Streets Of Poker - Heads-Up Texas Hold'em Poker Game

  3. #3
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194
    Thats exactly right, I'm drawing a line into that rect, but I need to get the rotation of the line for my line/ball code to work correctly. What I've found is that the code gives me the same result whether the line is sloping down to the left or down to the right. This is different though if I just draw a line into a MC, and then rotate the mc, that gives the correct result (i.e it gives a positive result for one orientation, but a neg result for the other way), but drawing the line dynamically and then trying to work out the rot of the line doesn't seem to work.
    FlashGameMaker.com - er..where I say stuff.

  4. #4
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194
    to give a quick example, if I draw a line such as x1=100,y1=300 to x2=200,320....and then use the above code to get the rotation, that result is the same as if the line I draw is x1=100m,y1=300 to x2=200, y2=280........even though the orientation of the lines is different.
    FlashGameMaker.com - er..where I say stuff.

  5. #5
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    Sounds like you need to conceptually decouple the bounding box of a line from its orientation.

    Either create state variables for both...

    class Line {
    var x0, y0 : Number; // Start
    var x1, y1 : Number; // End
    var xmin, ymin : Number; // Upper left
    var xmax, ymax : Number; // Lower right.
    }

    ... or store only the orientation and compute bounding box information on demand:

    class Line {
    var x0, y0 : Number
    var x1, y1 : Number
    function xmin() : Number { return Math.min(x0,x1) }
    function ymin() : Number { return Math.min(y0,y1) }
    function xmax ...
    function ymax ...
    }

  6. #6
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194
    um..thanks, but I'm allowing the user to draw the line, from x1,y1 to x2, y2...and they draw that line from left to right, or right to left.....what I don't understand is I'm sure it's possible to compute the correct angle from the start and end points, but im not sure how!

    I'm storing the points with a Line class as you suggested.
    FlashGameMaker.com - er..where I say stuff.

  7. #7
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    a) you are drawing a line inside an mc right?
    b) if so then place the mc at the beginning of the line
    c) then just use the x,y position of the ending of the line in your calculations

    the problem is you're trying to get the orientation from the bounding box which doesn't take into consideration the direction of the slope
    Last edited by BlinkOk; 04-03-2008 at 06:39 PM.
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  8. #8
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    Use the start/end oriented points inside your angle computation instead of the bounding box points.

    var dy:Number = line.y1-line.y0;
    var dx:Number = line.x1-line.x0;
    var angle:Number = Math.atan2(dy, dx);

  9. #9
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194
    Quote Originally Posted by rachil0
    Use the start/end oriented points inside your angle computation instead of the bounding box points.

    var dy:Number = line.y1-line.y0;
    var dx:Number = line.x1-line.x0;
    var angle:Number = Math.atan2(dy, dx);
    Thanks everyone for the help so far.

    Thats exactly what I'm trying but because the start and end points can be set by the user, i'm finding that the above code works when the start point is set a certain way, so lets say when you draw a line from 142x258 to 365x75 it works, but when you draw basically the same line but from the other direction so, from 371x115 to 175x268 it doesn't. I'm thinking I'm going to have to check for 4 possibilities and then swap the x and y's around to make it work?
    FlashGameMaker.com - er..where I say stuff.

  10. #10
    Junior Senior
    Join Date
    Nov 2000
    Location
    sydney
    Posts
    565
    You shouldn't need to check for the 4 possible orientations at all, just forget about the bounds object completely and get the angle from the start point to the end point, instead of getting the angle from the top left to the bottom right as you're doing.

    If you're still having trouble maybe post the line drawing code you're using so that people can see how you've positioned the line MC and how you've named the start and end points.
    Signature

  11. #11
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    DON'T USE GETBOUNDS

    when i calculate the angle of 142x258 to 365x75 i get -39
    when i calculate the angle of 371x115 to 175x268 i get 142

    what's the problem??
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  12. #12
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194
    Quote Originally Posted by BlinkOk
    DON'T USE GETBOUNDS

    when i calculate the angle of 142x258 to 365x75 i get -39
    when i calculate the angle of 371x115 to 175x268 i get 142

    what's the problem??
    haha, I'm not using getbounds anymore! btw, I've got it working now. Thanks!
    FlashGameMaker.com - er..where I say stuff.

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