Drawing a 2d box around a 3d model

Hey gang,

Got a quick (but possibly difficult?) question I’m struggling with. With my first person space sim, I’m trying to draw a box 2d box around other ships (which are 3d models). The idea is that this represents a targeting system; a thin, dull box for other targets with a bright red box for enemies.

What’s the best way to draw this box around the target? How can I make it appear correctly on the player’s “cockpit”? Is there an easy way to do this?

One way that I thought about is creating a sprite with a texture around the outside and parenting it to the enemy, which would technically work, but would cause problems as the line would get thinner or thicker depending on how far away the target is… I’d prefer it if the line thickness remained steady.

A neat feature would be the box being literally drawn around the enemy ship instead of being square- so if the ship had a large broadside profile it would be a large box, but if it had a narrow portrait profile it would have a small box… hmm.

Any thoughts?

Try this:

min, max = yourShip.getTightBounds()

Now, place your box between the points min and max. Then, use setRenderModeWireframe to make it a wireframe box.

one feature that would be nice would be if you could define in which viewspace the getTightBounds works. afaik the current implementation gives coordinates in world-space (x/y min&max).

if you could get the relative min&max x/y relative to the camera it would be more useful.

Certainly that is possible:

min = base.cam.getRelativePoint(render, min)
max = base.cam.getRelativePoint(render, max)

No, it’s not in render coord space, but in the node’s parent’s.

Hey Pro, thanks for the help- I’ve been a bit busy to try this yet, but I will… :slight_smile:

One quick question- how can you alter the colour of the wireframe? Red for enemies, blue for friendlies, etc…

Probably using setColor.

Hey Prosoft (and others)…

I gave it a shot- I couldn’t get the solutions proposed above to work as I kept getting coordinates way, way off the 2d screen such as -11… but I solved the original problem by simply creating a 3D cube as each ship was created, setting it to wireframe then parenting it to the ship and hiding/changing the colour as necessary.

This solution seems to work pretty well, except that the wireframe rendering mode shows the ‘cross sections’ of the cube (AKA lines running from the corners along each face) which does bad things for the visual effect. It’s otherwise perfect (although I can’t see any way to increase the line thickness without adding a second, slightly smaller cube…)

Any thoughts on how I can improve this?

Oh, I thought you wanted a cube, not a box. Sorry. You’d need to use lens.project to get the 2d coordinates:

min_screen = Point3()
base.camLens.project(min, min_screen)

Awesome mate, thanks! I’ll give this a shot when I get home! :smiley:

In our space flight sim, we use an onScreenImage parented to the enemy ship with a billboard effect set onto it.

As for the big box :frowning:, that was my first pass, I couldn’t get it to work.

self.targetBillboard = OnscreenImage(image = 'Art/textures/target.png',
                                             parent = nodeTarget,
                                             color = (1,1,1,.4),
                                             scale = 1)

Hey Prosoft (and others),

Got it working. Thanks a whole bunch! :smiley:

I’ll have some screenshots ready reasonably soon!



Ok! One last quick question. I’ve discovered that you can showTightBounds on models and I’m using that (temporarily) instead of my 2d images.

How can you change the colour of the bounding box? :smiley:

Hmm, I don’t think that’s possible (I could be wrong tho)
It wouldn’t be hard to implement though, we could maybe add a set_color function to ShowBoundsEffect.

We could, but that would be silly. The purpose of the ShowBoundsEffect is to aid developers in viewing the bounding volumes, it’s not intended to be a visually pleasing effect. It’s not really optimized for production code.

If you want to draw a bounding box in the color of your choosing, I recommend constructing the box you want directly.


I see.

Well, I only seem to be having one more trouble now- maybe I’m doing something wrong, but the “centre” of the 2d objects is off. It seems like the object is incorrectly assigning the centre to be the bottom left hand corner of the image- is this a known issue or am I doing something wrong?

Thanks for the continuing help guys. :smiley: I really appreciate it.