Find the UpperRightCorner & LowerLeftCorner - rotated Rect

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
switchblade_77
Posts: 10
Joined: Thu Sep 15, 2011 9:33 am

Find the UpperRightCorner & LowerLeftCorner - rotated Rect

Post by switchblade_77 »

How do I find the UpperRightCorner and the LowerLeftCorner of a rotated Rect?

I know that I can find the UpperLeftCorner and the LowerRightCorner of the irr rect,
from that can I find the URC and the LLC ?
The center of rotation is the middle of the rect or could be any of the 4 vertex of the rect..
eg : Like If I have a trash can sprite and want to rotate it according to the lowerRight vertex if a player kicks it from the left .. or a missle projectile.. etc

Can anyone explain me how to get it, I would be really grateful...
Markhor
Posts: 20
Joined: Thu Sep 02, 2010 2:09 pm

Re: Find the UpperRightCorner & LowerLeftCorner - rotated Re

Post by Markhor »

I've not actually looked but I think I get the concept. Can't you just copy/paste and make a new function, to reverse the side? Like it's taking the same top, but you've "swapped" the left or right for it?
Maybe someone who knows what they're saying should answer.
RdR
Competition winner
Posts: 273
Joined: Tue Mar 29, 2011 2:58 pm
Contact:

Re: Find the UpperRightCorner & LowerLeftCorner - rotated Re

Post by RdR »

If you want the accurate coordinates you can try this:

Code: Select all

 
        irr::core::vector3df rotation = irr::core::vector3df(0, 45, 0); // Rotation only around Y axis
        irr::core::rectf rect = irr::core::rectf(0, 0, 10, 10);  // Rectangle
 
        // Create a 3D box from your rectangle
        irr::core::vector3df minEdge = irr::core::vector3df(rect.UpperLeftCorner.X, 0, rect.UpperLeftCorner.Y);
        irr::core::vector3df maxEdge = irr::core::vector3df(rect.LowerRightCorner.X, 0, rect.LowerRightCorner.Y);
        irr::core::aabbox3df box = irr::core::aabbox3df(minEdge, maxEdge);
 
        // Transformation matrix
        irr::core::matrix4 m;
        m.setRotationDegrees(rotation);
        m.setRotationCenter(box.getCenter(), irr::core::vector3df());
 
        // Get edges from box
        irr::core::vector3df edges[8];
        box.getEdges(edges);
 
        // Transform edges
        for(irr::u32 i = 0; i < 8; i++) {
                irr::core::vector3df edge = edges[i];
 
                if(i == 1 || i == 3 || i == 5 || i == 7) {
                        m.transformVect(edge);
                }
        }
 
        // The edges you need are 1, 3, 5, and 7
 
Just get edges 1, 3, 5 and 7 to get the correct coordinates from your rotated rectangle.
Post Reply