1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package com.jme3.bullet.util; 33 34 import com.jme3.bullet.collision.shapes.CollisionShape; 35 import com.jme3.bullet.collision.shapes.CompoundCollisionShape; 36 import com.jme3.bullet.collision.shapes.infos.ChildCollisionShape; 37 import com.jme3.math.Matrix3f; 38 import com.jme3.scene.Geometry; 39 import com.jme3.scene.Mesh; 40 import com.jme3.scene.Node; 41 import com.jme3.scene.Spatial; 42 import com.jme3.scene.VertexBuffer.Type; 43 import com.jme3.util.TempVars; 44 import java.util.Iterator; 45 import java.util.List; 46 47 /** 48 * 49 * @author CJ Hare, normenhansen 50 */ 51 public class DebugShapeFactory { 52 53 /** The maximum corner for the aabb used for triangles to include in ConcaveShape processing.*/ 54 // private static final Vector3f aabbMax = new Vector3f(1e30f, 1e30f, 1e30f); 55 /** The minimum corner for the aabb used for triangles to include in ConcaveShape processing.*/ 56 // private static final Vector3f aabbMin = new Vector3f(-1e30f, -1e30f, -1e30f); 57 58 /** 59 * Creates a debug shape from the given collision shape. This is mostly used internally.<br> 60 * To attach a debug shape to a physics object, call <code>attachDebugShape(AssetManager manager);</code> on it. 61 * @param collisionShape 62 * @return 63 */ 64 public static Spatial getDebugShape(CollisionShape collisionShape) { 65 if (collisionShape == null) { 66 return null; 67 } 68 Spatial debugShape; 69 if (collisionShape instanceof CompoundCollisionShape) { 70 CompoundCollisionShape shape = (CompoundCollisionShape) collisionShape; 71 List<ChildCollisionShape> children = shape.getChildren(); 72 Node node = new Node("DebugShapeNode"); 73 for (Iterator<ChildCollisionShape> it = children.iterator(); it.hasNext();) { 74 ChildCollisionShape childCollisionShape = it.next(); 75 CollisionShape ccollisionShape = childCollisionShape.shape; 76 Geometry geometry = createDebugShape(ccollisionShape); 77 78 // apply translation 79 geometry.setLocalTranslation(childCollisionShape.location); 80 81 // apply rotation 82 TempVars vars = TempVars.get(); 83 Matrix3f tempRot = vars.tempMat3; 84 85 tempRot.set(geometry.getLocalRotation()); 86 childCollisionShape.rotation.mult(tempRot, tempRot); 87 geometry.setLocalRotation(tempRot); 88 89 vars.release(); 90 91 node.attachChild(geometry); 92 } 93 debugShape = node; 94 } else { 95 debugShape = createDebugShape(collisionShape); 96 } 97 if (debugShape == null) { 98 return null; 99 } 100 debugShape.updateGeometricState(); 101 return debugShape; 102 } 103 104 private static Geometry createDebugShape(CollisionShape shape) { 105 Geometry geom = new Geometry(); 106 geom.setMesh(DebugShapeFactory.getDebugMesh(shape)); 107 // geom.setLocalScale(shape.getScale()); 108 geom.updateModelBound(); 109 return geom; 110 } 111 112 public static Mesh getDebugMesh(CollisionShape shape) { 113 Mesh mesh = new Mesh(); 114 mesh = new Mesh(); 115 DebugMeshCallback callback = new DebugMeshCallback(); 116 getVertices(shape.getObjectId(), callback); 117 mesh.setBuffer(Type.Position, 3, callback.getVertices()); 118 mesh.getFloatBuffer(Type.Position).clear(); 119 return mesh; 120 } 121 122 private static native void getVertices(long shapeId, DebugMeshCallback buffer); 123 } 124