Getting Cube You Are Looking At (Voxel Engine).

Started by
13 comments, last by CryoGenesis 11 years, 8 months ago

By knowing the angle it enters the cube. If you know what angle it came from you know which face it had to pass through to get there, trigonometry will come in here


Do you have an example of how I could use trig in this way?
Advertisement
Without knowing many relevant things about your engine I'd say picking is pretty ace instead of messy or limiting. You can find all your objects, voxels and gui elements with checking a single value, and your user is guaranteed to pick exactly the pixel he chose. If you do this by rendering to an offline ID framebuffer, you can even do it with practically no performance hit.

The math isn't overwhelmingly hard either, so you can just raycast through the voxels. And it doesn't involve reading back from the GPU, which is a nice feature. Lode's tutorial has a very nice explanation which you should be able to turn into 3D pretty easily.
You can find the surface the ray huts by taking the position of the hit (i assume you dont use some laggy raycast which steps in tiny steps and each time checks what voxel its in...) and checling its distance from each 3 planes pointing along an axis.

Like if the distance of the point from the xz plane is about 0.5 (half voxels), it will be on one of the y axis surfaces depending of the sign (either -y or y surface)

o3o


Without knowing many relevant things about your engine I'd say picking is pretty ace instead of messy or limiting. You can find all your objects, voxels and gui elements with checking a single value, and your user is guaranteed to pick exactly the pixel he chose. If you do this by rendering to an offline ID framebuffer, you can even do it with practically no performance hit.

The math isn't overwhelmingly hard either, so you can just raycast through the voxels. And it doesn't involve reading back from the GPU, which is a nice feature. Lode's tutorial has a very nice explanation which you should be able to turn into 3D pretty easily.


Well, the way the engine (in its current state) gets the voxel you're looking at is just firing an invisible projectile in 3D space in a for loop. Every loop it checks it gets it's coordinates in voxel space by just dividing its x, y and z values by the voxel size. It then checks if the voxel at it's coordinate is alive or not(rendering) and returns the voxel if it is.
The way it gets the voxel to place is just working back the trajectory to the next block that isn't alive and sets it's alive boolean to true. It isn't a very good way because if you are looking at a corner of a block then the trajectory sometimes goes through the block. You can fix this by dividing the trajectory and increasing the amount of loops but it hinders performance a small amount...

You can find the surface the ray huts by taking the position of the hit (i assume you dont use some laggy raycast which steps in tiny steps and each time checks what voxel its in...) and checling its distance from each 3 planes pointing along an axis.

Like if the distance of the point from the xz plane is about 0.5 (half voxels), it will be on one of the y axis surfaces depending of the sign (either -y or y surface)


Haha yeah thats the way I do the raycasting. It isn't very laggy though seeing as instead of doing a 3d collision algorithm it just returns a coordinate in voxel 3D space so it doesn't hinder the performance unless I want perfect accuracy.
And thanks for the solution +1.

This topic is closed to new replies.

Advertisement