From c29a4bb1c6afa10b54ec5306c01210ea408a388c Mon Sep 17 00:00:00 2001 From: Stephen Gold Date: Fri, 16 Nov 2018 11:26:39 -0800 Subject: [PATCH] fix for JME issue #887 (debug mesh ignores scaling) --- .../debug/BulletCharacterDebugControl.java | 31 ++++++++++++++---- .../debug/BulletGhostObjectDebugControl.java | 32 +++++++++++++------ .../debug/BulletRigidBodyDebugControl.java | 28 ++++++++++++---- .../jme3/bullet/util/DebugShapeFactory.java | 12 ++----- 4 files changed, 72 insertions(+), 31 deletions(-) diff --git a/jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletCharacterDebugControl.java b/jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletCharacterDebugControl.java index 687d6ba27..514e79b16 100644 --- a/jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletCharacterDebugControl.java +++ b/jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletCharacterDebugControl.java @@ -67,6 +67,11 @@ public class BulletCharacterDebugControl extends AbstractPhysicsDebugControl { * geometry to visualize myShape (not null) */ protected Spatial geom; + /** + * physics scale for which geom was generated + */ + final private Vector3f oldScale = new Vector3f(); + /** * Instantiate an enabled control to visualize the specified character. * @@ -78,7 +83,10 @@ public class BulletCharacterDebugControl extends AbstractPhysicsDebugControl { super(debugAppState); this.body = body; myShape = body.getCollisionShape(); - this.geom = DebugShapeFactory.getDebugShape(body.getCollisionShape()); + oldScale.set(myShape.getScale()); + + this.geom = DebugShapeFactory.getDebugShape(myShape); + this.geom.setName(body.toString()); geom.setMaterial(debugAppState.DEBUG_PINK); } @@ -110,15 +118,24 @@ public class BulletCharacterDebugControl extends AbstractPhysicsDebugControl { */ @Override protected void controlUpdate(float tpf) { - if(myShape != body.getCollisionShape()){ - Node node = (Node) this.spatial; + CollisionShape newShape = body.getCollisionShape(); + Vector3f newScale = newShape.getScale(); + if (myShape != newShape || !oldScale.equals(newScale)) { + myShape = newShape; + oldScale.set(newScale); + + Node node = (Node) spatial; node.detachChild(geom); - geom = DebugShapeFactory.getDebugShape(body.getCollisionShape()); - geom.setMaterial(debugAppState.DEBUG_PINK); + + geom = DebugShapeFactory.getDebugShape(myShape); + geom.setName(body.toString()); + node.attachChild(geom); } - applyPhysicsTransform(body.getPhysicsLocation(location), Quaternion.IDENTITY); - geom.setLocalScale(body.getCollisionShape().getScale()); + geom.setMaterial(debugAppState.DEBUG_PINK); + + body.getPhysicsLocation(location); + applyPhysicsTransform(location, Quaternion.IDENTITY); } /** diff --git a/jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletGhostObjectDebugControl.java b/jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletGhostObjectDebugControl.java index 09f24c7fc..bdb6932d7 100644 --- a/jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletGhostObjectDebugControl.java +++ b/jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletGhostObjectDebugControl.java @@ -70,7 +70,10 @@ public class BulletGhostObjectDebugControl extends AbstractPhysicsDebugControl { * geometry to visualize myShape (not null) */ protected Spatial geom; - + /** + * physics scale for which geom was generated + */ + final private Vector3f oldScale = new Vector3f(); /** * Instantiate an enabled control to visualize the specified ghost object. * @@ -81,8 +84,9 @@ public class BulletGhostObjectDebugControl extends AbstractPhysicsDebugControl { super(debugAppState); this.body = body; myShape = body.getCollisionShape(); - this.geom = DebugShapeFactory.getDebugShape(body.getCollisionShape()); - this.geom.setName(body.toString()); + oldScale.set(myShape.getScale()); + + this.geom = DebugShapeFactory.getDebugShape(myShape); this.geom.setName(body.toString()); geom.setMaterial(debugAppState.DEBUG_YELLOW); } @@ -115,15 +119,25 @@ public class BulletGhostObjectDebugControl extends AbstractPhysicsDebugControl { */ @Override protected void controlUpdate(float tpf) { - if (myShape != body.getCollisionShape()) { - Node node = (Node) this.spatial; + CollisionShape newShape = body.getCollisionShape(); + Vector3f newScale = newShape.getScale(); + if (myShape != newShape || !oldScale.equals(newScale)) { + myShape = newShape; + oldScale.set(newScale); + + Node node = (Node) spatial; node.detachChild(geom); - geom = DebugShapeFactory.getDebugShape(body.getCollisionShape()); - geom.setMaterial(debugAppState.DEBUG_YELLOW); + + geom = DebugShapeFactory.getDebugShape(myShape); + geom.setName(body.toString()); + node.attachChild(geom); } - applyPhysicsTransform(body.getPhysicsLocation(location), body.getPhysicsRotation(rotation)); - geom.setLocalScale(body.getCollisionShape().getScale()); + geom.setMaterial(debugAppState.DEBUG_YELLOW); + + body.getPhysicsLocation(location); + body.getPhysicsRotation(rotation); + applyPhysicsTransform(location, rotation); } /** diff --git a/jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletRigidBodyDebugControl.java b/jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletRigidBodyDebugControl.java index 83e5b786f..727deebc3 100644 --- a/jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletRigidBodyDebugControl.java +++ b/jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletRigidBodyDebugControl.java @@ -70,6 +70,10 @@ public class BulletRigidBodyDebugControl extends AbstractPhysicsDebugControl { * geometry to visualize myShape (not null) */ protected Spatial geom; + /** + * physics scale for which geom was generated + */ + final private Vector3f oldScale = new Vector3f(); /** * Instantiate an enabled control to visualize the specified body. @@ -81,7 +85,9 @@ public class BulletRigidBodyDebugControl extends AbstractPhysicsDebugControl { super(debugAppState); this.body = body; myShape = body.getCollisionShape(); - this.geom = DebugShapeFactory.getDebugShape(body.getCollisionShape()); + oldScale.set(myShape.getScale()); + + this.geom = DebugShapeFactory.getDebugShape(myShape); this.geom.setName(body.toString()); geom.setMaterial(debugAppState.DEBUG_BLUE); } @@ -114,10 +120,18 @@ public class BulletRigidBodyDebugControl extends AbstractPhysicsDebugControl { */ @Override protected void controlUpdate(float tpf) { - if(myShape != body.getCollisionShape()){ - Node node = (Node) this.spatial; + CollisionShape newShape = body.getCollisionShape(); + Vector3f newScale = newShape.getScale(); + if (myShape != newShape || !oldScale.equals(newScale)) { + myShape = newShape; + oldScale.set(newScale); + + Node node = (Node) spatial; node.detachChild(geom); - geom = DebugShapeFactory.getDebugShape(body.getCollisionShape()); + + geom = DebugShapeFactory.getDebugShape(myShape); + geom.setName(body.toString()); + node.attachChild(geom); } if(body.isActive()){ @@ -125,8 +139,10 @@ public class BulletRigidBodyDebugControl extends AbstractPhysicsDebugControl { }else{ geom.setMaterial(debugAppState.DEBUG_BLUE); } - applyPhysicsTransform(body.getPhysicsLocation(location), body.getPhysicsRotation(rotation)); - geom.setLocalScale(body.getCollisionShape().getScale()); + + body.getPhysicsLocation(location); + body.getPhysicsRotation(rotation); + applyPhysicsTransform(location, rotation); } /** diff --git a/jme3-bullet/src/main/java/com/jme3/bullet/util/DebugShapeFactory.java b/jme3-bullet/src/main/java/com/jme3/bullet/util/DebugShapeFactory.java index 935130685..d56440911 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/util/DebugShapeFactory.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/util/DebugShapeFactory.java @@ -35,7 +35,6 @@ import com.jme3.bullet.collision.shapes.CollisionShape; import com.jme3.bullet.collision.shapes.CompoundCollisionShape; import com.jme3.bullet.collision.shapes.infos.ChildCollisionShape; import com.jme3.math.Matrix3f; -import com.jme3.math.Vector3f; import com.jme3.scene.Geometry; import com.jme3.scene.Mesh; import com.jme3.scene.Node; @@ -46,6 +45,7 @@ import java.util.Iterator; import java.util.List; /** + * A utility class to generate debug spatials from Bullet collision shapes. * * @author CJ Hare, normenhansen */ @@ -128,14 +128,8 @@ public class DebugShapeFactory { public static Mesh getDebugMesh(CollisionShape shape) { Mesh mesh = new Mesh(); DebugMeshCallback callback = new DebugMeshCallback(); - /* - * Populate the mesh based on an unscaled shape; - * the shape's scale will be applied later, to the geometry. - */ - Vector3f savedScale = shape.getScale().clone(); - shape.setScale(Vector3f.UNIT_XYZ); - getVertices(shape.getObjectId(), callback); - shape.setScale(savedScale); + long id = shape.getObjectId(); + getVertices(id, callback); mesh.setBuffer(Type.Position, 3, callback.getVertices()); mesh.getFloatBuffer(Type.Position).clear();