views:

692

answers:

5

I need to know what the visible height of a display object will be after I change it's rotationX value.

I have an application that allows users to lay out a floor in 3D space. I want the size of the floor to automatically stretch after a 3D rotation so that it always covers a certain area.

Anyone know a formula for working this out?

EDIT: I guess what I am really trying to do is convert degrees to pixels.

On a 2D plane say 100 x 100 pixels, a -10 degree change on rotationX means that the plane has a gap at the top where it is no longer visible. I want to know how many pixels this gap will be so that I can stretch the plane.

In Flex, the value for the display objects height property remains the same both before and after applying the rotation, which may in fact be a bug.

EDIT 2: There must be a general math formula to work this out rather than something Flash/Flex specific. When viewing an object in 3D space, if the object rotates backwards (top of object somersaults away from the viewer), what would the new visible height be based on degrees of rotation? This could be in pixels, metres, cubits or whatever.

A: 

Have you tried using the object's bounding rectangle and testing that?

var dO:DisplayObject = new DisplayObject();
dO.rotation = 10;
var rect:Rectangle = dO.getRect();

// rect.topLeft.y is now the new top point.
// rect.width is the new width.
// rect.height is the new height.

As to the floor, I would need more information, but have you tried setting floor.percentWidth = 100? That might work.

Christopher W. Allen-Poole
No luck. It seems that the display objects height and y (in this case a canvas containing an bitmap) does not change when adjusting it's 3d rotations, even though the visible height and y is different. The bounding rectangle appears to do the same.The floor will be made up of multiple square tiles. I plan on adding rows or columns of individual tiles to the floor after a 3d rotation, until the floor area reaches the target y position. On top of the floor will be added a PNG with a walls and furniture. The floor area of the PNG will be transparent allowing the floor image to show through.
Daniel
A: 

Have you checked DisplayObject.transform.pixelBounds? I haven't tried it, but it might be more likely to take the rotation into account.

Scott
A: 

Check out this article

keyle
A: 

Rotation actually changes DisplayObject's axis's (i.e. x and y axes are rotated). That is why you are not seeing the difference in height. So for getting the visual height and y you might try this.

var dO:DisplayObject = new DisplayObject();
addChild();
var rect1:Rectangle = dO.getRect(dO.parent);
dO.rotation = 10;
var rect2:Rectangle = dO.getRect(dO.parent);
rect1 and rect2 should be different in this case. If you want to check the visual coordinates of the dO then just change dO.parent with root.

bhups
I am not getting the results I need. Should this apply to 3D rotations (rotationX, ...Y, ...Z) also?
Daniel
can you provide some piece of code?
bhups
A: 

I don't have a test case, but off the top of my head I'd guess something like:

var d:DisplayObject;
var rotationRadians:Number = d.rotationX * Math.PI / 180;
var visibleHeight:Number = d.height * Math.cos(rotationRadians);

This doesn't take any other transformations into account, though.

Cory Petosky
Perfect. That's exactly what I was after. I sense that Flash should be doing this in the background and then exposing the value automatically. I seems to hit a lot of quirks with using the internal 3D packages, as opposed to PPV or Away3D etc. Maybe a future update will be more intuitive.Any ideas on how to get the new width of the object (being smaller at the top or bottom depending on +xRotation or -xRotation)?
Daniel
Glad I could help.Assuming there's no built-in methods for this (I haven't played with Flash's 3D yet) -- for anything more complicated than a single rotation, you're probably going to need to: 1. Construct a rotation matrix 2. Multiply the coordinates of each corner of your display object's bounding box through the matrix to get their new "rotated" coordinates 3. Use the z value of each coordinate with some division to translate the x and y the appropriate amount toward the origin 4. Create a bounding box of these translated coordinates and use it's width and height.
Cory Petosky