|
Calculating bounding rectangle of rotation |
Matt Weir
Member #7,476
July 2006
|
Hi there, I've been scratching my head over this a little and I thought someone might know the answer to this, my trigs a little rusty... I want to know what the measurements would be (ie. how to calculate) a bounding rectangle around a rotated rectangle. A pictures worth 1000 words.... http://www.angelfire.com/funky/feartheweasel/pics/Rotate.jpg Thanks! |
vbovio
Member #8,283
January 2007
|
One way could be to rotate the 4 points of the rectangle, then get the min and max values of x and y, and then compute the bounding rectangle width and height. --------------- |
Kris Asick
Member #1,424
July 2001
|
I recommend the same approach. Find the corners of the rectangle to enclose and use the smallest XY values for the top-left corner of the bounding rectangle, and the largest XY values for the bottom-right corner. However, I'm a little perplexed as to why you want to use a bounding rectangle instead of a bounding circle. A bounding circle is much easier to calculate. All it takes is a little Pythagorean Theorem magic. --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
Richard Phipps
Member #1,632
November 2001
|
|
CGamesPlay
Member #2,559
July 2002
|
Quote: One way could be to rotate the 4 points of the rectangle Better, for rectangles, is to take two points on one side, since you can mirror the others across the fulcrum. This should save you two sin/cos calls. -- Ryan Patterson - <http://cgamesplay.com/> |
Archon
Member #4,195
January 2004
|
Quote: This should save you two sin/cos calls. If the angles are whole numbers (integers), then these 'expensive' sin/cos calls could be turned into a lookup table (array). |
CGamesPlay
Member #2,559
July 2002
|
Okay, but using two out of four points still saves you 4 lookups -- Ryan Patterson - <http://cgamesplay.com/> |
Kikaru
Member #7,616
August 2006
|
You can also multiply the values of the sin/cos in the table by 128, then just >>7 the number after multiplication. This saves on use of floating point math. (note: I have been working with this stuff [rotation] for a couple months, so I have learned much ) |
Matt Weir
Member #7,476
July 2006
|
Cheers, I've got it doing what I want now. I was mainly trying to cut down on calculating all four points and just doing two. In the meantime I also constructed some lookup tables. I shouldn't be worrying about speed to much, it's only really for previewing things accurately. (see below) Cheers for this pointer Kikaru: Quote: Kikaru: You can also multiply the values of the sin/cos in the table by 128, then just >>7 the number after multiplication. This saves on use of floating point math. I was just thinking about doing something like this to help accuracy... The reason I'm not using a bounding circle is that this is for a quick routine I wrote to draw a transparent rotated sprite in standard Allegro (only has trans_sprite OR rotate_sprite....). My game uses Openlayer (or Allegro if you want about half the framerate, wasn't hard to do both) but my Animation editor can only use Allegro because I use the Allegro GUI. Here's a pic anyway if anyones interested. It's all based on 2d skeletal animation with interpolation between frames (can't see animation in a screenshot though...). Each bone supports it's own skin animations too (the flower bobs up and down in a happy way). All the programming for the game is nearly done, just starting to draw graphics and make other game content now. BTW I'm not sure what's up with the guy being a cactus, I just drew the skins quickly for testing... http://www.angelfire.com/funky/feartheweasel/pics/GRIM_ScreenShots_3_2_2007.jpg |
Paul whoknows
Member #5,081
September 2004
|
[off-topic] ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
ImLeftFooted
Member #3,935
October 2003
|
I like the cactus. You should make it a bonus special character. |
Matt Weir
Member #7,476
July 2006
|
Haha yeah, 'Bonus Character!' is what I thought too when I chucked the skin on the bones! As for downloading I guess I could put the GRIM editor online. I don't imagine it would be much use for anyone other than having a look and a play but I don't mind. I'll start a thread in the proper place... EDIT: Download available in this thread if anyones interested. |
Archon
Member #4,195
January 2004
|
That's the kind of thing that I'm working on too. |
Matt Weir
Member #7,476
July 2006
|
Cool! I've wanted to do this for a while and had a brief tech demo going mid last year but never did anything more for 6 months. I completely rewrote the first attempt into a proper extendible structure and started the editor last week. If you want to compare notes or anything I don't mind at all! Something I've learnt might help you out or vice versa. How are you handling skin drawing order? Matt. |
Archon
Member #4,195
January 2004
|
Quote: How are you handling skin drawing order? Whatever the furthest is from the camera is drawn first. Use glDepthFunc(GL_LEQUAL) to draw things in order (like as if the bitmaps would do it). |
Matt Weir
Member #7,476
July 2006
|
Cool. I meant more in terms of structures, it was a bit of a problem I had originally because I use a linked list tree structure to hold all the bones with their lengths and relative angles to each other. I originally just drew the skins as the bone position was calculated when iterating through the tree but of course the order wasn't perfect all the time. In the end I made a seperate linear linked list of pointers to the bones which is the draw order, it makes it easy to change. Under the 'Bones' section in my editor is actually the list of all the bones where you can click and drag the order all around. Screenshot: |
Kikaru
Member #7,616
August 2006
|
Since you said "bounding circle," I remembered a neat little trick for copying that: //for collision at a known distance if ((x_diff < d1)&&(y_diff < d1)&&(x_diff + y_diff < d2)) hit = true; This creates a "bounding octagon," which works a bit like a circle. //for estimating distances if (x_diff > y_diff) dist = (x_diff+x_diff+y_diff)>>2; else dist = (x_diff+y_diff+y_diff)>>2; this is a fairly accurate distance check, least accurate around 45o or so. Best for checking "which is closer" than actual distances. |
|