Hello everyone.

I'm trying some stuff out (R-Trees), and I know that starling AND flash sprites can give you the AABB of a display object using getBounds(), or getRect() for flash as well - but I needed this for my camera...

I have implemented one myself for any rotated rectangle :

public static function createAABB(x:Number, y:Number, w:Number, h:Number, a:Number):Rectangle { var cont:Sprite = new Sprite(); var spr:Sprite = new Sprite(); cont.addChild(spr); spr.graphics.drawRect(0, 0, w, h); spr.rotation = a * 180 / Math.PI; var aabb:Rectangle = cont.getRect(null); aabb.x -= x; aabb.y -= y; cont = spr = null; return aabb; }

It looks very hackish since all my attempts at trigonometry failed, and I thought... well why not go through flash's internal functions anyway and go through a sprite 'surrogate' . I put it in a container to get the actual bounds of the rotated rectangle (apparently I couldn't get the right results without putting the sprite in another sprite).

Then I found this question on stackoverflow, and the last answer using matrices intrigued me :

http://stackoverflow.com/questions/622140/calculate-bounding-box-coordinates-from-a-rotated-rectangle-picture-inside

so I've made a similar function using matrices as suggested in the answer

public static function createAABB2(x:Number,y:Number,w:Number,h:Number,a:Number):Rectangle { var bounds:Rectangle = new Rectangle(x, y, w, h); var c:Number = Math.cos(a); var s:Number = Math.sin(a); var m:Matrix = new Matrix(c, s, -s, c); var topLeft:Point = m.transformPoint(bounds.topLeft); var topRight:Point = m.transformPoint(new Point(bounds.right, bounds.top)); var bottomRight:Point = m.transformPoint(bounds.bottomRight); var bottomLeft:Point = m.transformPoint(new Point(bounds.left, bounds.bottom)); var left:Number = Math.min(topLeft.x, topRight.x, bottomRight.x, bottomLeft.x); var top:Number = Math.min(topLeft.y, topRight.y, bottomRight.y, bottomLeft.y); var right:Number = Math.max(topLeft.x, topRight.x, bottomRight.x, bottomLeft.x); var bottom:Number = Math.max(topLeft.y, topRight.y, bottomRight.y, bottomLeft.y); return new Rectangle(left, top, right - left, bottom - top); }

both appear to give a similar result so I think they are ok.

I'm not sure which one is faster (so this is where by default, using the second one seems sensible anyway) and I'm not a pro at benchmarking yet.

I thought I'd share those little utility functions, which might be of use to only a small percentage of you but still!

If I missed a utility that's hiding somewhere that does exactly that then I'm sorry - and please tell me where it is!