putting a 2D box around an object?

Blitz3D Forums/Blitz3D Programming/putting a 2D box around an object?

DrakeX(Posted 2004) [#1]
i'm making a flight-sim-esque game, and i'd like to be able to draw a 2D box around an enemy when it crosses near the aiming reticle, as part of the HUD. i've seen this done in first-person-perspective games, and it does a lot to help with visibility, especially aginst busy backgrounds.

problem is, i've no idea how i'd go about this. i was thinking maybe it would be possible to use the (extended command) EntityRadius() to approximate how big the object is, then factor in the object's distance from the camera, and draw a box that way. but i'm not real sure how well that'd work.

has anyone done anything similar who could offer some advice?


BODYPRINT(Posted 2004) [#2]
I used this feature in a space game.

I made sprites that were simply scaled according the distance from the camera.

Worked a charm. If you like I can dig up the code and give you a demo.


DrakeX(Posted 2004) [#3]
would be nice :)


Perturbatio(Posted 2004) [#4]
or you can get this code here: http://www.blitzbasic.com/codearcs/codearcs.php?code=712


jfk EO-11110(Posted 2004) [#5]
EntityRadius does not return a radius, it's used to set the Collision Radius if spherical Collision is used.

This is really not so easy since ProjectCamera will return the Meshes center onscreen, but a the mesh usually don't knows how big it is since ScaleEntity and ScaleMEsh is involved, and EntityWidht / Height / Depth depends on the View Angle

What I would do is: After loading the mesh I would parse all Vertices of the mesh and store Min xyz and max xyz, These can be used to create 8 Pivots that are representing a Box that completely and exactly covers the mesh. Then I'd parent the pivots to the mesh, so any further scaling will affect the pivot loactions too.

Now to draw the rectangle simply use ProjectCamera with all 8 Pivots and find the onscreen Min xy and max xy pivots. Now you've got the 4 2D Points to draw the rectangle.

Probably there is an easier solution, I don't know right now.
EDIT - I must agree, the sprite solution is clever, although the angle of view may produce inaccuracies.


N(Posted 2004) [#6]
http://blitzbasic.com/codearcs/codearcs.php?code=1089


DrakeX(Posted 2004) [#7]
perturbatio: oh, how cool :D and it's decently fast, too. works fine even with a 32-segment sphere, which is something like 4000 polys.

jfk: that's a very slick idea. calculating from 8 points rather than all vertices in the mesh.. that sounds like the winner.

noel: cool as well, pretty similar to the code perturbatio posted. :D


BODYPRINT(Posted 2004) [#8]
OK, here is my game.
If you have trouble finding the appropriate code let me know and I will try decipher my own program :-)

a=accelerate
z=decelerate
h=hyperspace
left mouse to fire.

c to send chat messages.

Net code is all working I think. It uses my own GNET server with 4 hour refresh.

http://members.iinet.net.au/~pmerwarth/SOS.zip

[edit]
OK here is the section that updates the sprite size.

For galax.planet=Each planet
distance#=EntityDistance (galax\marker,this)
crosssize#=distance/20
ScaleSprite galax\marker,crosssize,crosssize
If crosssize<galax\scale-1 Then crosssize=galax\scale-1
EntityRadius galax\marker,crosssize
Next

[/edit]


DrakeX(Posted 2004) [#9]
thanks for that phil :) though i think i'll be using jfk's method, as my objects are of funny size.

your net code might come in handy eventually, however ;)


DrakeX(Posted 2004) [#10]
i got jfk's code working beautifully :) thanks all!


Difference(Posted 2004) [#11]
What I would do is: After loading the mesh I would parse all Vertices of the mesh and store Min xyz and max xyz, These can be used to create 8 Pivots that are representing a Box that completely and exactly covers the mesh. Then I'd parent the pivots to the mesh, so any further scaling will affect the pivot loactions too.

That's what I do too, or rather I make a transparent cube parented to the entity, and make only the cube pickable. That way I control exactly how the entitybox looks. I also make the cubes slightly larger than the objects, but that's another story.