A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Bounding box of a rotated ellipse ??

  1. #1
    Junior Member
    Join Date
    Jul 2005
    Posts
    3

    Bounding box of a rotated ellipse ??

    Hi ..

    Actionscripts getBounds() methods returns the bounds of the rectangle that surrounds ellipse. But when i rotated the ellipse it returns the rotated rectangles bounds( see attached image) ..

    ýs there a formula that calculates the Bounds of a rotated ellipse?. I searhed the net but coudnt find any resources on that..
    attached image ->

  2. #2
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Welcome to FlashKit okanulas,

    By "bounds" of an ellipse, do you mean oriented (rotated) bounding-box (OBB), as shown by the thin black line, or still an axis-aligned bounding-box (AABB), but which closely hugs the edges of the ellipse? I'm guessing the thin black line...

    If you know the height and width (major and minor axes lengths) of the ellipse, then there are several ways of doing it. I'm actually writting a tutorial on a very similar thing for my OBB rigid-body stuff, but for rectangles. It's almost ready, I'll double check it all and finally post it on my website sometime today/tomorrow.

    If you know how to use vectors, then the corners of the OBB are just the linear combinations of the minor and major axis vectors. Minor would be from the circle/dot to the middle of the right edge, major would be from the circle to the top. (You could change these though, as long as you change the following).

    For example, then
    top-right corner = minor + major,
    bottom-right corner = minor - major,
    top-left = major - minor,
    bottom-left = -major - minor.

    Hope you see what I mean. Don't forget if you want global coordinates, you'll have to add on the centre of the ellipses position vector too... the above gives the (local) coordinates from the centre circle only.

    Oh, and of course there's the easy way ( ) of putting empty MovieClips at the corners of the ellipse MovieClip (assuming it's like that and not dynamically generated) and using localToGlobal() on them. But, as I'll say in my tutorial "where's the fun in that"

    Hope this helps.
    jonmack
    flash racer blog - advanced arcade racer development blog

  3. #3
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    mabey;
    Code:
    function getBox(mc) {
     var x = mc._width / 2;
     var y = mc._height / 2;
     var min = { x: x * -1, y: y * -1 };
     var max = { x: x, y: y };
     mc.localToGlobal(min);
     mc.localToGlobal(max);
     return({ xMin: min.x, yMin: min.y, xMax: max.x, yMax: max.y });
    }
    i think this will work for any symetrical object when the shape is perfectly centered in the mc. It will return the same object that getBounds() returns. note that the co-ordinates will ALWAYS be returned relative to the _root co-ordinate space (but you can change this if you need)
    here is some hittest code i did that's kinna based on the same principle;
    http://users.nlc.net.au/oceana/games/collision3.swf
    http://users.nlc.net.au/oceana/games/collision3.zip
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  4. #4
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Blinky baby, would you mind explaining this awesome line (I cleaned it up a little to make it clearer for me, but I still can't work out how it works...)

    Code:
    if (((b1.xMin > -w2 && b1.xMax < w2) || (b1.xMin < w2 && b1.xMax > -w2)) &&
        ((b2.xMin > -w1 && b2.xMax < w1) || (b2.xMin < w1 && b2.xMax > -w1)) &&
        ((b1.yMin > -h2 && b1.yMax < h2) || (b1.yMin < h2 && b1.yMax > -h2)) &&
        ((b2.yMin > -h1 && b2.yMax < h1) || (b2.yMin < h1 && b2.yMax > -h1)))
    I've spent about a week figuring out separating axis theorem to plug into my rigid-body dynamics stuff ( am doing a racing game too... must be a bug going round ) and you seem to have done all that I have in 4 lines of code. I hate you! Then again, I am needing more collision info for response, other than just boolean. Mind if I yoink this idea and play with it for my project? That is of course if I ever understand it.

    Just one last thing - that code you gave above - is it exactly the same as getBounds()? It looks like it might be, but I haven't tested or anything. If it does, then what's the point? It will still give an AABB to a rotated ellipse ( or whatever ), not a OBB, surely?

    Cheers.
    jonmack
    flash racer blog - advanced arcade racer development blog

  5. #5
    Junior Member
    Join Date
    Jul 2005
    Posts
    3
    sorry i think i coudnt explained my problem clearly. Ý am making a paint application and when shapes are selected ,a thin selection line surrounds the shape (like the thin line when movieClips selected in Flash) . there is no problem with rectangles.They fit perfectly even after rotated. But When i draw an ellipse and rotated it i coudnt get the ellipses bounds. Actionscripts get bounds method returns the bounds of the rectangle that surrounds my ellipse as in the previous attached image (blue line is my selection tools bounds, black line is the bounds of the ellipse ). When objects are selected only blue line is visible and its far away from the ellipse's bounds. Thats how flash works i coudnt get the ellipses bounds.

    What i did was changing rotation method and render the shape after rotated with that pretty code i found here. And it works perfect..

    // Rotated ellipse //
    c= x + rx*Math.cos(r)*Math.cos(z) - ry*Math.sin(r)*Math.sin(z);
    d= y + rx*Math.sin(r)*Math.cos(z) + ry*Math.cos(r)*Math.sin(z);


    jonmack your rigidbody tutorials seem interesting . Ý cant wait to read it (but i have to wait untill the deadline of my project ..

    The 4 line code u're talking about checks the line to line collision . I did an line 2 line collision example a while ago based on the jobe makars excellent book.here is the link..
    line 2 line collision
    maybe you can figure it out faster with an example....

    And friends thanx for your support.

  6. #6
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    jonmack;
    i cant' remember what my thoughts were when i wrote that. lemme have a look and get back to you. you can use it any way you want to. as for the rigid body thing i think you will want to get the point of collision which (for my money) is another tack;
    http://users.nlc.net.au/oceana/games/collision5.html
    http://users.nlc.net.au/oceana/games/collision5.zip
    this will test a maximum of 4 points, a minimum of 2 and return the point of collision. it uses localToGlobal to get the center of each object and then calculate the point of collision. I reckon you could get it down to a maximum of two points by using some angle calculations to determine the point of collision.
    now that i look at it the getbounds() i did it just returns the top/left and bottom/right corner of the object. you could place a square at xMin/yMin, set it's rotation to the same as the target mc and set it's width and height using xMax-xMin and yMax-yMin
    okanulas;
    glad to see you got it worked out dude
    Last edited by BlinkOk; 07-10-2005 at 11:49 PM.
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

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