Skip to content

Commit

Permalink
Only render blocks within view
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixinova committed Aug 15, 2024
1 parent e86921a commit e9623db
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next
- Added side hitboxes to blocks
- Changed renderer to only render blocks within view of the player

# 0.0.20_2
*2024-08-15 01:40*
Expand Down
10 changes: 10 additions & 0 deletions src/com/nixinova/graphics/Raycast.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
import com.nixinova.main.Game;

public class Raycast {
private static final double VIEWPORT = 0.75;

public static boolean isBlockVisibleToPlayer(Game game, int blockX, int blockY, int blockZ) {
final double stepSize = Coord1.blockToPx(0.5);

// Get camera position of player
var camPos = game.controls.getCameraPosition().toPx();
var camRot = game.controls.getViewDirection();

// Define the corners of the block
int[][] cornerOffsets = {
// X, Y, Z
{ 0, 0, 0 },
{ 1, 0, 0 },
{ 0, 1, 0 },
Expand Down Expand Up @@ -44,6 +47,13 @@ public static boolean isBlockVisibleToPlayer(Game game, int blockX, int blockY,
double vecY = distY / length;
double vecZ = distZ / length;

// Early return if block is not within the player's view
double dxRot = Math.abs(vecX - camRot.x);
double dyRot = Math.abs(vecY - camRot.y);
double dzRot = Math.abs(vecZ - camRot.z);
if (dxRot > VIEWPORT || dyRot > VIEWPORT || dzRot > VIEWPORT)
continue;

// Number of steps to reach the block
int stepCount = (int) (length / stepSize);

Expand Down

0 comments on commit e9623db

Please sign in to comment.