From 5c42dc4199ebc07f067ebd629ed651822bfae234 Mon Sep 17 00:00:00 2001 From: Anuken Date: Sun, 6 Jul 2025 12:36:32 -0400 Subject: [PATCH] Debug renderer for unit hitboxes --- core/assets/bundles/bundle.properties | 1 + core/src/mindustry/Vars.java | 2 + core/src/mindustry/core/Renderer.java | 4 ++ .../graphics/DebugCollisionRenderer.java | 71 +++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 core/src/mindustry/graphics/DebugCollisionRenderer.java diff --git a/core/assets/bundles/bundle.properties b/core/assets/bundles/bundle.properties index 682c627bc6..3a3e0792f4 100644 --- a/core/assets/bundles/bundle.properties +++ b/core/assets/bundles/bundle.properties @@ -1393,6 +1393,7 @@ keybind.chat_scroll.name = Chat Scroll keybind.chat_mode.name = Change Chat Mode keybind.drop_unit.name = Drop Unit keybind.zoom_minimap.name = Zoom Minimap +keybind.detach_camera.name = Toggle Free Camera mode.help.title = Description of modes mode.survival.name = Survival mode.survival.description = The normal mode. Limited resources and automatic incoming waves.\n[gray]Requires enemy spawns in the map to play. diff --git a/core/src/mindustry/Vars.java b/core/src/mindustry/Vars.java index ecaca32343..f3b4849ee6 100644 --- a/core/src/mindustry/Vars.java +++ b/core/src/mindustry/Vars.java @@ -202,6 +202,8 @@ public class Vars implements Loadable{ /** Whether to draw shadows of blocks at map edges and static blocks. * Do not change unless you know exactly what you are doing.*/ public static boolean enableDarkness = true; + /** Whether to draw debug lines for collisions. */ + public static boolean drawCollisionDebug = false; /** application data directory, equivalent to {@link Settings#getDataDirectory()} */ public static Fi dataDirectory; /** data subdirectory used for screenshots */ diff --git a/core/src/mindustry/core/Renderer.java b/core/src/mindustry/core/Renderer.java index 3afdb57201..ebcc82f852 100644 --- a/core/src/mindustry/core/Renderer.java +++ b/core/src/mindustry/core/Renderer.java @@ -415,6 +415,10 @@ public class Renderer implements ApplicationListener{ Groups.draw.draw(Drawc::draw); + if(drawCollisionDebug){ + DebugCollisionRenderer.draw(); + } + Draw.reset(); Draw.flush(); Draw.sort(false); diff --git a/core/src/mindustry/graphics/DebugCollisionRenderer.java b/core/src/mindustry/graphics/DebugCollisionRenderer.java new file mode 100644 index 0000000000..1ecd332c65 --- /dev/null +++ b/core/src/mindustry/graphics/DebugCollisionRenderer.java @@ -0,0 +1,71 @@ +package mindustry.graphics; + +import arc.*; +import arc.graphics.*; +import arc.graphics.g2d.*; +import arc.math.*; +import arc.math.geom.*; +import arc.util.*; +import mindustry.core.*; +import mindustry.gen.*; +import mindustry.world.*; + +import static arc.Core.*; +import static mindustry.Vars.*; + +public class DebugCollisionRenderer{ + + public static void draw(){ + Rect rect = camera.bounds(new Rect()); + Draw.draw(Layer.overlayUI, () -> { + //hitboxes + Draw.color(Color.green, 0.3f); + Groups.draw.each(d -> { + if(d instanceof Hitboxc h && rect.overlaps(Tmp.r1.setCentered(d.x(), d.y(), d.clipSize()))){ + Fill.square(d.x(), d.y(), h.hitSize()/2f); + } + }); + + //tile hitboxes for units + Lines.stroke(0.3f, Color.magenta); + + int rx = Mathf.clamp((int)(Core.camera.width / tilesize / 2) + 1, 0, world.width()/2); + int ry = Mathf.clamp((int)(Core.camera.height / tilesize / 2) + 1, 0, world.height()/2); + + for(int x = -rx; x <= rx; x++){ + for(int y = -ry; y <= ry; y++){ + int wx = World.toTile(Core.camera.position.x) + x; + int wy = World.toTile(Core.camera.position.y) + y; + Tile tile = world.tile(wx, wy); + if(tile != null && tile.solid()){ + Draw.color(tile.legSolid() ? Color.pink : Color.magenta); + Lines.rect(wx * tilesize - tilesize/2f, wy * tilesize - tilesize/2f, tilesize, tilesize); + } + } + } + + + Groups.draw.each(d -> { + if(d instanceof Unit u && rect.overlaps(Tmp.r1.setCentered(u.x, u.y, d.clipSize()))){ + u.hitboxTile(Tmp.r1); + + Lines.rect(Tmp.r1); + } + }); + + Lines.stroke(0.5f); + //physics + Draw.color(Color.red, 0.5f); + Groups.draw.each(d -> { + if(d instanceof Unit u && rect.overlaps(Tmp.r1.setCentered(u.x, u.y, u.clipSize()))){ + Lines.circle(u.x, u.y, u.hitSize * unitCollisionRadiusScale); + } + }); + Draw.reset(); + + }); + + + Draw.reset(); + } +}