How to find where a 3d point is drawn on the screen

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
greenman
Posts: 7
Joined: Mon Dec 30, 2019 3:43 am

How to find where a 3d point is drawn on the screen

Post by greenman »

Ok so i'm making an RTS style game and one of the things the game nees is a drag select feature.
Basically you create a box with ur mouse and everything in the box should be selected.
Now i figured an easy way to do would be to use a transformation matrix.
The way it should work is that everything that is visually inside the box should be selected but the objects are in 3d space and the box is 2d.
Now it just so happens that a transformation from 3d to 2d already exist and it's the one that is being used to draw everything on the screen so if i can just figure out where a given object is drawn on the screen in the same coordinate system as is used for drawing 2d images (which im using for the selection box) than i easily figure out which object should be selected.
Now i have a very rudimentary understanding of 3d graphics and so i know that for 3d points to be drawn in the right location is through various matrix transformations so i tried something like this.
matrix4 viewMatrix = driver_->getTransform(ETS_VIEW);
vector3df transformedPoint;
viewMatrix.translateVect(transformedPoint);
but it's not really working properly i also tried the projectionmatrix and the worldmatrix thought they seem to just be identity matrices (just ones along the diagonal)
Anyway I'm not that familiar with this stuff from what i remember i should be able to combine all the matrices and then transfrom the point but I'm not sure how to do that and im not even sure if I'm getting the right matricies.
Note that I'm moving the camera around and rotating it and everything is being draw correctly.

Sorry if i made some types my screen is kinda beyond repair so i can't really see what I'm doing all the time.
CuteAlien
Admin
Posts: 9652
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: How to find where a 3d point is drawn on the screen

Post by CuteAlien »

When you really go from 3d to 2d you lose one dimension and don't get it back.
With depth-buffer you could get it back in theory, but that's not the way to do this generally.

Easiest solution: Shoot a ray from your mouse into the screen and collide that ray with 3d geometry. As result you get a 3d coordinate. Check http://irrlicht.sourceforge.net/docu/cl ... nager.html for a few functions which can be used for this, especially the getRayFromScreenCoordinates.

For RTS-games it's sometimes even easier as some of them have a flat floor. In that case you don't have to collide against your 3d geometry but you can collide against a 3d plane. Irrlicht has a plane class with functions for that http://irrlicht.sourceforge.net/docu/cl ... ane3d.html
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
greenman
Posts: 7
Joined: Mon Dec 30, 2019 3:43 am

Re: How to find where a 3d point is drawn on the screen

Post by greenman »

Yeah i know you lose one dimension but i actually don't care about that since it is rts style it will be very rare for anything that you can select to be behind something else so the "depth" really doesn't matter.
Also I'm talking about box selection which is a rectangle you make with ur mouse and everything in this rectangle needs to be selected.
So a simple ray wouldn't be enough unless i would shoot thousands of rays inside the box which seems like a bad idea.
If i where to use collision i would have to find the collision between some kind of pyramid and everything else.
This problem is actaually very similar to frustrum culling except instead of culling everything inside the frustrum i want to find what is inside the frustrum but it's also not the whole view frustrum but a specific smaller part of it.
I just noticed a function which looks very close to what i need getSceneNodeFromScreenCoordinatesBB
but it runs into the same problem of only checking for a single point and not a rectangle.
greenman
Posts: 7
Joined: Mon Dec 30, 2019 3:43 am

Re: How to find where a 3d point is drawn on the screen

Post by greenman »

ok i just found this function getScreenCoordinatesFrom3DPosition which is exactly what i need. thx for the help
Post Reply