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.
Targeting Box
-
- Posts: 77
- Joined: Sat Jul 08, 2006 5:55 am
- Location: Cyberjaya, Malaysia
-
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
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.
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
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
-
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
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:
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
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way