Skip to content

Commit

Permalink
debug bounding boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
Goby56 committed Jun 24, 2024
1 parent 95d0e9e commit 85f85e1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
34 changes: 27 additions & 7 deletions src/main/java/com/goby56/wakes/render/debug/WakeDebugRenderer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.goby56.wakes.render.debug;

import com.goby56.wakes.WakesClient;
import com.goby56.wakes.simulation.QuadTree;
import com.goby56.wakes.simulation.WakeHandler;
import com.goby56.wakes.simulation.WakeNode;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
Expand All @@ -10,19 +11,38 @@
import net.minecraft.util.math.Vec3d;

import java.util.ArrayList;
import java.util.Random;

public class WakeDebugRenderer implements WorldRenderEvents.DebugRender {

@Override
public void beforeDebugRender(WorldRenderContext context) {
if (!WakesClient.CONFIG_INSTANCE.drawDebugBoxes) {
return;
if (WakesClient.CONFIG_INSTANCE.drawDebugBoxes) {
WakeHandler wakeHandler = WakeHandler.getInstance();
for (QuadTree.DebugBB debugBox : wakeHandler.getBBs()) {
System.out.println(debugBox);
if (debugBox.depth() < 4) {
continue;
}
Random r = new Random(debugBox.depth());
DebugRenderer.drawBox(context.matrixStack(), context.consumers(), debugBox.bb(), r.nextFloat(), r.nextFloat(), r.nextFloat(), 0.4f);
}
ArrayList<WakeNode> nodes = wakeHandler.getVisible(context.frustum());
for (WakeNode node : nodes) {
Vec3d pos = node.getPos().add(context.camera().getPos().negate());
Box box = new Box(pos.x, pos.y - 0.1, pos.z, pos.x + 1, pos.y - 0.2, pos.z + 1);
DebugRenderer.drawBox(context.matrixStack(), context.consumers(), box, 1f, 0f, 1f, 0.4f);
}
}
ArrayList<WakeNode> nodes = WakeHandler.getInstance().getVisible(context.frustum());
for (WakeNode node : nodes) {
Vec3d pos = node.getPos().add(context.camera().getPos().negate());
Box box = new Box(pos.x, pos.y - 0.1, pos.z, pos.x + 1, pos.y - 0.2, pos.z + 1);
DebugRenderer.drawBox(context.matrixStack(), context.consumers(), box, 1f, 0f, 1f, 0.4f);
}

public static class TreeBoundingBox {
public Box bb;
public int depth;

public TreeBoundingBox(Box bb, int depth) {
this.bb = bb;
this.depth = depth;
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/goby56/wakes/simulation/QuadTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@ private void distribute() {
throw new NotImplementedException();
}

public void getBBs(ArrayList<DebugBB> bbs, int height) {
bbs.add(new DebugBB(this.bounds.toBox(height), this.depth));
if (this.NE == null) {
return;
}
this.NE.getBBs(bbs, height);
this.NW.getBBs(bbs, height);
this.SW.getBBs(bbs, height);
this.SE.getBBs(bbs, height);
}

public record DebugBB(Box bb, int depth) {
}

public record AABB(int x, int z, int width) {
public boolean contains(int x, int z) {
return this.x - this.width <= x && x <= this.x + this.width &&
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/com/goby56/wakes/simulation/WakeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.Frustum;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.math.Box;
import net.minecraft.world.World;

import java.util.ArrayList;
Expand Down Expand Up @@ -101,16 +102,14 @@ public int getTotal() {
return n;
}

public ArrayList<WakeNode> getNearby(int x, int y, int z) {
ArrayList<WakeNode> foundNodes = new ArrayList<>();
int i = this.getArrayIndex(y);
if (i < 0) return foundNodes;
if (this.trees.get(i) == null) {
return foundNodes;
public ArrayList<QuadTree.DebugBB> getBBs() {
ArrayList<QuadTree.DebugBB> boxes = new ArrayList<>();
for (int y = 0; y < this.maxY - this.minY; y++) {
if (this.trees.get(y) != null) {
this.trees.get(y).getBBs(boxes, y - Math.abs(this.minY));
}
}
QuadTree.Circle range = new QuadTree.Circle(x, z, this.MAX_QUERY_RANGE);
this.trees.get(i).query(range, foundNodes);
return foundNodes;
return boxes;
}

private int getArrayIndex(int height) {
Expand Down

0 comments on commit 85f85e1

Please sign in to comment.