From 23729f8eee5b38f5af5cfe80ee9187461cce45eb Mon Sep 17 00:00:00 2001 From: Nathan Sweet Date: Wed, 5 Oct 2022 22:42:19 -0400 Subject: [PATCH] [libgdx] Physics debug rendering for Skeleton Viewer. --- .../spine/SkeletonRendererDebug.java | 38 +++++++++++++++---- .../spine/SkeletonViewer.java | 1 + .../spine/SkeletonViewerUI.java | 6 ++- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonRendererDebug.java b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonRendererDebug.java index f50e89c02..a29b29512 100644 --- a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonRendererDebug.java +++ b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonRendererDebug.java @@ -38,6 +38,8 @@ import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.FloatArray; +import com.esotericsoftware.spine.PhysicsConstraintData.Node; +import com.esotericsoftware.spine.PhysicsConstraintData.Spring; import com.esotericsoftware.spine.attachments.Attachment; import com.esotericsoftware.spine.attachments.BoundingBoxAttachment; import com.esotericsoftware.spine.attachments.ClippingAttachment; @@ -55,7 +57,7 @@ public class SkeletonRendererDebug { private final ShapeRenderer shapes; private boolean drawBones = true, drawRegionAttachments = true, drawBoundingBoxes = true, drawPoints = true; - private boolean drawMeshHull = true, drawMeshTriangles = true, drawPaths = true, drawClipping = true; + private boolean drawMeshHull = true, drawMeshTriangles = true, drawPaths = true, drawClipping = true, drawPhysics = true; private final SkeletonBounds bounds = new SkeletonBounds(); private final FloatArray vertices = new FloatArray(32); private float scale = 1; @@ -236,6 +238,22 @@ public class SkeletonRendererDebug { } } + if (drawPhysics) { + Array physicsConstraints = skeleton.physicsConstraints; + for (int i = 0, n = physicsConstraints.size; i < n; i++) { + PhysicsConstraint constraint = physicsConstraints.get(i); + if (constraint.mix <= 0) continue; + for (Spring spring : constraint.springs) { + shapes.setColor(Color.GOLDENROD); + shapes.line(spring.node1.x, spring.node1.y, spring.node2.x, spring.node2.y); + } + for (Node node : constraint.nodes) { + shapes.setColor(Color.GREEN); + shapes.circle(node.x, node.y, 8); + } + } + } + shapes.end(); shapes.begin(ShapeType.Filled); @@ -277,31 +295,35 @@ public class SkeletonRendererDebug { } public void setRegionAttachments (boolean regionAttachments) { - this.drawRegionAttachments = regionAttachments; + drawRegionAttachments = regionAttachments; } public void setBoundingBoxes (boolean boundingBoxes) { - this.drawBoundingBoxes = boundingBoxes; + drawBoundingBoxes = boundingBoxes; } public void setMeshHull (boolean meshHull) { - this.drawMeshHull = meshHull; + drawMeshHull = meshHull; } public void setMeshTriangles (boolean meshTriangles) { - this.drawMeshTriangles = meshTriangles; + drawMeshTriangles = meshTriangles; } public void setPaths (boolean paths) { - this.drawPaths = paths; + drawPaths = paths; } public void setPoints (boolean points) { - this.drawPoints = points; + drawPoints = points; } public void setClipping (boolean clipping) { - this.drawClipping = clipping; + drawClipping = clipping; + } + + public void setPhysics (boolean physics) { + drawPhysics = physics; } public void setPremultipliedAlpha (boolean premultipliedAlpha) { diff --git a/spine-libgdx/spine-skeletonviewer/src/com/esotericsoftware/spine/SkeletonViewer.java b/spine-libgdx/spine-skeletonviewer/src/com/esotericsoftware/spine/SkeletonViewer.java index 1e26cb6f3..5c5c1e2ff 100644 --- a/spine-libgdx/spine-skeletonviewer/src/com/esotericsoftware/spine/SkeletonViewer.java +++ b/spine-libgdx/spine-skeletonviewer/src/com/esotericsoftware/spine/SkeletonViewer.java @@ -283,6 +283,7 @@ public class SkeletonViewer extends ApplicationAdapter { debugRenderer.setPaths(ui.debugPathsCheckbox.isChecked()); debugRenderer.setPoints(ui.debugPointsCheckbox.isChecked()); debugRenderer.setClipping(ui.debugClippingCheckbox.isChecked()); + debugRenderer.setClipping(ui.debugPhysicsCheckbox.isChecked()); debugRenderer.draw(skeleton); } diff --git a/spine-libgdx/spine-skeletonviewer/src/com/esotericsoftware/spine/SkeletonViewerUI.java b/spine-libgdx/spine-skeletonviewer/src/com/esotericsoftware/spine/SkeletonViewerUI.java index ef9572574..01e227b68 100644 --- a/spine-libgdx/spine-skeletonviewer/src/com/esotericsoftware/spine/SkeletonViewerUI.java +++ b/spine-libgdx/spine-skeletonviewer/src/com/esotericsoftware/spine/SkeletonViewerUI.java @@ -112,6 +112,7 @@ class SkeletonViewerUI { CheckBox debugPathsCheckbox = new CheckBox("Paths", skin); CheckBox debugPointsCheckbox = new CheckBox("Points", skin); CheckBox debugClippingCheckbox = new CheckBox("Clipping", skin); + CheckBox debugPhysicsCheckbox = new CheckBox("Physics", skin); CheckBox pmaCheckbox = new CheckBox("Premultiplied", skin); @@ -257,7 +258,7 @@ class SkeletonViewerUI { root.add(); root.add(table(debugPathsCheckbox, debugPointsCheckbox, debugClippingCheckbox)).row(); root.add(); - root.add(table(debugMeshHullCheckbox, debugMeshTrianglesCheckbox)).row(); + root.add(table(debugMeshHullCheckbox, debugMeshTrianglesCheckbox, debugPhysicsCheckbox)).row(); root.add("Atlas alpha:"); { Table table = table(); @@ -626,6 +627,7 @@ class SkeletonViewerUI { debugPathsCheckbox.addListener(savePrefsListener); debugPointsCheckbox.addListener(savePrefsListener); debugClippingCheckbox.addListener(savePrefsListener); + debugPhysicsCheckbox.addListener(savePrefsListener); pmaCheckbox.addListener(savePrefsListener); linearCheckbox.addListener(savePrefsListener); bonesSetupPoseButton.addListener(savePrefsListener); @@ -698,6 +700,7 @@ class SkeletonViewerUI { prefs.putBoolean("debugPaths", debugPathsCheckbox.isChecked()); prefs.putBoolean("debugPoints", debugPointsCheckbox.isChecked()); prefs.putBoolean("debugClipping", debugClippingCheckbox.isChecked()); + prefs.putBoolean("debugPhysics", debugPhysicsCheckbox.isChecked()); prefs.putBoolean("premultiplied", pmaCheckbox.isChecked()); prefs.putBoolean("linear", linearCheckbox.isChecked()); if (bonesSetupPoseButton.isChecked()) @@ -740,6 +743,7 @@ class SkeletonViewerUI { debugPathsCheckbox.setChecked(prefs.getBoolean("debugPaths", true)); debugPointsCheckbox.setChecked(prefs.getBoolean("debugPoints", true)); debugClippingCheckbox.setChecked(prefs.getBoolean("debugClipping", true)); + debugPhysicsCheckbox.setChecked(prefs.getBoolean("debugPhysics", true)); pmaCheckbox.setChecked(prefs.getBoolean("premultiplied", true)); linearCheckbox.setChecked(prefs.getBoolean("linear", true)); String setupPose = prefs.getString("setupPose", "");