Reputation: 87
I am writing a voxel game in Unity.
The world of the game is a 3d array voxels (cubes).
Each voxel may either be empty or occupied.
I need to cast a ray from any point within the world and figure out where will it first hit a surface of an occupied voxel.
public struct Vector3 {
public float x, y, z;
}
public struct Vector3Int {
public int x, y, z;
}
public enum Voxel {
Empty,
Occupied,
}
public struct RaycastResult {
public bool success; // whether any block was hit by raycast
public Vector3 point; // point where ray hits the surface of first solid voxel
public Vector3Int block; // what block was hit by raycast
}
public class World {
public Voxel[,,] voxels;
public Voxel GetVoxel(Vector3Int position) {
return voxels[position.x, position.y, position.z]
}
public RaycastResult Raycast(Vector3 origin, Vector3 direction) {
// What to do here?
}
}
What algorithm do I use to determine this?
Is there an (efficient) way to do this in unity without writing my own raycaster? Note that the size of the world may be very large.
If there is no good builtin solution, is there a general algorithm?
Upvotes: -2
Views: 1229
Reputation: 36629
Here are some basic principles. You will need to be fairly comfortable with linear algebra to even attempt this, and read and understand how ray-plane intersection works.
Your ray will start inside a cube, and it will hit one of the 6 faces on its way out of the cube. In normal cases we can quickly eliminate three of the faces by just checking if the ray direction points in the same direction as the cube-face. This is done by checking the sign of the dot product between the vectors. To find the first hit of the three remaining faces we do an intersection test to the corresponding plane, and pick the closest one, if you know the hit face you know what cube it hit. If that cube is empty you repeat the process until you find a non-empty cube. You may also add some checks to avoid edge cases, like eliminating all planes that are parallel to your ray as early as possible.
However, to get any real speed you really need some kind of tree structure to reduce the number of checks done. There are many alternatives, kd-trees, r-trees etc, but in this specific case I would probably consider a sparse octree. This means your cubes will be part of larger 2x2x2 sections, where the whole section can be tagged as empty, filled, partial filled etc. These sections will also be grouped into larger 2x2x2 sections and so on, until you hit some max size that either contains your entire play area, or one independently loadable unit of your play area, and have some logic to test multiple units if needed.
Raycasting is done more or less the same way with a octree as in the simple case, except you now have variable sized cubes. And when you hit a face you need to traverse the tree to find the next cube to test.
But to actually make a well optimized implementation of this requires quite a bit of experience, both in the concepts involved, and in the language and hardware. You may also need insight in the structure of your actual game-world, since there might be non obvious shortcuts you can take that significantly help speedup performance. So it is not guaranteed that your implementation will be faster than unitys built in.
Upvotes: 2