|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2017 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.math.Vector3f; |
| 39 | +import com.jme3.scene.Geometry; |
| 40 | +import com.jme3.scene.Mesh; |
| 41 | +import com.jme3.scene.Node; |
| 42 | +import com.jme3.scene.Spatial; |
| 43 | +import com.jme3.scene.VertexBuffer.Type; |
| 44 | +import com.jme3.util.TempVars; |
| 45 | +import java.util.Iterator; |
| 46 | +import java.util.List; |
| 47 | + |
| 48 | +/** |
| 49 | + * |
| 50 | + * @author CJ Hare, normenhansen |
| 51 | + */ |
| 52 | +public class DebugShapeFactory { |
| 53 | + |
| 54 | + /** The maximum corner for the aabb used for triangles to include in ConcaveShape processing.*/ |
| 55 | + // private static final Vector3f aabbMax = new Vector3f(1e30f, 1e30f, 1e30f); |
| 56 | + /** The minimum corner for the aabb used for triangles to include in ConcaveShape processing.*/ |
| 57 | + // private static final Vector3f aabbMin = new Vector3f(-1e30f, -1e30f, -1e30f); |
| 58 | + |
| 59 | + /** |
| 60 | + * Creates a debug shape from the given collision shape. This is mostly used internally.<br> |
| 61 | + * To attach a debug shape to a physics object, call <code>attachDebugShape(AssetManager manager);</code> on it. |
| 62 | + * @param collisionShape |
| 63 | + * @return |
| 64 | + */ |
| 65 | + public static Spatial getDebugShape(CollisionShape collisionShape) { |
| 66 | + if (collisionShape == null) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + Spatial debugShape; |
| 70 | + if (collisionShape instanceof CompoundCollisionShape) { |
| 71 | + CompoundCollisionShape shape = (CompoundCollisionShape) collisionShape; |
| 72 | + List<ChildCollisionShape> children = shape.getChildren(); |
| 73 | + Node node = new Node("DebugShapeNode"); |
| 74 | + for (Iterator<ChildCollisionShape> it = children.iterator(); it.hasNext();) { |
| 75 | + ChildCollisionShape childCollisionShape = it.next(); |
| 76 | + CollisionShape ccollisionShape = childCollisionShape.shape; |
| 77 | + Geometry geometry = createDebugShape(ccollisionShape); |
| 78 | + |
| 79 | + // apply translation |
| 80 | + geometry.setLocalTranslation(childCollisionShape.location); |
| 81 | + |
| 82 | + // apply rotation |
| 83 | + TempVars vars = TempVars.get(); |
| 84 | + Matrix3f tempRot = vars.tempMat3; |
| 85 | + |
| 86 | + tempRot.set(geometry.getLocalRotation()); |
| 87 | + childCollisionShape.rotation.mult(tempRot, tempRot); |
| 88 | + geometry.setLocalRotation(tempRot); |
| 89 | + geometry.setLocalScale(ccollisionShape.getScale()); |
| 90 | + |
| 91 | + vars.release(); |
| 92 | + |
| 93 | + node.attachChild(geometry); |
| 94 | + } |
| 95 | + debugShape = node; |
| 96 | + } else { |
| 97 | + debugShape = createDebugShape(collisionShape); |
| 98 | + } |
| 99 | + if (debugShape == null) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + debugShape.updateGeometricState(); |
| 103 | + return debugShape; |
| 104 | + } |
| 105 | + |
| 106 | + private static Geometry createDebugShape(CollisionShape shape) { |
| 107 | + Geometry geom = new Geometry(); |
| 108 | + geom.setMesh(DebugShapeFactory.getDebugMesh(shape)); |
| 109 | + // geom.setLocalScale(shape.getScale()); |
| 110 | + geom.updateModelBound(); |
| 111 | + return geom; |
| 112 | + } |
| 113 | + |
| 114 | + public static Mesh getDebugMesh(CollisionShape shape) { |
| 115 | + Mesh mesh = new Mesh(); |
| 116 | + DebugMeshCallback callback = new DebugMeshCallback(); |
| 117 | + /* |
| 118 | + * Populate the mesh based on an unscaled shape; |
| 119 | + * the shape's scale will be applied later, to the geometry. |
| 120 | + */ |
| 121 | + Vector3f savedScale = shape.getScale().clone(); |
| 122 | + shape.setScale(Vector3f.UNIT_XYZ); |
| 123 | + getVertices(shape.getObjectId(), callback); |
| 124 | + shape.setScale(savedScale); |
| 125 | + |
| 126 | + mesh.setBuffer(Type.Position, 3, callback.getVertices()); |
| 127 | + mesh.getFloatBuffer(Type.Position).clear(); |
| 128 | + return mesh; |
| 129 | + } |
| 130 | + |
| 131 | + private static native void getVertices(long shapeId, DebugMeshCallback buffer); |
| 132 | +} |
0 commit comments