fix for JME issue #887 (debug mesh ignores scaling)

v3.2
Stephen Gold 6 years ago
parent 6f6041f6ae
commit e63a6255cd
  1. 31
      jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletCharacterDebugControl.java
  2. 32
      jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletGhostObjectDebugControl.java
  3. 28
      jme3-bullet/src/common/java/com/jme3/bullet/debug/BulletRigidBodyDebugControl.java
  4. 12
      jme3-bullet/src/main/java/com/jme3/bullet/util/DebugShapeFactory.java

@ -67,6 +67,11 @@ public class BulletCharacterDebugControl extends AbstractPhysicsDebugControl {
* geometry to visualize myShape (not null) * geometry to visualize myShape (not null)
*/ */
protected Spatial geom; 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. * Instantiate an enabled control to visualize the specified character.
* *
@ -78,7 +83,10 @@ public class BulletCharacterDebugControl extends AbstractPhysicsDebugControl {
super(debugAppState); super(debugAppState);
this.body = body; this.body = body;
myShape = body.getCollisionShape(); 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); geom.setMaterial(debugAppState.DEBUG_PINK);
} }
@ -110,15 +118,24 @@ public class BulletCharacterDebugControl extends AbstractPhysicsDebugControl {
*/ */
@Override @Override
protected void controlUpdate(float tpf) { protected void controlUpdate(float tpf) {
if(myShape != body.getCollisionShape()){ CollisionShape newShape = body.getCollisionShape();
Node node = (Node) this.spatial; Vector3f newScale = newShape.getScale();
if (myShape != newShape || !oldScale.equals(newScale)) {
myShape = newShape;
oldScale.set(newScale);
Node node = (Node) spatial;
node.detachChild(geom); node.detachChild(geom);
geom = DebugShapeFactory.getDebugShape(body.getCollisionShape());
geom.setMaterial(debugAppState.DEBUG_PINK); geom = DebugShapeFactory.getDebugShape(myShape);
geom.setName(body.toString());
node.attachChild(geom); node.attachChild(geom);
} }
applyPhysicsTransform(body.getPhysicsLocation(location), Quaternion.IDENTITY); geom.setMaterial(debugAppState.DEBUG_PINK);
geom.setLocalScale(body.getCollisionShape().getScale());
body.getPhysicsLocation(location);
applyPhysicsTransform(location, Quaternion.IDENTITY);
} }
/** /**

@ -70,7 +70,10 @@ public class BulletGhostObjectDebugControl extends AbstractPhysicsDebugControl {
* geometry to visualize myShape (not null) * geometry to visualize myShape (not null)
*/ */
protected Spatial geom; 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. * Instantiate an enabled control to visualize the specified ghost object.
* *
@ -81,8 +84,9 @@ public class BulletGhostObjectDebugControl extends AbstractPhysicsDebugControl {
super(debugAppState); super(debugAppState);
this.body = body; this.body = body;
myShape = body.getCollisionShape(); myShape = body.getCollisionShape();
this.geom = DebugShapeFactory.getDebugShape(body.getCollisionShape()); oldScale.set(myShape.getScale());
this.geom.setName(body.toString());
this.geom = DebugShapeFactory.getDebugShape(myShape);
this.geom.setName(body.toString()); this.geom.setName(body.toString());
geom.setMaterial(debugAppState.DEBUG_YELLOW); geom.setMaterial(debugAppState.DEBUG_YELLOW);
} }
@ -115,15 +119,25 @@ public class BulletGhostObjectDebugControl extends AbstractPhysicsDebugControl {
*/ */
@Override @Override
protected void controlUpdate(float tpf) { protected void controlUpdate(float tpf) {
if (myShape != body.getCollisionShape()) { CollisionShape newShape = body.getCollisionShape();
Node node = (Node) this.spatial; Vector3f newScale = newShape.getScale();
if (myShape != newShape || !oldScale.equals(newScale)) {
myShape = newShape;
oldScale.set(newScale);
Node node = (Node) spatial;
node.detachChild(geom); node.detachChild(geom);
geom = DebugShapeFactory.getDebugShape(body.getCollisionShape());
geom.setMaterial(debugAppState.DEBUG_YELLOW); geom = DebugShapeFactory.getDebugShape(myShape);
geom.setName(body.toString());
node.attachChild(geom); node.attachChild(geom);
} }
applyPhysicsTransform(body.getPhysicsLocation(location), body.getPhysicsRotation(rotation)); geom.setMaterial(debugAppState.DEBUG_YELLOW);
geom.setLocalScale(body.getCollisionShape().getScale());
body.getPhysicsLocation(location);
body.getPhysicsRotation(rotation);
applyPhysicsTransform(location, rotation);
} }
/** /**

@ -70,6 +70,10 @@ public class BulletRigidBodyDebugControl extends AbstractPhysicsDebugControl {
* geometry to visualize myShape (not null) * geometry to visualize myShape (not null)
*/ */
protected Spatial geom; 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. * Instantiate an enabled control to visualize the specified body.
@ -81,7 +85,9 @@ public class BulletRigidBodyDebugControl extends AbstractPhysicsDebugControl {
super(debugAppState); super(debugAppState);
this.body = body; this.body = body;
myShape = body.getCollisionShape(); myShape = body.getCollisionShape();
this.geom = DebugShapeFactory.getDebugShape(body.getCollisionShape()); oldScale.set(myShape.getScale());
this.geom = DebugShapeFactory.getDebugShape(myShape);
this.geom.setName(body.toString()); this.geom.setName(body.toString());
geom.setMaterial(debugAppState.DEBUG_BLUE); geom.setMaterial(debugAppState.DEBUG_BLUE);
} }
@ -114,10 +120,18 @@ public class BulletRigidBodyDebugControl extends AbstractPhysicsDebugControl {
*/ */
@Override @Override
protected void controlUpdate(float tpf) { protected void controlUpdate(float tpf) {
if(myShape != body.getCollisionShape()){ CollisionShape newShape = body.getCollisionShape();
Node node = (Node) this.spatial; Vector3f newScale = newShape.getScale();
if (myShape != newShape || !oldScale.equals(newScale)) {
myShape = newShape;
oldScale.set(newScale);
Node node = (Node) spatial;
node.detachChild(geom); node.detachChild(geom);
geom = DebugShapeFactory.getDebugShape(body.getCollisionShape());
geom = DebugShapeFactory.getDebugShape(myShape);
geom.setName(body.toString());
node.attachChild(geom); node.attachChild(geom);
} }
if(body.isActive()){ if(body.isActive()){
@ -125,8 +139,10 @@ public class BulletRigidBodyDebugControl extends AbstractPhysicsDebugControl {
}else{ }else{
geom.setMaterial(debugAppState.DEBUG_BLUE); 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);
} }
/** /**

@ -35,7 +35,6 @@ import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.collision.shapes.CompoundCollisionShape; import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
import com.jme3.bullet.collision.shapes.infos.ChildCollisionShape; import com.jme3.bullet.collision.shapes.infos.ChildCollisionShape;
import com.jme3.math.Matrix3f; import com.jme3.math.Matrix3f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry; import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh; import com.jme3.scene.Mesh;
import com.jme3.scene.Node; import com.jme3.scene.Node;
@ -46,6 +45,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
* A utility class to generate debug spatials from Bullet collision shapes.
* *
* @author CJ Hare, normenhansen * @author CJ Hare, normenhansen
*/ */
@ -128,14 +128,8 @@ public class DebugShapeFactory {
public static Mesh getDebugMesh(CollisionShape shape) { public static Mesh getDebugMesh(CollisionShape shape) {
Mesh mesh = new Mesh(); Mesh mesh = new Mesh();
DebugMeshCallback callback = new DebugMeshCallback(); DebugMeshCallback callback = new DebugMeshCallback();
/* long id = shape.getObjectId();
* Populate the mesh based on an unscaled shape; getVertices(id, callback);
* 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);
mesh.setBuffer(Type.Position, 3, callback.getVertices()); mesh.setBuffer(Type.Position, 3, callback.getVertices());
mesh.getFloatBuffer(Type.Position).clear(); mesh.getFloatBuffer(Type.Position).clear();

Loading…
Cancel
Save