2
\$\begingroup\$

I'm currently working on a Voxel game in 3D and need a tad bit of help to figure out what Voxel the player is looking at. This is the data I currently have:

GRRLIB_Camera3dSettings(Player.x, Player.y, Player.z + 4, 0,0,1, Player.x + Camera.lookx,Player.y + Camera.looky,Player.z + Camera.lookz);

And the two 3d arrays that contains the player cords and block cords:

struct Player{
  int X;
  int Y;
  int Z;
}
int World[X_MAX][Y_MAX][Z_MAX];
//The cords are ALWAYS positive. ( and the array data: 0 Means air, anything higher is solid), The player 

(GRRLIB camera is described here: http://grrlib.santo.fr/doc/group___all_func.html#ga7300940a38526ab5aa9be0f4abe4a32a)

So what I basically need to understand is how I would implement ray tracing using this data. The whole project can be found here: https://github.com/filfat/WiiCraft

For clarification I am asking for a simple way in C or C++ to create a Ray tracing algorithm that would be used in a Minecraft like game for selecting the block the player is looking at, all data that is needed is above. So in short I need help to understand the math in a Ray Tracing algorithm and need a simple example so I could implement it myself.

Thanks! :)

\$\endgroup\$
  • \$\begingroup\$ Please describe a bit more what you've tried and exactly what the problem is you're having now. How to implement an entire feature is a bit broad, so I'd suggest narrowing the scope if that's your question. \$\endgroup\$ – MichaelHouse Nov 10 '14 at 15:55
  • \$\begingroup\$ Alright, edited question to be more understandable. \$\endgroup\$ – Filiph Sandström Nov 10 '14 at 19:13
  • \$\begingroup\$ this is called traversing. It is the most important and basic setup step before the actual ray/thing collision routine takes place, to reduce the potential collider's set. Some people uses marching as a specific case of traversal, which incrementally progresses in the partitionning structure, often using crazy SSE tricks for packet marching, when the structure is regular enough (grids..). \$\endgroup\$ – v.oddou Nov 11 '14 at 1:44
  • \$\begingroup\$ @v.oddou Didnt not know that, thanks for the heads up :) \$\endgroup\$ – Filiph Sandström Nov 11 '14 at 6:17
3
\$\begingroup\$

I assume your world is represented as a grid, the algorithm you need to do line grid intersection is called Digital differential analyzer algorithm. The code too much to write quickly, you can find a full explanation here.

This algorithm/technique is used to solve different problems that need interpolating a variable over an interval. It's similar to Bresenham line drawing algorithm but extended into 3D Grid.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.