North, East, South, West facing from camera

I am working on a small game that uses Minecraft style blocks as models for rendering dungeons. I’m using a simple 3d array to store block data, for clipping, instanced meshes etc. I’m using this array for clipping because the number of blocks bogs down raycasting.

Everything actually works pretty well, but I am looking for a clean way to offset x and z index in the block array based on the direction of the camera, and I want to keep it simple in terms of human readability. A.k.a I want to get the facing direction and translate it into north, north east, east, south east, south… etc.

I’m learning trig as I need it, but I’m also trying to avoid as many calculations as possible.

My code is simple. I get the world direction from the camera, I rotate that direction to get left, right, and backward. I take the position + direction and round it to get an array accessor. It’s always close, but for left and right clipping, it is always off by one on either x or z. And thus allows clipping into blocks sometimes.

My thought is if I have a cardinal direction test i.e. facingNorth it becomes easy to adjust the array index for x and z.

I am working under limited resources, and can post code if it is a must, but I currently only have internet on a cheap android and it would be painful to get the code published.

Thanks for any help.

In the spirit of sharing, and because my laptop arrived today, I pushed my code and it can be seen. Keep in mind the controls are target strictly at a phone in this version and I need to add keyboard and mouse support.

https://zekenaulty.github.io/maze-quest/experiments/basic_room/

The array filtering still needs to be reworked to avoid looping over every block, and instead access the data based on x,y,z offsets from the player, but this is still very early in development.

Also, I’m using Sphax PureBD Craft textures which are the sole property of https://bdcraft.net

So far everything I try just shows me I don’t understand something simple.

For example setting the y rotation of the camera to 3.14 and 0 look south and north as expected but setting it to 0.785 and 5.495 both look to the “north east” except it’s west as the rotation is inverted.

Does the rotation go from 0 to 6.28? I realize you can give it any number and it will rotate until it reaches that number, but if the rotation is in radians why would 45 and 315 point to the same position? CCW vs. cw aside this behavior is strange.

I solved the clipping problem, by filtering my instanced blocks to a chunk of blocks in a 32 block radius.

This also yields overall performance gains, and has everything running at ~60fps with no bog down with raycasting.

I would still love to know how to get the approximation of the facing direction from the camera world direction. However, I’ll take the gains in performance and be satisfied for now.

I knew I would need to do this culling at some point, if I wanted to have truly large and expansive dungeons, but I wasn’t honestly sure the instanced mesh swapping would be a performance gain. It wasn’t until I throttled it to a 10 block distance change per update.

Being honest, the array clipping was better in terms of smooth movement. Raycasting, has a more jagged clipping in comparison, and I may end up trying to find the math I need to use the array block data eventually.

When I say jagged, I mean the raycasting is very precise, and it can create a stutter at angles that wasn’t as severe when using the integer space of the blocks.

Still it is better than sometimes strafing right and ignoring a full block.

If anyone know the calculation to correctly offset the x and z, based on the y rotation of the camera, please share. #learning

I have deep black fog at 20 blocks fyi, so things still look good even without a far field of vision…