Game Development Community

Getting the 2d bounding box of a 3d model

by Drew Parker · in Torque Game Engine · 03/29/2004 (2:51 pm) · 6 replies

I'm developing some simple targeting code, like Deus Ex or System Shock 2, or any Mech or Space sim. What I'd like to do is draw a bounding box around an object when the player targets it, but I don't want it to be the 3d bounding box, like the object selection resource, but just a simple 2d bounding box.

My question: Is there a function that gives you the screen coordinates for a box that bounds any given visible model?

If not, I'm thinking how I'll calculate it is grab the 3d bounding box with getWorldBox(), then find the screen coords for each point in the bounding box, and calculate which points are the farthest out from the model. Then I'll use those points to determine how big the box should be. I know that's not overly pretty, but I can't think of a better way right now. Does anyone have any ideas on a better way to do this?

#1
03/29/2004 (3:26 pm)
Yes there is.. You have to project the 3D coords to 2D. Check out the function dglPointToScreen for what you need.

- Brett
#2
03/29/2004 (6:20 pm)
Hey Brett, thanks for replying to my call for help, I need all the help I can get. :)

I've worked with the dglPointToScreen() function before, and I see how I can use it to convert the 3d points from the model's 3d bounding box to 2d points, then go from there to derive a 2d bounding box for my targeting code. But before I take a shot at it, I was just curious if there was a quicker route.

So if there is no model.get2dBoundingBox() function, does anyone have any good ideas on implementation? It seems like this kind of functionality is used a lot, so maybe there's a good algorithm that's recommended? Or could you code this any which way and there would be no speed hit (seeing as this function will be called a lot, anytime the player comes across an item or NPC)?
#3
03/29/2004 (8:18 pm)
You're finding a few minimum and maximum values of eight points. Even the sloppiest code around ought to be pretty fast with such a small data set.
#4
03/30/2004 (12:20 pm)
Has anyone ever had any trouble with dglPointToScreen()?

I was wondering why my little targeting function screen point values were never changing as I rotated the player, and I pinned it down to that when I call dglPointToScreen(), the screen points it's giving me back are the same as the 3d point I pass it in, with the excepetion that the z value is inverted.

Anyone have an idea as to what I'm doing wrong? I included dgl.h at the top of the file I'm working in. I've read of one person having a similiar problem, as well as someone on my team has had the same problem. I'm going to try to get the GUI classes' project() function to work for me instead.
#5
04/14/2004 (6:53 am)
Ok, I got this working. I still couldn't get dglPointToScreen() to work, so I had to move my code into a GUI class (because the GUIs have project() and unproject()). I think I put my code in GameTSCtrl(). Using project and unrpoject works great. I made a function, I pass it a ShapeBase object pointer, it takes the shapebase and calculates the 2d bounds off of the 3d collision bounds (which isn't totally accurate, needs a better 3d box that is closer to the model's size), then the function stores the box points in an array, and also stores the center point.

After that, my render function uses these points to draw the targeting brackets, and to check if the points are still on screen and if it needs to draw the targeting reticles at all. In the process, I learned how to use some drawing functions and others which was pretty nice.

Basically, what I have now is a resizing target reticle that looks pretty much like Deus Ex (except with the in-and-out jittery motion :) )
#6
07/23/2005 (5:33 am)
I need to do something similar. Is there any example code you could share?