Targeting Box

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
thomascheah
Posts: 77
Joined: Sat Jul 08, 2006 5:55 am
Location: Cyberjaya, Malaysia

Targeting Box

Post by thomascheah »

How do I calculate a targeting box in screen coordinates that encloses an object? For example, in flight simulator, whenever you saw a bogie in front of you, you have a targeting box in your HUD that encloses the enemy ship. I want to achieve the same.

Thanks in advance.
Objective World Pvt. Ltd.
"Turning Knowledge Into Wisdom."
http://www.objectiveworld.com
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

render the bounding box of the model?

driver->draw3DBox (node->getTransformedBoundingBox(), SColor(255, 255, 255, 255));

call that after smgr->drawAll();
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

That's the second time today that you didn't read the question fully. One more, and you're ready for promotion to quality assurance. :P

Code: Select all

#include <irrlicht.h>

#pragma comment(lib, "Irrlicht.lib")

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

int main()
{

	IrrlichtDevice *device =
		createDevice( video::EDT_OPENGL, dimension2d<s32>(640, 480), 16,
			false, false, false, 0);

	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();

	IAnimatedMesh* mesh = smgr->getMesh("../../media/sydney.md2");
	IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );
	if (node)
	{
		node->setMaterialFlag(EMF_LIGHTING, false);
		node->setMD2Animation ( scene::EMAT_STAND );
		node->setMaterialTexture( 0, driver->getTexture("../../media/sydney.bmp") );
	}

	ICameraSceneNode * cam = smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));
	ISceneCollisionManager * collMan = smgr->getSceneCollisionManager();

	while(device->run())
	{
		driver->beginScene(true, true, SColor(255,100,101,140));
		smgr->drawAll();

		aabbox3df box = node->getTransformedBoundingBox();

		vector3df corners[8];
		box.getEdges(corners);
		rect<s32> screenBounds;
		screenBounds.UpperLeftCorner.X = 9999;
		screenBounds.UpperLeftCorner.Y = 9999;
		for(int corner = 0; corner < 8; ++corner)
		{
			position2di screenPosition = collMan->getScreenCoordinatesFrom3DPosition(corners[corner], cam);
			screenBounds.UpperLeftCorner.X = min_(screenBounds.UpperLeftCorner.X, screenPosition.X);
			screenBounds.UpperLeftCorner.Y = min_(screenBounds.UpperLeftCorner.Y, screenPosition.Y);
			screenBounds.LowerRightCorner.X = max_(screenBounds.LowerRightCorner.X, screenPosition.X);
			screenBounds.LowerRightCorner.Y = max_(screenBounds.LowerRightCorner.Y, screenPosition.Y);
		}

		driver->draw2DRectangle(SColor(64, 255, 0, 0), screenBounds);

		driver->endScene();
	}

	device->drop();

	return 0;
}


Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Ahh fiddlesticks! That's it... down the road... not across the tracks...

But to be fair, why would you want a silly ol' 2D HUD? That's soooo last-gen! 3D HUDs is the way forward!
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Less is more these days. That extra dimension can be knitted into a scarf for a starving eskimo child.

Oh, and the reason that the 2d rectangle is larger than the model is because it's using the extremes of the bounding box. node->setDebugDataVisible(-1); to see what's going on. If you want an accurate box, you'll need something like this:

Code: Select all

        IMesh * mesh = node->getMesh()->getMesh((s32)node->getFrameNr());
        matrix4 transform = node->getAbsoluteTransformation();

        for(u32 bufferId = 0; bufferId < mesh->getMeshBufferCount(); ++bufferId)
        {
            IMeshBuffer * buffer = mesh->getMeshBuffer(bufferId);
            if(buffer->getVertexType() != EVT_STANDARD) 
                continue;

            S3DVertex * vertices = (S3DVertex *)(buffer->getVertices());
            for(s32 vertexId = (s32)buffer->getVertexCount() - 1; vertexId >= 0; --vertexId)
            {
                vector3df position = vertices[vertexId].Pos;
                transform.transformVect(position);

                position2di screenPosition = collMan->getScreenCoordinatesFrom3DPosition(position, cam);
                screenBounds.UpperLeftCorner.X = min_(screenBounds.UpperLeftCorner.X, screenPosition.X);
                screenBounds.UpperLeftCorner.Y = min_(screenBounds.UpperLeftCorner.Y, screenPosition.Y);
                screenBounds.LowerRightCorner.X = max_(screenBounds.LowerRightCorner.X, screenPosition.X);
                screenBounds.LowerRightCorner.Y = max_(screenBounds.LowerRightCorner.Y, screenPosition.Y);
            }
        }
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
pera
Posts: 460
Joined: Wed May 14, 2008 1:05 pm
Location: Novi Sad, Serbia
Contact:

Post by pera »

this is getting better then my daily portion of Scrubs.
:)
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Scrubs has gone downhill in the recent series :(

On a brighter note it seems that rogerborg just gets better and better! :lol:
Image Image Image
Post Reply