title |
---|
Debugging |
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.
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 “hasslefree” simple shapes, and a “hasslefree” unshaded material or wireframe: No model, no light source, no materials are needed to see them in your test scene.
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.
Here are some different debug shapes:
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.
private void attachCoordinateAxes(Vector3f pos){ Arrow arrow = new Arrow(Vector3f.UNIT_X); arrow.setLineWidth(4); // make arrow thicker putShape(arrow, ColorRGBA.Red).setLocalTranslation(pos); arrow = new Arrow(Vector3f.UNIT_Y); arrow.setLineWidth(4); // make arrow thicker putShape(arrow, ColorRGBA.Green).setLocalTranslation(pos); arrow = new Arrow(Vector3f.UNIT_Z); arrow.setLineWidth(4); // make arrow thicker putShape(arrow, ColorRGBA.Blue).setLocalTranslation(pos); } private Geometry putShape(Mesh shape, ColorRGBA color){ Geometry g = new Geometry("coordinate axis", shape); Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.getAdditionalRenderState().setWireframe(true); mat.setColor("Color", color); g.setMaterial(mat); rootNode.attachChild(g); return g; }
Use a wireframe grid (com.jme3.scene.debug.Grid) as a ruler or simple floor.
private Geometry attachGrid(Vector3f pos, float size, ColorRGBA color){ Geometry g = new Geometry("wireframe grid", new Grid(size, size, 0.2f) ); Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.getAdditionalRenderState().setWireframe(true); mat.setColor("Color", color); g.setMaterial(mat); g.center().move(pos); rootNode.attachChild(g); return g; }
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.
public Geometry attachWireBox(Vector3f pos, float size, ColorRGBA color){ Geometry g = new Geometry("wireframe cube", new WireBox(size, size, size)); Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.getAdditionalRenderState().setWireframe(true); mat.setColor("Color", color); g.setMaterial(mat); g.setLocalTranslation(pos); rootNode.attachChild(g); return g; }
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.
private Geometry attachWireSphere(Vector3f pos, float size, ColorRGBA color){ Geometry g = new Geometry("wireframe sphere", new WireSphere(size)); Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.getAdditionalRenderState().setWireframe(true); mat.setColor("Color", color); g.setMaterial(mat); g.setLocalTranslation(pos); rootNode.attachChild(g); return g; }
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.
physicsSpace.enableDebug(assetManager);
With debugging enabled, colors are used to indicate various types of physical objects:
- A magenta wire mesh indicates an active rigid body.
- A blue wire mesh indicates a rigid body which is either new or inactive.
- A yellow wire mesh indicates a ghost.
- Two green arrows indicate a joint.
- A pink wire mesh indicates a character.
Making the skeleton visible inside animated models can be handy for debugging animations. The control
object is an AnimControl, player
is the loaded model.
SkeletonDebugger skeletonDebug = new SkeletonDebugger("skeleton", control.getSkeleton()); Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.setColor("Color", ColorRGBA.Green); mat.getAdditionalRenderState().setDepthTest(false); skeletonDebug.setMaterial(mat); player.attachChild(skeletonDebug);
We assume that you have loaded a model with a material mat
.
Then you can add a switch to toggle the model's wireframe on and off, like this:
- Create a key input trigger that switches between the two materials: E.g. we toggle when the T key is pressed:
inputManager.addMapping("toggle wireframe", new KeyTrigger(KeyInput.KEY_T)); inputManager.addListener(actionListener, "toggle wireframe");
- Now add the toggle action to the action listener
private ActionListener actionListener = new ActionListener() { @Override public void onAction(String name, boolean pressed, float tpf) { // toggle wireframe if (name.equals("toggle wireframe") && !pressed) { wireframe = !wireframe; // toggle boolean mat.getAdditionalRenderState().setWireframe(wireframe); } // else ... other input tests. } };
- 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
private ActionListener actionListener = new ActionListener() { boolean wireframe = false; @Override public void onAction(String name, boolean pressed, float tpf) { // toggle wireframe if (name.equals("toggle wireframe") && !pressed) { wireframe = !wireframe; // toggle boolean rootNode.depthFirstTraversal(new SceneGraphVisitor() { public void visit(Spatial spatial) { if (spatial instanceof Geometry) ((Geometry)spatial).getMaterial().getAdditionalRenderState().setWireframe(wireframe); } }); } // else ... other input tests. } };
TIP :: To set the line width of wireframe display, use mesh.setLineWidth(lineWidth). Default line width is 1.
To display the wireframe of the entire scene instead on one material at a time, first create the following Scene Processor
public class WireProcessor implements SceneProcessor { RenderManager renderManager; Material wireMaterial; public WireProcessor(AssetManager assetManager) { wireMaterial = new Material(assetManager, "/Common/MatDefs/Misc/Unshaded.j3md"); wireMaterial.setColor("Color", ColorRGBA.Blue); wireMaterial.getAdditionalRenderState().setWireframe(true); } public void initialize(RenderManager rm, ViewPort vp) { renderManager = rm; } public void reshape(ViewPort vp, int w, int h) { throw new UnsupportedOperationException("Not supported yet."); } public boolean isInitialized() { return renderManager != null; } public void preFrame(float tpf) { } public void postQueue(RenderQueue rq) { renderManager.setForcedMaterial(wireMaterial); } public void postFrame(FrameBuffer out) { renderManager.setForcedMaterial(null); } public void cleanup() { renderManager.setForcedMaterial(null); } }
Then attach the scene processor to the GUI Viewport.
getViewPort().addProcessor(new WireProcessor());
- Spatial – if you can't see certain spatials, you can modify the culling behaviour to identify problems (such as inside-out custom meshes)