Example

Voxel Engine

8

McPepic 2023-08-20 13:45 (Edited)

Renders a 3D voxel world using raycasting

World: 32x32x32 voxels
Block types: 9 (including air)
Shading based on distance from camera
Camera wraps around if you go out of bounds
World goes invisible if you go inside of a block

Movement:
Right d-pad (forward, backward, left, right)
Left A & B button (up and down)

Rotation:
Right d-pad (up, down, left, right)

Debug mode shows camera position and rotation.

Feel free to modify or suggest stuff!

Update: Added world generation, changed color of sky, added variable to change the seed
Update: Can now place and break blocks with the right A and B buttons, worked on input system to be more consistent, and precalculated screen rays

Voxel Engine.nx | Open in app
2023-08-23 15:52
Voxel Engine.nx | Open in app
2023-08-21 15:03
Voxel Engine.nx | Open in app
2023-08-20 13:45

was8bit 2023-08-22 04:06

Remarkable :D


McPepic 2023-08-22 14:25

@was8bit
Thank you! I’m working on speeding up rendering at the moment.
That way, it will be more expandable!


Pablo 2023-08-23 20:09 (Edited)

Impressive!


Awsa 2023-08-24 01:45

It was hard for me to distinguish everything at first, but I’m starting to see how impressive this really is!


McPepic 2023-08-24 16:03

@Awsa
Thanks! That’s why I added lighting based on distance to the camera (Super Mario 64 did the same thing with its lighting). If you go into debug mode, it displays the camera’s position and rotation. :)

Let me know if you have any questions.


Awsa 2023-09-01 19:33

How does this even work?


McPepic 2023-09-01 20:33

@Awsa
I can provide better information on how it works if you tell me which aspect of the program you want to understand.

For example, if you want to know about the rendering:
For each tile on the screen, I get the direction to the tile relative to the camera. Then, I create a 3D line segment with that direction that starts at the camera and that has a set length.
I then check 3D points along the line in regular intervals and check if a block exists at the current position.
If one does, I get the position of the point that collided with a block and the index of the block that was hit.
I can use the block index to choose a color and the distance between the point and camera to get the brightness.

Hope this helps!


Log in to reply.