I am working on a box-based game engine like minecraft and I was wondering how to detect the "clicked face" when placing blocks.
I have created this engine in C++ ( DirectX / D3D ), C# ( XNA ) and Flash ( away3d & papervision3d ).
The engine code is pretty much the same on each, however as one is C# and the other C++ I'm just looking for the pseudo code, theory or advice on how to "snap" placed blocks on the face that has been clicked on.
How should I get its orientation and then place a block on that face in that position with the corresponding orientation.
For now, I've decided to go with a simple but effective method:
First I've remembered the basic 3D to 2D Projection Algorythm.
If I now have a click-point on the screen, I can simply trace a line from the camera ( eye ) through the screen, up to a specific distance.
With this line "in mind" and a modified ( 3D ) version of Bresenham's ( 2D ) line algorithm, I was now able to see, if my imagined line is "pointing" onto a Block in my 3D grid.
thx for all of your suggestions.
I hope it explains my solution. ( red is the original algorythm, green is my modified version )
also: with "rounded" i mean that the original algorythm is just rounding the x-y-factor-value to get the nearest pixel. hope u guys know what i'm talking about.