Getting Cube You Are Looking At (Voxel Engine).

Started by
13 comments, last by CryoGenesis 11 years, 8 months ago
Hey I need help! (AGAIN...)
I'm writing a voxel engine. You can pick up and place blocks just like minecraft. But, it's just an experience project and it's not going to be a clone at all.
I've written a chunk generator that generates a world, the fps view (loads of trigonometry), all the basic collision and movement physics, and the block renderer.
Now all I have to do is make it so I can place and destroy the cubes. The problem is I don't know how to get the cube the player is looking at.
I have access to both the x and y angles, the player position, and a way to convert 3D space to voxel coordinate so I don't see why you can't use those to work out the block you are looking at.

Any helpful answers or links will get +1 rep and thanks.

Also, I'm using OpenGL.
Advertisement
You need to use a technique called ray casting. From the position of the player's eye (centre of the viewport) and your camera's direction vector you create a ray and then test that against your geometry to determine where it intersects.
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler

You need to use a technique called ray casting. From the position of the player's eye (centre of the viewport) and your camera's direction vector you create a ray and then test that against your geometry to determine where it intersects.

I did think of that. But, I was wandering if there is anyway to get the block with some equation?
Another solution is to use color picking. You need to code each face of the voxels in different colors. When the player clicks on the screen, you read the color at that pixel. This drawing is only done in the back buffer, so the player doesn't have to see it. (Though it is fun during development to see what it was, nice colors).

Notice that it is the face of a voxel you need to identify. The reason for that is when you add new voxels. That is done to the face of another voxel.

If you have an infinite world, it is difficult to enumerate the faces in the RGBA data, so you need to limit the size of the world that can be selected. A simple encoding is to store the x, y, and z relative distance (from the camera point of view) in r, g, and b. Every voxel have 6 faces, and this number can be stored in the alpha channel.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
to raycast start from the block the ray starts from, if its empty find out where it exits the block and which other block it enters and repeat from that new position on the surface of that next block.

I think picking using the results of rendering would be kind of messy and possibly limiting.

o3o


to raycast start from the block the ray starts from, if its empty find out where it exits the block and which other block it enters and repeat from that new position on the surface of that next block.

I think picking using the results of rendering would be kind of messy and possibly limiting.

Yeah I think I'm going to do raycasting instead just because it would be a lot cleaner and so I don't have to code a lot more.
Hey thanks guys I got it working.
Do you know how to get the face you are looking at to place blocks?
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
You dont need trig if you know the position on the surface you hit. Just a few comparisons...

o3o

Well the ray pass through the box slowly so I'm guessing that I can get away with not using trig.
Even so, I will use it just for accuracy.

This topic is closed to new replies.

Advertisement