debugging.adoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. = Debugging
  2. :revnumber: 2.0
  3. :revdate: 2020/07/23
  4. When you deal with complex game engine features like animations or physics it is handy to get feedback from the engine how it interpreted the current state. Is the physical object's collision shape really where you think it is? Is the skeleton of the animated character moving like you think it should? This document shows you how to activate visual debug aides.
  5. What if you just want to quickly write code that loads models and brings them in their start position? You may not want to hunt for a sample model, convert it, add lights, and load materials. Instead you use "`hassle-free`" simple shapes, and a "`hassle-free`" unshaded material or wireframe: No model, no light source, no materials are needed to see them in your test scene.
  6. If you ever have problems with objects appearing in the wrong spot, with the wrong scale, or wrong orientation, simply attach debug shapes to your scene to have a point of reference in 3D space – just like a giant ruler. If your code positions the debug shapes correctly, but models remain invisible when you apply the same code to them, you know that the problem must be either the model (where is its origin coordinate?), or the light (too dark? too bright? missing?), or the model's material (missing?) – and not the positioning code.
  7. Here are some different debug shapes:
  8. image::how-to/debug-shapes.png[debug-shapes.png,width="600",height="220",align="center"]
  9. == Debug Shapes
  10. === Coordinate Axes
  11. The coordinate axes (com.jme3.scene.debug.Arrow) help you see the cardinal directions (X,Y,Z) from their center point. Scale the arrows to use them as a "`ruler`" for a certain length.
  12. [source,java]
  13. ----
  14. private void attachCoordinateAxes(Vector3f pos) {
  15. Arrow arrow = new Arrow(Vector3f.UNIT_X);
  16. putShape(arrow, ColorRGBA.Red).setLocalTranslation(pos);
  17. arrow = new Arrow(Vector3f.UNIT_Y);
  18. putShape(arrow, ColorRGBA.Green).setLocalTranslation(pos);
  19. arrow = new Arrow(Vector3f.UNIT_Z);
  20. putShape(arrow, ColorRGBA.Blue).setLocalTranslation(pos);
  21. }
  22. private Geometry putShape(Mesh shape, ColorRGBA color) {
  23. Geometry g = new Geometry("coordinate axis", shape);
  24. Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  25. mat.getAdditionalRenderState().setWireframe(true);
  26. mat.getAdditionalRenderState().setLineWidth(4);
  27. mat.setColor("Color", color);
  28. g.setMaterial(mat);
  29. rootNode.attachChild(g);
  30. return g;
  31. }
  32. ----
  33. === Wireframe Grid
  34. Use a wireframe grid (com.jme3.scene.debug.Grid) as a ruler or simple floor.
  35. [source,java]
  36. ----
  37. private Geometry attachGrid(Vector3f pos, int size, ColorRGBA color) {
  38. Geometry g = new Geometry("wireframe grid", new Grid(size, size, 0.2f));
  39. Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  40. mat.getAdditionalRenderState().setWireframe(true);
  41. mat.setColor("Color", color);
  42. g.setMaterial(mat);
  43. g.center().move(pos);
  44. rootNode.attachChild(g);
  45. return g;
  46. }
  47. ----
  48. === Wireframe Cube
  49. Use a wireframe cube (com.jme3.scene.debug.WireBox) as a stand-in object to see whether your code scales, positions, or orients, loaded models right.
  50. [source,java]
  51. ----
  52. public Geometry attachWireBox(Vector3f pos, float size, ColorRGBA color) {
  53. Geometry g = new Geometry("wireframe cube", new WireBox(size, size, size));
  54. Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  55. mat.getAdditionalRenderState().setWireframe(true);
  56. mat.setColor("Color", color);
  57. g.setMaterial(mat);
  58. g.setLocalTranslation(pos);
  59. rootNode.attachChild(g);
  60. return g;
  61. }
  62. ----
  63. === Wireframe Sphere
  64. Use a wireframe sphere (com.jme3.scene.debug.WireSphere) as a stand-in object to see whether your code scales, positions, or orients, loaded models right.
  65. [source,java]
  66. ----
  67. private Geometry attachWireSphere(Vector3f pos, float size, ColorRGBA color) {
  68. Geometry g = new Geometry("wireframe sphere", new WireSphere(size));
  69. Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  70. mat.getAdditionalRenderState().setWireframe(true);
  71. mat.setColor("Color", color);
  72. g.setMaterial(mat);
  73. g.setLocalTranslation(pos);
  74. rootNode.attachChild(g);
  75. return g;
  76. }
  77. ----
  78. == Wireframe for Physics
  79. You can display a wireframe of the (usually invisible) collision shape around all physical objects. Use this for debugging when analyzing unexpected behaviour. Does not work with DETACHED physics, please switch to PARALLEL or SEQUENTIAL for debugging.
  80. [source,java]
  81. ----
  82. //Create the physics space.
  83. bulletAppState = new BulletAppState();
  84. bulletAppState.setDebugEnabled(true);
  85. getStateManager().attach(bulletAppState);
  86. ----
  87. With debugging enabled, colors are used to indicate various types of physical objects:
  88. * A magenta wire mesh indicates an active rigid body.
  89. * A blue wire mesh indicates a rigid body which is either new or inactive.
  90. * A yellow wire mesh indicates a ghost.
  91. * Two green arrows indicate a joint.
  92. * A pink wire mesh indicates a character.
  93. == Wireframe for Animations
  94. Making the skeleton visible inside animated models can be handy for debugging animations. The `control` object is an AnimControl, `player` is the loaded model.
  95. .AnimControl is known to be in the main node
  96. [source,java]
  97. ----
  98. SkeletonDebugger skeletonDebug = new SkeletonDebugger("skeleton", control.getSkeleton());
  99. Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  100. mat.setColor("Color", ColorRGBA.Green);
  101. mat.getAdditionalRenderState().setDepthTest(false);
  102. skeletonDebug.setMaterial(mat);
  103. player.attachChild(skeletonDebug);
  104. ----
  105. .AnimControl is nested somewhere
  106. [source,java]
  107. ----
  108. private void debugSkeleton(Node player) {
  109. player.depthFirstTraversal(new SceneGraphVisitorAdapter() {
  110. @Override
  111. public void visit(Node node) {
  112. if (node.getControl(AnimControl.class) != null) {
  113. AnimControl control = node.getControl(AnimControl.class);
  114. SkeletonDebugger skeletonDebug = new SkeletonDebugger("skeleton",
  115. control.getSkeleton());
  116. Material mat = new Material(getApplication().getAssetManager(),
  117. "Common/MatDefs/Misc/Unshaded.j3md");
  118. mat.setColor("Color", ColorRGBA.Green);
  119. mat.getAdditionalRenderState().setDepthTest(false);
  120. skeletonDebug.setMaterial(mat);
  121. player.attachChild(skeletonDebug);
  122. }
  123. }
  124. });
  125. }
  126. ----
  127. == Example: Toggle Wireframe on Model
  128. We assume that you have loaded a model with a material `mat`.
  129. Then you can add a switch to toggle the model's wireframe on and off, like this:
  130. . Create a key input trigger that switches between the two materials: E.g. we toggle when the T key is pressed.
  131. +
  132. [source,java]
  133. ----
  134. inputManager.addMapping("toggle wireframe", new KeyTrigger(KeyInput.KEY_T));
  135. inputManager.addListener(actionListener, "toggle wireframe");
  136. ----
  137. . Now add the toggle action to the action listener.
  138. +
  139. [source,java]
  140. ----
  141. private ActionListener actionListener = new ActionListener() {
  142. @Override
  143. public void onAction(String name, boolean pressed, float tpf) {
  144. // toggle wireframe
  145. if (name.equals("toggle wireframe") && !pressed) {
  146. wireframe = !wireframe; // toggle boolean
  147. mat.getAdditionalRenderState().setWireframe(wireframe);
  148. }
  149. // else ... other input tests.
  150. }
  151. };
  152. ----
  153. . Alternatively, you could traverse over the whole scene and toggle for all Geometry objects in there if you don't want to create a new SceneProcessor.
  154. +
  155. [source,java]
  156. ----
  157. private ActionListener actionListener = new ActionListener() {
  158. boolean wireframe = false;
  159. @Override
  160. public void onAction(String name, boolean pressed, float tpf) {
  161. // toggle wireframe
  162. if (name.equals("toggle wireframe") && !pressed) {
  163. wireframe = !wireframe; // toggle boolean
  164. rootNode.depthFirstTraversal(new SceneGraphVisitor() {
  165. @Override
  166. public void visit(Spatial spatial) {
  167. if (spatial instanceof Geometry) {
  168. ((Geometry) spatial).getMaterial()
  169. .getAdditionalRenderState().setWireframe(wireframe);
  170. }
  171. }
  172. });
  173. }
  174. // else ... other input tests.
  175. }
  176. };
  177. ----
  178. TIP: To set the line width of wireframe display, use mesh.setLineWidth(lineWidth). Default line width is 1.
  179. == Example: Toggle Wireframe on the scene
  180. To display the wireframe of the entire scene instead on one material at a time, first create the following Scene Processor.
  181. [source,java]
  182. ----
  183. public class WireProcessor implements SceneProcessor {
  184. RenderManager renderManager;
  185. Material wireMaterial;
  186. public WireProcessor(AssetManager assetManager) {
  187. wireMaterial = new Material(assetManager, "/Common/MatDefs/Misc/Unshaded.j3md");
  188. wireMaterial.setColor("Color", ColorRGBA.Blue);
  189. wireMaterial.getAdditionalRenderState().setWireframe(true);
  190. }
  191. @Override
  192. public void initialize(RenderManager rm, ViewPort vp) {
  193. renderManager = rm;
  194. }
  195. @Override
  196. public void reshape(ViewPort vp, int w, int h) {
  197. throw new UnsupportedOperationException("Not supported yet.");
  198. }
  199. @Override
  200. public boolean isInitialized() {
  201. return renderManager != null;
  202. }
  203. @Override
  204. public void preFrame(float tpf) {
  205. }
  206. @Override
  207. public void postQueue(RenderQueue rq) {
  208. renderManager.setForcedMaterial(wireMaterial);
  209. }
  210. @Override
  211. public void postFrame(FrameBuffer out) {
  212. renderManager.setForcedMaterial(null);
  213. }
  214. @Override
  215. public void cleanup() {
  216. renderManager.setForcedMaterial(null);
  217. }
  218. }
  219. ----
  220. Then attach the scene processor to the +++<abbr title="Graphical User Interface">GUI</abbr>+++ Viewport.
  221. [source,java]
  222. ----
  223. getViewPort().addProcessor(new WireProcessor());
  224. ----
  225. == See also
  226. * xref:core:scene/spatial.adoc[Spatial] – if you can't see certain spatials, you can modify the culling behaviour to identify problems (such as inside-out custom meshes)