From 5bfd3bd68cb98fa5c8cb285e900b5e3ab150a2e7 Mon Sep 17 00:00:00 2001 From: "nor..67" Date: Sat, 15 Oct 2011 14:27:28 +0000 Subject: [PATCH] - add collision callbacks to native bullet - add SixDofSpringJoint to native bullet (thanks to @chototsu) git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8395 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../bullet/com/jme3/bullet/PhysicsSpace.java | 4 + .../collision/PhysicsCollisionEvent.java | 168 ++++++------------ .../PhysicsCollisionEventFactory.java | 6 +- .../collision/PhysicsCollisionObject.java | 19 ++ .../com/jme3/bullet/joints/SixDofJoint.java | 18 +- .../jme3/bullet/joints/SixDofSpringJoint.java | 103 +++++++++++ .../jme3/bullet/objects/PhysicsCharacter.java | 1 + .../bullet/objects/PhysicsGhostObject.java | 35 ++-- .../jme3/bullet/objects/PhysicsRigidBody.java | 1 + .../jme3/bullet/objects/PhysicsVehicle.java | 6 + engine/src/bullet/native/build.xml | 44 ++++- engine/src/bullet/native/bullet.properties | 2 +- .../native/com_jme3_bullet_PhysicsSpace.cpp | 20 ++- ...ullet_collision_PhysicsCollisionObject.cpp | 67 +++++++ ..._bullet_collision_PhysicsCollisionObject.h | 24 +++ ...m_jme3_bullet_joints_SixDofSpringJoint.cpp | 103 +++++++++++ ...com_jme3_bullet_joints_SixDofSpringJoint.h | 61 +++++++ ...m_jme3_bullet_objects_PhysicsCharacter.cpp | 2 +- ...jme3_bullet_objects_PhysicsGhostObject.cpp | 46 ++++- ...m_jme3_bullet_objects_PhysicsGhostObject.h | 8 + ...m_jme3_bullet_objects_PhysicsRigidBody.cpp | 1 + engine/src/bullet/native/jmeClasses.cpp | 16 ++ engine/src/bullet/native/jmeClasses.h | 3 + engine/src/bullet/native/jmeMotionState.cpp | 1 + engine/src/bullet/native/jmePhysicsSpace.cpp | 43 ++++- engine/src/bullet/native/jmePhysicsSpace.h | 3 + engine/src/bullet/native/jmeUserPointer.h | 11 ++ 27 files changed, 670 insertions(+), 146 deletions(-) create mode 100644 engine/src/bullet/com/jme3/bullet/joints/SixDofSpringJoint.java create mode 100644 engine/src/bullet/native/com_jme3_bullet_joints_SixDofSpringJoint.cpp create mode 100644 engine/src/bullet/native/com_jme3_bullet_joints_SixDofSpringJoint.h create mode 100644 engine/src/bullet/native/jmeUserPointer.h diff --git a/engine/src/bullet/com/jme3/bullet/PhysicsSpace.java b/engine/src/bullet/com/jme3/bullet/PhysicsSpace.java index 35bd447a0..d2f02e7e2 100644 --- a/engine/src/bullet/com/jme3/bullet/PhysicsSpace.java +++ b/engine/src/bullet/com/jme3/bullet/PhysicsSpace.java @@ -329,6 +329,10 @@ public class PhysicsSpace { // } // }); // } + private void addCollisionEvent_native(PhysicsCollisionObject node, PhysicsCollisionObject node1, long manifoldPointObjectId) { +// System.out.println("addCollisionEvent:"+node.getObjectId()+" "+ node1.getObjectId()); + collisionEvents.add(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, manifoldPointObjectId)); + } /** * updates the physics space * @param time the current time value diff --git a/engine/src/bullet/com/jme3/bullet/collision/PhysicsCollisionEvent.java b/engine/src/bullet/com/jme3/bullet/collision/PhysicsCollisionEvent.java index e79e315fe..a9f63d63c 100644 --- a/engine/src/bullet/com/jme3/bullet/collision/PhysicsCollisionEvent.java +++ b/engine/src/bullet/com/jme3/bullet/collision/PhysicsCollisionEvent.java @@ -49,58 +49,13 @@ public class PhysicsCollisionEvent extends EventObject { private int type; private PhysicsCollisionObject nodeA; private PhysicsCollisionObject nodeB; - public final Vector3f localPointA; - public final Vector3f localPointB; - public final Vector3f positionWorldOnB; - public final Vector3f positionWorldOnA; - public final Vector3f normalWorldOnB; - public float distance1; - public float combinedFriction; - public float combinedRestitution; - public int partId0; - public int partId1; - public int index0; - public int index1; - public Object userPersistentData; - public float appliedImpulse; - public boolean lateralFrictionInitialized; - public float appliedImpulseLateral1; - public float appliedImpulseLateral2; - public int lifeTime; - public final Vector3f lateralFrictionDir1; - public final Vector3f lateralFrictionDir2; + private long manifoldPointObjectId = 0; - public PhysicsCollisionEvent(int type, PhysicsCollisionObject nodeA, PhysicsCollisionObject nodeB) { - this(type, nodeA, nodeB, new Vector3f(), new Vector3f(), new Vector3f(), new Vector3f(), new Vector3f(), 0, 0, 0, 0, 0, 0, 0, null, 0, false, 0, 0, 0, new Vector3f(), new Vector3f()); - } - - public PhysicsCollisionEvent(int type, PhysicsCollisionObject nodeA, PhysicsCollisionObject nodeB, Vector3f localPointA, Vector3f localPointB, Vector3f positionWorldOnB, Vector3f positionWorldOnA, Vector3f normalWorldOnB, float distance1, float combinedFriction, float combinedRestitution, int partId0, int partId1, int index0, int index1, Object userPersistentData, float appliedImpulse, boolean lateralFrictionInitialized, float appliedImpulseLateral1, float appliedImpulseLateral2, int lifeTime, Vector3f lateralFrictionDir1, Vector3f lateralFrictionDir2) { + public PhysicsCollisionEvent(int type, PhysicsCollisionObject nodeA, PhysicsCollisionObject nodeB, long manifoldPointObjectId) { super(nodeA); - this.type = type; - this.nodeA = nodeA; - this.nodeB = nodeB; - this.localPointA = localPointA; - this.localPointB = localPointB; - this.positionWorldOnB = positionWorldOnB; - this.positionWorldOnA = positionWorldOnA; - this.normalWorldOnB = normalWorldOnB; - this.distance1 = distance1; - this.combinedFriction = combinedFriction; - this.combinedRestitution = combinedRestitution; - this.partId0 = partId0; - this.partId1 = partId1; - this.index0 = index0; - this.index1 = index1; - this.userPersistentData = userPersistentData; - this.appliedImpulse = appliedImpulse; - this.lateralFrictionInitialized = lateralFrictionInitialized; - this.appliedImpulseLateral1 = appliedImpulseLateral1; - this.appliedImpulseLateral2 = appliedImpulseLateral2; - this.lifeTime = lifeTime; - this.lateralFrictionDir1 = lateralFrictionDir1; - this.lateralFrictionDir2 = lateralFrictionDir2; + this.manifoldPointObjectId = manifoldPointObjectId; } - + /** * used by event factory, called when event is destroyed */ @@ -109,56 +64,18 @@ public class PhysicsCollisionEvent extends EventObject { this.type = 0; this.nodeA = null; this.nodeB = null; -// this.localPointA = null; -// this.localPointB = null; -// this.positionWorldOnB = null; -// this.positionWorldOnA = null; -// this.normalWorldOnB = null; -// this.distance1 = null -// this.combinedFriction = null; -// this.combinedRestitution = null; -// this.partId0 = null; -// this.partId1 = null; -// this.index0 = null; -// this.index1 = null; - this.userPersistentData = null; -// this.appliedImpulse = null; -// this.lateralFrictionInitialized = null; -// this.appliedImpulseLateral1 = null; -// this.appliedImpulseLateral2 = null; -// this.lifeTime = null; -// this.lateralFrictionDir1 = null; -// this.lateralFrictionDir2 = null; + this.manifoldPointObjectId = 0; } /** * used by event factory, called when event reused */ - public void refactor(int type, PhysicsCollisionObject source, PhysicsCollisionObject nodeB, Vector3f localPointA, Vector3f localPointB, Vector3f positionWorldOnB, Vector3f positionWorldOnA, Vector3f normalWorldOnB, float distance1, float combinedFriction, float combinedRestitution, int partId0, int partId1, int index0, int index1, Object userPersistentData, float appliedImpulse, boolean lateralFrictionInitialized, float appliedImpulseLateral1, float appliedImpulseLateral2, int lifeTime, Vector3f lateralFrictionDir1, Vector3f lateralFrictionDir2) { + public void refactor(int type, PhysicsCollisionObject source, PhysicsCollisionObject nodeB, long manifoldPointObjectId) { this.source = source; this.type = type; this.nodeA = source; this.nodeB = nodeB; - this.localPointA.set(localPointA); - this.localPointB.set(localPointB); - this.positionWorldOnB.set(positionWorldOnB); - this.positionWorldOnA.set(positionWorldOnA); - this.normalWorldOnB.set(normalWorldOnB); - this.distance1 = distance1; - this.combinedFriction = combinedFriction; - this.combinedRestitution = combinedRestitution; - this.partId0 = partId0; - this.partId1 = partId1; - this.index0 = index0; - this.index1 = index1; - this.userPersistentData = userPersistentData; - this.appliedImpulse = appliedImpulse; - this.lateralFrictionInitialized = lateralFrictionInitialized; - this.appliedImpulseLateral1 = appliedImpulseLateral1; - this.appliedImpulseLateral2 = appliedImpulseLateral2; - this.lifeTime = lifeTime; - this.lateralFrictionDir1.set(lateralFrictionDir1); - this.lateralFrictionDir2.set(lateralFrictionDir2); + this.manifoldPointObjectId = manifoldPointObjectId; } public int getType() { @@ -194,82 +111,109 @@ public class PhysicsCollisionEvent extends EventObject { } public float getAppliedImpulse() { - return appliedImpulse; + return getAppliedImpulse(manifoldPointObjectId); } + private native float getAppliedImpulse(long manifoldPointObjectId); public float getAppliedImpulseLateral1() { - return appliedImpulseLateral1; + return getAppliedImpulseLateral1(manifoldPointObjectId); } + private native float getAppliedImpulseLateral1(long manifoldPointObjectId); public float getAppliedImpulseLateral2() { - return appliedImpulseLateral2; + return getAppliedImpulseLateral2(manifoldPointObjectId); } + private native float getAppliedImpulseLateral2(long manifoldPointObjectId); public float getCombinedFriction() { - return combinedFriction; + return getCombinedFriction(manifoldPointObjectId); } + private native float getCombinedFriction(long manifoldPointObjectId); public float getCombinedRestitution() { - return combinedRestitution; + return getCombinedRestitution(manifoldPointObjectId); } + private native float getCombinedRestitution(long manifoldPointObjectId); public float getDistance1() { - return distance1; + return getDistance1(manifoldPointObjectId); } + private native float getDistance1(long manifoldPointObjectId); public int getIndex0() { - return index0; + return getIndex0(manifoldPointObjectId); } + private native int getIndex0(long manifoldPointObjectId); public int getIndex1() { - return index1; + return getIndex1(manifoldPointObjectId); } + private native int getIndex1(long manifoldPointObjectId); - public Vector3f getLateralFrictionDir1() { + public Vector3f getLateralFrictionDir1(Vector3f lateralFrictionDir1) { + getLateralFrictionDir1(manifoldPointObjectId, lateralFrictionDir1); return lateralFrictionDir1; } + private native void getLateralFrictionDir1(long manifoldPointObjectId, Vector3f lateralFrictionDir1); - public Vector3f getLateralFrictionDir2() { + public Vector3f getLateralFrictionDir2(Vector3f lateralFrictionDir2) { + getLateralFrictionDir2(manifoldPointObjectId, lateralFrictionDir2); return lateralFrictionDir2; } + private native void getLateralFrictionDir2(long manifoldPointObjectId, Vector3f lateralFrictionDir2); public boolean isLateralFrictionInitialized() { - return lateralFrictionInitialized; + return isLateralFrictionInitialized(manifoldPointObjectId); } + private native boolean isLateralFrictionInitialized(long manifoldPointObjectId); public int getLifeTime() { - return lifeTime; + return getLifeTime(manifoldPointObjectId); } + private native int getLifeTime(long manifoldPointObjectId); - public Vector3f getLocalPointA() { + public Vector3f getLocalPointA(Vector3f localPointA) { + getLocalPointA(manifoldPointObjectId, localPointA); return localPointA; } + private native void getLocalPointA(long manifoldPointObjectId, Vector3f localPointA); - public Vector3f getLocalPointB() { + public Vector3f getLocalPointB(Vector3f localPointB) { + getLocalPointB(manifoldPointObjectId, localPointB); return localPointB; } + private native void getLocalPointB(long manifoldPointObjectId, Vector3f localPointB); - public Vector3f getNormalWorldOnB() { + public Vector3f getNormalWorldOnB(Vector3f normalWorldOnB) { + getNormalWorldOnB(manifoldPointObjectId, normalWorldOnB); return normalWorldOnB; } + private native void getNormalWorldOnB(long manifoldPointObjectId, Vector3f normalWorldOnB); public int getPartId0() { - return partId0; + return getPartId0(manifoldPointObjectId); } + private native int getPartId0(long manifoldPointObjectId); public int getPartId1() { - return partId1; + return getPartId1(manifoldPointObjectId); } - public Vector3f getPositionWorldOnA() { + private native int getPartId1(long manifoldPointObjectId); + + public Vector3f getPositionWorldOnA(Vector3f positionWorldOnA) { + getPositionWorldOnA(positionWorldOnA); return positionWorldOnA; } + private native void getPositionWorldOnA(long manifoldPointObjectId, Vector3f positionWorldOnA); - public Vector3f getPositionWorldOnB() { + public Vector3f getPositionWorldOnB(Vector3f positionWorldOnB) { + getPositionWorldOnB(manifoldPointObjectId, positionWorldOnB); return positionWorldOnB; } + private native void getPositionWorldOnB(long manifoldPointObjectId, Vector3f positionWorldOnB); - public Object getUserPersistentData() { - return userPersistentData; - } +// public Object getUserPersistentData() { +// return userPersistentData; +// } } diff --git a/engine/src/bullet/com/jme3/bullet/collision/PhysicsCollisionEventFactory.java b/engine/src/bullet/com/jme3/bullet/collision/PhysicsCollisionEventFactory.java index 51d4fe4ca..4d88fc071 100644 --- a/engine/src/bullet/com/jme3/bullet/collision/PhysicsCollisionEventFactory.java +++ b/engine/src/bullet/com/jme3/bullet/collision/PhysicsCollisionEventFactory.java @@ -41,12 +41,12 @@ public class PhysicsCollisionEventFactory { private ConcurrentLinkedQueue eventBuffer = new ConcurrentLinkedQueue(); - public PhysicsCollisionEvent getEvent(int type, PhysicsCollisionObject source, PhysicsCollisionObject nodeB) { + public PhysicsCollisionEvent getEvent(int type, PhysicsCollisionObject source, PhysicsCollisionObject nodeB, long manifoldPointObjectId) { PhysicsCollisionEvent event = eventBuffer.poll(); if (event == null) { - event = new PhysicsCollisionEvent(type, source, nodeB); + event = new PhysicsCollisionEvent(type, source, nodeB, manifoldPointObjectId); }else{ -// event.refactor(type, source, nodeB); + event.refactor(type, source, nodeB, manifoldPointObjectId); } return event; } diff --git a/engine/src/bullet/com/jme3/bullet/collision/PhysicsCollisionObject.java b/engine/src/bullet/com/jme3/bullet/collision/PhysicsCollisionObject.java index 74559397f..3ccd09802 100644 --- a/engine/src/bullet/com/jme3/bullet/collision/PhysicsCollisionObject.java +++ b/engine/src/bullet/com/jme3/bullet/collision/PhysicsCollisionObject.java @@ -125,6 +125,9 @@ public abstract class PhysicsCollisionObject implements Savable { */ public void setCollisionGroup(int collisionGroup) { this.collisionGroup = collisionGroup; + if (objectId != 0) { + setCollisionGroup(objectId, collisionGroup); + } } /** @@ -135,6 +138,9 @@ public abstract class PhysicsCollisionObject implements Savable { */ public void addCollideWithGroup(int collisionGroup) { this.collisionGroupsMask = this.collisionGroupsMask | collisionGroup; + if (objectId != 0) { + setCollideWithGroups(objectId, this.collisionGroupsMask); + } } /** @@ -143,6 +149,9 @@ public abstract class PhysicsCollisionObject implements Savable { */ public void removeCollideWithGroup(int collisionGroup) { this.collisionGroupsMask = this.collisionGroupsMask & ~collisionGroup; + if (objectId != 0) { + setCollideWithGroups(this.collisionGroupsMask); + } } /** @@ -151,6 +160,9 @@ public abstract class PhysicsCollisionObject implements Savable { */ public void setCollideWithGroups(int collisionGroups) { this.collisionGroupsMask = collisionGroups; + if (objectId != 0) { + setCollideWithGroups(objectId, this.collisionGroupsMask); + } } /** @@ -161,6 +173,11 @@ public abstract class PhysicsCollisionObject implements Savable { return collisionGroupsMask; } + protected void initUserPointer() { + Logger.getLogger(this.getClass().getName()).log(Level.INFO, "initUserPointer() objectId = {0}", Long.toHexString(objectId)); + initUserPointer(objectId, collisionGroup, collisionGroupsMask); + } + native void initUserPointer(long objectId, int group, int groups); /** * Creates a visual debug shape of the current collision shape of this physics object
* Does not work with detached physics, please switch to PARALLEL or SEQUENTIAL for debugging @@ -278,6 +295,8 @@ public abstract class PhysicsCollisionObject implements Savable { } protected native void attachCollisionShape(long objectId, long collisionShapeId); + native void setCollisionGroup(long objectId, int collisionGroup); + native void setCollideWithGroups(long objectId, int collisionGroups); @Override public void write(JmeExporter e) throws IOException { diff --git a/engine/src/bullet/com/jme3/bullet/joints/SixDofJoint.java b/engine/src/bullet/com/jme3/bullet/joints/SixDofJoint.java index 6bed78f25..05d75df7a 100644 --- a/engine/src/bullet/com/jme3/bullet/joints/SixDofJoint.java +++ b/engine/src/bullet/com/jme3/bullet/joints/SixDofJoint.java @@ -59,14 +59,14 @@ import java.util.logging.Logger; */ public class SixDofJoint extends PhysicsJoint { - private Matrix3f rotA, rotB; - private boolean useLinearReferenceFrameA; - private LinkedList rotationalMotors = new LinkedList(); - private TranslationalLimitMotor translationalMotor; - private Vector3f angularUpperLimit = new Vector3f(Vector3f.POSITIVE_INFINITY); - private Vector3f angularLowerLimit = new Vector3f(Vector3f.NEGATIVE_INFINITY); - private Vector3f linearUpperLimit = new Vector3f(Vector3f.POSITIVE_INFINITY); - private Vector3f linearLowerLimit = new Vector3f(Vector3f.NEGATIVE_INFINITY); + Matrix3f rotA, rotB; + boolean useLinearReferenceFrameA; + LinkedList rotationalMotors = new LinkedList(); + TranslationalLimitMotor translationalMotor; + Vector3f angularUpperLimit = new Vector3f(Vector3f.POSITIVE_INFINITY); + Vector3f angularLowerLimit = new Vector3f(Vector3f.NEGATIVE_INFINITY); + Vector3f linearUpperLimit = new Vector3f(Vector3f.POSITIVE_INFINITY); + Vector3f linearLowerLimit = new Vector3f(Vector3f.NEGATIVE_INFINITY); public SixDofJoint() { } @@ -160,7 +160,7 @@ public class SixDofJoint extends PhysicsJoint { private native void setAngularLowerLimit(long objctId, Vector3f vector); - private native long createJoint(long objectIdA, long objectIdB, Vector3f pivotA, Matrix3f rotA, Vector3f pivotB, Matrix3f rotB, boolean useLinearReferenceFrameA); + native long createJoint(long objectIdA, long objectIdB, Vector3f pivotA, Matrix3f rotA, Vector3f pivotB, Matrix3f rotB, boolean useLinearReferenceFrameA); @Override public void read(JmeImporter im) throws IOException { diff --git a/engine/src/bullet/com/jme3/bullet/joints/SixDofSpringJoint.java b/engine/src/bullet/com/jme3/bullet/joints/SixDofSpringJoint.java new file mode 100644 index 000000000..1fa312fa1 --- /dev/null +++ b/engine/src/bullet/com/jme3/bullet/joints/SixDofSpringJoint.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2009-2010 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jme3.bullet.joints; + +import com.jme3.export.JmeExporter; +import com.jme3.export.JmeImporter; +import com.jme3.math.Matrix3f; +import com.jme3.math.Vector3f; +import com.jme3.bullet.joints.motors.RotationalLimitMotor; +import com.jme3.bullet.joints.motors.TranslationalLimitMotor; +import com.jme3.bullet.objects.PhysicsRigidBody; +import com.jme3.export.InputCapsule; +import com.jme3.export.OutputCapsule; +import java.io.IOException; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * From bullet manual:
+ * This generic constraint can emulate a variety of standard constraints, + * by configuring each of the 6 degrees of freedom (dof). + * The first 3 dof axis are linear axis, which represent translation of rigidbodies, + * and the latter 3 dof axis represent the angular motion. Each axis can be either locked, + * free or limited. On construction of a new btGeneric6DofConstraint, all axis are locked. + * Afterwards the axis can be reconfigured. Note that several combinations that + * include free and/or limited angular degrees of freedom are undefined. + * @author normenhansen + */ +public class SixDofSpringJoint extends SixDofJoint { + + final boolean springEnabled[] = new boolean[6]; + final float equilibriumPoint[] = new float[6]; + final float springStiffness[] = new float[6]; + final float springDamping[] = new float[6]; // between 0 and 1 (1 == no damping) + + public SixDofSpringJoint() { + } + + /** + * @param pivotA local translation of the joint connection point in node A + * @param pivotB local translation of the joint connection point in node B + */ + public SixDofSpringJoint(PhysicsRigidBody nodeA, PhysicsRigidBody nodeB, Vector3f pivotA, Vector3f pivotB, Matrix3f rotA, Matrix3f rotB, boolean useLinearReferenceFrameA) { + super(nodeA, nodeB, pivotA, pivotB, rotA, rotB, useLinearReferenceFrameA); + } + public void enableSpring(int index, boolean onOff) { + enableSpring(objectId, index, onOff); + } + native void enableSpring(long objctId, int index, boolean onOff); + + public void setStiffness(int index, float stiffness) { + setStiffness(objectId, index, stiffness); + } + native void setStiffness(long objctId, int index, float stiffness); + + public void setDamping(int index, float damping) { + setDamping(objectId, index, damping); + + } + native void setDamping(long objctId, int index, float damping); + public void setEquilibriumPoint() { // set the current constraint position/orientation as an equilibrium point for all DOF + setEquilibriumPoint(objectId); + } + native void setEquilibriumPoint(long objctId); + public void setEquilibriumPoint(int index){ // set the current constraint position/orientation as an equilibrium point for given DOF + setEquilibriumPoint(objectId, index); + } + native void setEquilibriumPoint(long objctId, int index); + @Override + native long createJoint(long objectIdA, long objectIdB, Vector3f pivotA, Matrix3f rotA, Vector3f pivotB, Matrix3f rotB, boolean useLinearReferenceFrameA); + +} diff --git a/engine/src/bullet/com/jme3/bullet/objects/PhysicsCharacter.java b/engine/src/bullet/com/jme3/bullet/objects/PhysicsCharacter.java index d9ee34d0d..430ed9944 100644 --- a/engine/src/bullet/com/jme3/bullet/objects/PhysicsCharacter.java +++ b/engine/src/bullet/com/jme3/bullet/objects/PhysicsCharacter.java @@ -79,6 +79,7 @@ public class PhysicsCharacter extends PhysicsCollisionObject { if (objectId == 0) { objectId = createGhostObject(); Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Creating GhostObject {0}", Long.toHexString(objectId)); + initUserPointer(); } setCharacterFlags(objectId); attachCollisionShape(objectId, collisionShape.getObjectId()); diff --git a/engine/src/bullet/com/jme3/bullet/objects/PhysicsGhostObject.java b/engine/src/bullet/com/jme3/bullet/objects/PhysicsGhostObject.java index a037b00bd..d644f7063 100644 --- a/engine/src/bullet/com/jme3/bullet/objects/PhysicsGhostObject.java +++ b/engine/src/bullet/com/jme3/bullet/objects/PhysicsGhostObject.java @@ -80,6 +80,7 @@ public class PhysicsGhostObject extends PhysicsCollisionObject { objectId = createGhostObject(); Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Ghost Object {0}", Long.toHexString(objectId)); setGhostFlags(objectId); + initUserPointer(); } // if (gObject == null) { // gObject = new PairCachingGhostObject(); @@ -91,13 +92,13 @@ public class PhysicsGhostObject extends PhysicsCollisionObject { private native long createGhostObject(); private native void setGhostFlags(long objectId); - + @Override public void setCollisionShape(CollisionShape collisionShape) { super.setCollisionShape(collisionShape); if (objectId == 0) { buildObject(); - }else{ + } else { attachCollisionShape(objectId, collisionShape.getObjectId()); } } @@ -109,7 +110,7 @@ public class PhysicsGhostObject extends PhysicsCollisionObject { public void setPhysicsLocation(Vector3f location) { setPhysicsLocation(objectId, location); } - + private native void setPhysicsLocation(long objectId, Vector3f location); /** @@ -119,7 +120,7 @@ public class PhysicsGhostObject extends PhysicsCollisionObject { public void setPhysicsRotation(Matrix3f rotation) { setPhysicsRotation(objectId, rotation); } - + private native void setPhysicsRotation(long objectId, Matrix3f rotation); /** @@ -129,7 +130,7 @@ public class PhysicsGhostObject extends PhysicsCollisionObject { public void setPhysicsRotation(Quaternion rotation) { setPhysicsRotation(objectId, rotation); } - + private native void setPhysicsRotation(long objectId, Quaternion rotation); /** @@ -142,7 +143,7 @@ public class PhysicsGhostObject extends PhysicsCollisionObject { getPhysicsLocation(objectId, trans); return trans; } - + private native void getPhysicsLocation(long objectId, Vector3f vector); /** @@ -155,7 +156,7 @@ public class PhysicsGhostObject extends PhysicsCollisionObject { getPhysicsRotation(objectId, rot); return rot; } - + private native void getPhysicsRotation(long objectId, Quaternion rot); /** @@ -168,7 +169,7 @@ public class PhysicsGhostObject extends PhysicsCollisionObject { getPhysicsRotationMatrix(objectId, rot); return rot; } - + private native void getPhysicsRotationMatrix(long objectId, Matrix3f rot); /** @@ -179,7 +180,7 @@ public class PhysicsGhostObject extends PhysicsCollisionObject { getPhysicsLocation(objectId, vec); return vec; } - + /** * @return the physicsLocation */ @@ -201,7 +202,6 @@ public class PhysicsGhostObject extends PhysicsCollisionObject { // public PairCachingGhostObject getObjectId() { // return gObject; // } - /** * destroys this PhysicsGhostNode and removes it from memory */ @@ -215,13 +215,20 @@ public class PhysicsGhostObject extends PhysicsCollisionObject { * @return All CollisionObjects overlapping with this GhostNode. */ public List getOverlappingObjects() { -// overlappingObjects.clear(); + overlappingObjects.clear(); + getOverlappingObjects(objectId); // for (com.bulletphysics.collision.dispatch.CollisionObject collObj : gObject.getOverlappingPairs()) { // overlappingObjects.add((PhysicsCollisionObject) collObj.getUserPointer()); // } return overlappingObjects; } + protected native void getOverlappingObjects(long objectId); + + private void addOverlappingObject_native(PhysicsCollisionObject co) { + overlappingObjects.add(co); + } + /** * * @return With how many other CollisionObjects this GhostNode is currently overlapping. @@ -229,7 +236,7 @@ public class PhysicsGhostObject extends PhysicsCollisionObject { public int getOverlappingCount() { return getOverlappingCount(objectId); } - + private native int getOverlappingCount(long objectId); /** @@ -244,7 +251,7 @@ public class PhysicsGhostObject extends PhysicsCollisionObject { public void setCcdSweptSphereRadius(float radius) { setCcdSweptSphereRadius(objectId, radius); } - + private native void setCcdSweptSphereRadius(long objectId, float radius); public void setCcdMotionThreshold(float threshold) { @@ -256,7 +263,7 @@ public class PhysicsGhostObject extends PhysicsCollisionObject { public float getCcdSweptSphereRadius() { return getCcdSweptSphereRadius(objectId); } - + private native float getCcdSweptSphereRadius(long objectId); public float getCcdMotionThreshold() { diff --git a/engine/src/bullet/com/jme3/bullet/objects/PhysicsRigidBody.java b/engine/src/bullet/com/jme3/bullet/objects/PhysicsRigidBody.java index eac3f3e7f..21e9bfc79 100644 --- a/engine/src/bullet/com/jme3/bullet/objects/PhysicsRigidBody.java +++ b/engine/src/bullet/com/jme3/bullet/objects/PhysicsRigidBody.java @@ -121,6 +121,7 @@ public class PhysicsRigidBody extends PhysicsCollisionObject { } else { setStatic(objectId, false); } + initUserPointer(); } /** diff --git a/engine/src/bullet/com/jme3/bullet/objects/PhysicsVehicle.java b/engine/src/bullet/com/jme3/bullet/objects/PhysicsVehicle.java index 6896769c7..c0ae3233a 100644 --- a/engine/src/bullet/com/jme3/bullet/objects/PhysicsVehicle.java +++ b/engine/src/bullet/com/jme3/bullet/objects/PhysicsVehicle.java @@ -149,6 +149,12 @@ public class PhysicsVehicle extends PhysicsRigidBody { setCoordinateSystem(vehicleId, 0, 1, 2); for (VehicleWheel wheel : wheels) { wheel.setWheelId(addWheel(vehicleId, wheel.getLocation(), wheel.getDirection(), wheel.getAxle(), wheel.getRestLength(), wheel.getRadius(), tuning, wheel.isFrontWheel())); + wheel.setFrictionSlip(tuning.frictionSlip); + wheel.setMaxSuspensionTravelCm(tuning.maxSuspensionTravelCm); + wheel.setSuspensionStiffness(tuning.suspensionStiffness); + wheel.setWheelsDampingCompression(tuning.suspensionCompression); + wheel.setWheelsDampingRelaxation(tuning.suspensionDamping); + wheel.setMaxSuspensionForce(tuning.maxSuspensionForce); } } diff --git a/engine/src/bullet/native/build.xml b/engine/src/bullet/native/build.xml index 9d4663863..7c3fbd07c 100644 --- a/engine/src/bullet/native/build.xml +++ b/engine/src/bullet/native/build.xml @@ -5,6 +5,9 @@ + + + @@ -22,6 +25,9 @@ + + + @@ -41,7 +47,7 @@ - + @@ -51,6 +57,7 @@ + @@ -78,6 +85,7 @@ + @@ -172,7 +180,10 @@ + + + @@ -181,6 +192,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/engine/src/bullet/native/bullet.properties b/engine/src/bullet/native/bullet.properties index ca8c444b0..0c6a24660 100644 --- a/engine/src/bullet/native/bullet.properties +++ b/engine/src/bullet/native/bullet.properties @@ -16,7 +16,7 @@ bullet.osx.syslibroot=/Developer/SDKs/MacOSX10.5.sdk # change this to msvc for MS Visual Studio compiler bullet.windows.compiler=g++ bullet.linux.compiler=g++ - +bullet.solaris.compiler=g++ # native header include directories bullet.java.include=${java.home}/../include # OSX has no JRE, only JDK diff --git a/engine/src/bullet/native/com_jme3_bullet_PhysicsSpace.cpp b/engine/src/bullet/native/com_jme3_bullet_PhysicsSpace.cpp index 9e8b1cee0..1801e8afb 100644 --- a/engine/src/bullet/native/com_jme3_bullet_PhysicsSpace.cpp +++ b/engine/src/bullet/native/com_jme3_bullet_PhysicsSpace.cpp @@ -32,7 +32,7 @@ #include "com_jme3_bullet_PhysicsSpace.h" #include "jmePhysicsSpace.h" #include "jmeBulletUtil.h" - +#include "jmeUserPointer.h" /** * Author: Normen Hansen */ @@ -93,6 +93,9 @@ extern "C" { env->ThrowNew(newExc, "The collision object does not exist."); return; } + jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer(); + userPointer -> space = space; + space->getDynamicsWorld()->addCollisionObject(collisionObject); } @@ -116,6 +119,8 @@ extern "C" { return; } space->getDynamicsWorld()->removeCollisionObject(collisionObject); + jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer(); + userPointer -> space = NULL; } /* @@ -137,6 +142,8 @@ extern "C" { env->ThrowNew(newExc, "The collision object does not exist."); return; } + jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer(); + userPointer -> space = space; space->getDynamicsWorld()->addRigidBody(collisionObject); } @@ -159,6 +166,8 @@ extern "C" { env->ThrowNew(newExc, "The collision object does not exist."); return; } + jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer(); + userPointer -> space = NULL; space->getDynamicsWorld()->removeRigidBody(collisionObject); } @@ -181,7 +190,12 @@ extern "C" { env->ThrowNew(newExc, "The collision object does not exist."); return; } - space->getDynamicsWorld()->addCollisionObject(collisionObject, btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter | btBroadphaseProxy::DefaultFilter); + jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer(); + userPointer -> space = space; + space->getDynamicsWorld()->addCollisionObject(collisionObject, + btBroadphaseProxy::CharacterFilter, + btBroadphaseProxy::StaticFilter | btBroadphaseProxy::DefaultFilter + ); } /* @@ -203,6 +217,8 @@ extern "C" { env->ThrowNew(newExc, "The collision object does not exist."); return; } + jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer(); + userPointer -> space = NULL; space->getDynamicsWorld()->removeCollisionObject(collisionObject); } diff --git a/engine/src/bullet/native/com_jme3_bullet_collision_PhysicsCollisionObject.cpp b/engine/src/bullet/native/com_jme3_bullet_collision_PhysicsCollisionObject.cpp index 38eeb67e0..515041ace 100644 --- a/engine/src/bullet/native/com_jme3_bullet_collision_PhysicsCollisionObject.cpp +++ b/engine/src/bullet/native/com_jme3_bullet_collision_PhysicsCollisionObject.cpp @@ -35,6 +35,8 @@ */ #include "com_jme3_bullet_collision_PhysicsCollisionObject.h" #include "jmeBulletUtil.h" +#include "jmePhysicsSpace.h" +#include "jmeUserPointer.h" #ifdef __cplusplus extern "C" { @@ -75,8 +77,73 @@ extern "C" { env->ThrowNew(newExc, "The native object does not exist."); return; } + if (collisionObject -> getUserPointer() != NULL){ + jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer(); + delete(userPointer); + } delete(collisionObject); } + /* + * Class: com_jme3_bullet_collision_PhysicsCollisionObject + * Method: initUserPointer + * Signature: (JII)V + */ + JNIEXPORT void JNICALL Java_com_jme3_bullet_collision_PhysicsCollisionObject_initUserPointer + (JNIEnv *env, jobject object, jlong objectId, jint group, jint groups) { + btCollisionObject* collisionObject = (btCollisionObject*) objectId; + if (collisionObject == NULL) { + jclass newExc = env->FindClass("java/lang/NullPointerException"); + env->ThrowNew(newExc, "The native object does not exist."); + return; + } + jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer(); + if (userPointer != NULL) { +// delete(userPointer); + } + userPointer = new jmeUserPointer(); + userPointer -> javaCollisionObject = env->NewWeakGlobalRef(object); + userPointer -> group = group; + userPointer -> groups = groups; + userPointer -> space = NULL; + collisionObject -> setUserPointer(userPointer); + } + /* + * Class: com_jme3_bullet_collision_PhysicsCollisionObject + * Method: setCollisionGroup + * Signature: (JI)V + */ + JNIEXPORT void JNICALL Java_com_jme3_bullet_collision_PhysicsCollisionObject_setCollisionGroup + (JNIEnv *env, jobject object, jlong objectId, jint group) { + btCollisionObject* collisionObject = (btCollisionObject*) objectId; + if (collisionObject == NULL) { + jclass newExc = env->FindClass("java/lang/NullPointerException"); + env->ThrowNew(newExc, "The native object does not exist."); + return; + } + jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer(); + if (userPointer != NULL){ + userPointer -> group = group; + } + } + /* + * Class: com_jme3_bullet_collision_PhysicsCollisionObject + * Method: setCollideWithGroups + * Signature: (JI)V + */ + JNIEXPORT void JNICALL Java_com_jme3_bullet_collision_PhysicsCollisionObject_setCollideWithGroups + (JNIEnv *env, jobject object, jlong objectId, jint groups) { + btCollisionObject* collisionObject = (btCollisionObject*) objectId; + if (collisionObject == NULL) { + jclass newExc = env->FindClass("java/lang/NullPointerException"); + env->ThrowNew(newExc, "The native object does not exist."); + return; + } + jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer(); + if (userPointer != NULL){ + userPointer -> groups = groups; + } + } + #ifdef __cplusplus } #endif diff --git a/engine/src/bullet/native/com_jme3_bullet_collision_PhysicsCollisionObject.h b/engine/src/bullet/native/com_jme3_bullet_collision_PhysicsCollisionObject.h index 955c7e31c..db5bf7aaf 100644 --- a/engine/src/bullet/native/com_jme3_bullet_collision_PhysicsCollisionObject.h +++ b/engine/src/bullet/native/com_jme3_bullet_collision_PhysicsCollisionObject.h @@ -41,6 +41,14 @@ extern "C" { #define com_jme3_bullet_collision_PhysicsCollisionObject_COLLISION_GROUP_15 16384L #undef com_jme3_bullet_collision_PhysicsCollisionObject_COLLISION_GROUP_16 #define com_jme3_bullet_collision_PhysicsCollisionObject_COLLISION_GROUP_16 32768L +/* + * Class: com_jme3_bullet_collision_PhysicsCollisionObject + * Method: initUserPointer + * Signature: (JII)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_collision_PhysicsCollisionObject_initUserPointer + (JNIEnv *, jobject, jlong, jint, jint); + /* * Class: com_jme3_bullet_collision_PhysicsCollisionObject * Method: attachCollisionShape @@ -49,6 +57,22 @@ extern "C" { JNIEXPORT void JNICALL Java_com_jme3_bullet_collision_PhysicsCollisionObject_attachCollisionShape (JNIEnv *, jobject, jlong, jlong); +/* + * Class: com_jme3_bullet_collision_PhysicsCollisionObject + * Method: setCollisionGroup + * Signature: (JI)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_collision_PhysicsCollisionObject_setCollisionGroup + (JNIEnv *, jobject, jlong, jint); + +/* + * Class: com_jme3_bullet_collision_PhysicsCollisionObject + * Method: setCollideWithGroups + * Signature: (JI)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_collision_PhysicsCollisionObject_setCollideWithGroups + (JNIEnv *, jobject, jlong, jint); + /* * Class: com_jme3_bullet_collision_PhysicsCollisionObject * Method: finalizeNative diff --git a/engine/src/bullet/native/com_jme3_bullet_joints_SixDofSpringJoint.cpp b/engine/src/bullet/native/com_jme3_bullet_joints_SixDofSpringJoint.cpp new file mode 100644 index 000000000..282b400f9 --- /dev/null +++ b/engine/src/bullet/native/com_jme3_bullet_joints_SixDofSpringJoint.cpp @@ -0,0 +1,103 @@ + +/** + * Author: Normen Hansen + */ +#include "com_jme3_bullet_joints_SixDofSpringJoint.h" +#include "jmeBulletUtil.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Class: com_jme3_bullet_joints_SixDofSpringJoint + * Method: enableString + * Signature: (JIZ)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_joints_SixDofSpringJoint_enableSpring + (JNIEnv *env, jobject object, jlong jointId, jint index, jboolean onOff) { + btGeneric6DofSpringConstraint* joint = (btGeneric6DofSpringConstraint*) jointId; + joint -> enableSpring(index, onOff); +} + + +/* + * Class: com_jme3_bullet_joints_SixDofSpringJoint + * Method: setStiffness + * Signature: (JIF)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_joints_SixDofSpringJoint_setStiffness + (JNIEnv *env, jobject object, jlong jointId, jint index, jfloat stiffness) { + btGeneric6DofSpringConstraint* joint = (btGeneric6DofSpringConstraint*) jointId; + joint -> setStiffness(index, stiffness); +} + +/* + * Class: com_jme3_bullet_joints_SixDofSpringJoint + * Method: setDamping + * Signature: (JIF)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_joints_SixDofSpringJoint_setDamping + (JNIEnv *env, jobject object, jlong jointId, jint index, jfloat damping) { + btGeneric6DofSpringConstraint* joint = (btGeneric6DofSpringConstraint*) jointId; + joint -> setDamping(index, damping); +} + +/* + * Class: com_jme3_bullet_joints_SixDofSpringJoint + * Method: setEquilibriumPoint + * Signature: (JIF)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_joints_SixDofSpringJoint_setEquilibriumPoint__J + (JNIEnv *env, jobject object, jlong jointId) { + btGeneric6DofSpringConstraint* joint = (btGeneric6DofSpringConstraint*) jointId; + joint -> setEquilibriumPoint(); +} + +/* + * Class: com_jme3_bullet_joints_SixDofSpringJoint + * Method: setEquilibriumPoint + * Signature: (JI)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_joints_SixDofSpringJoint_setEquilibriumPoint__JI + (JNIEnv *env, jobject object, jlong jointId, jint index) { + btGeneric6DofSpringConstraint* joint = (btGeneric6DofSpringConstraint*) jointId; + joint -> setEquilibriumPoint(index); +} + + + + +/* + * Class: com_jme3_bullet_joints_SixDofSpringJoint + * Method: createJoint + * Signature: (JJLcom/jme3/math/Vector3f;Lcom/jme3/math/Matrix3f;Lcom/jme3/math/Vector3f;Lcom/jme3/math/Matrix3f;Z)J + */ +JNIEXPORT jlong JNICALL Java_com_jme3_bullet_joints_SixDofSpringJoint_createJoint + (JNIEnv * env, jobject object, jlong bodyIdA, jlong bodyIdB, jobject pivotA, jobject rotA, jobject pivotB, jobject rotB, jboolean useLinearReferenceFrameA) { + jmeClasses::initJavaClasses(env); + btRigidBody* bodyA = (btRigidBody*) bodyIdA; + btRigidBody* bodyB = (btRigidBody*) bodyIdB; + btMatrix3x3* mtx1 = &btMatrix3x3(); + btMatrix3x3* mtx2 = &btMatrix3x3(); +/* btTransform* transA = &btTransform(*mtx1); + jmeBulletUtil::convert(env, pivotA, &transA->getOrigin()); + jmeBulletUtil::convert(env, rotA, &transA->getBasis()); + btTransform* transB = &btTransform(*mtx2); + jmeBulletUtil::convert(env, pivotB, &transB->getOrigin()); + jmeBulletUtil::convert(env, rotB, &transB->getBasis()); +*/ + btTransform transA; + jmeBulletUtil::convert(env, pivotA, &transA.getOrigin()); + jmeBulletUtil::convert(env, rotA, &transA.getBasis()); + btTransform transB; + jmeBulletUtil::convert(env, pivotB, &transB.getOrigin()); + jmeBulletUtil::convert(env, rotB, &transB.getBasis()); + + btGeneric6DofSpringConstraint* joint = new btGeneric6DofSpringConstraint(*bodyA, *bodyB, transA, transB, useLinearReferenceFrameA); + return (long)joint; + } + +#ifdef __cplusplus +} +#endif diff --git a/engine/src/bullet/native/com_jme3_bullet_joints_SixDofSpringJoint.h b/engine/src/bullet/native/com_jme3_bullet_joints_SixDofSpringJoint.h new file mode 100644 index 000000000..b4fced0c9 --- /dev/null +++ b/engine/src/bullet/native/com_jme3_bullet_joints_SixDofSpringJoint.h @@ -0,0 +1,61 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_jme3_bullet_joints_SixDofSpringJoint */ + +#ifndef _Included_com_jme3_bullet_joints_SixDofSpringJoint +#define _Included_com_jme3_bullet_joints_SixDofSpringJoint +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: com_jme3_bullet_joints_SixDofSpringJoint + * Method: enableSpring + * Signature: (JIZ)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_joints_SixDofSpringJoint_enableSpring + (JNIEnv *, jobject, jlong, jint, jboolean); + +/* + * Class: com_jme3_bullet_joints_SixDofSpringJoint + * Method: setStiffness + * Signature: (JIF)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_joints_SixDofSpringJoint_setStiffness + (JNIEnv *, jobject, jlong, jint, jfloat); + +/* + * Class: com_jme3_bullet_joints_SixDofSpringJoint + * Method: setDamping + * Signature: (JIF)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_joints_SixDofSpringJoint_setDamping + (JNIEnv *, jobject, jlong, jint, jfloat); + +/* + * Class: com_jme3_bullet_joints_SixDofSpringJoint + * Method: setEquilibriumPoint + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_joints_SixDofSpringJoint_setEquilibriumPoint__J + (JNIEnv *, jobject, jlong); + +/* + * Class: com_jme3_bullet_joints_SixDofSpringJoint + * Method: setEquilibriumPoint + * Signature: (JI)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_joints_SixDofSpringJoint_setEquilibriumPoint__JI + (JNIEnv *, jobject, jlong, jint); + +/* + * Class: com_jme3_bullet_joints_SixDofSpringJoint + * Method: createJoint + * Signature: (JJLcom/jme3/math/Vector3f;Lcom/jme3/math/Matrix3f;Lcom/jme3/math/Vector3f;Lcom/jme3/math/Matrix3f;Z)J + */ +JNIEXPORT jlong JNICALL Java_com_jme3_bullet_joints_SixDofSpringJoint_createJoint + (JNIEnv *, jobject, jlong, jlong, jobject, jobject, jobject, jobject, jboolean); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsCharacter.cpp b/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsCharacter.cpp index ea525480e..b8d3afa00 100644 --- a/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsCharacter.cpp +++ b/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsCharacter.cpp @@ -68,7 +68,7 @@ extern "C" { env->ThrowNew(newExc, "The native object does not exist."); return; } - ghost->setCollisionFlags(ghost->getCollisionFlags() | btCollisionObject::CF_CHARACTER_OBJECT); + ghost->setCollisionFlags(/*ghost->getCollisionFlags() |*/ btCollisionObject::CF_CHARACTER_OBJECT); ghost->setCollisionFlags(ghost->getCollisionFlags() & ~btCollisionObject::CF_NO_CONTACT_RESPONSE); } diff --git a/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsGhostObject.cpp b/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsGhostObject.cpp index 42d496eb7..b0c894472 100644 --- a/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsGhostObject.cpp +++ b/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsGhostObject.cpp @@ -37,8 +37,10 @@ #include #include "com_jme3_bullet_objects_PhysicsGhostObject.h" +#include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h" #include "jmeBulletUtil.h" - +#include "jmePhysicsSpace.h" +#include "jmeUserPointer.h" #ifdef __cplusplus extern "C" { #endif @@ -167,6 +169,48 @@ extern "C" { jmeBulletUtil::convert(env, &ghost->getWorldTransform().getBasis(), value); } + class jmeGhostOverlapCallback : public btOverlapCallback { + JNIEnv* m_env; + jobject m_object; + public: + jmeGhostOverlapCallback(JNIEnv *env, jobject object) + :m_env(env), + m_object(object) + { + } + virtual ~jmeGhostOverlapCallback() {} + virtual bool processOverlap(btBroadphasePair& pair) + { + btCollisionObject *co1 = (btCollisionObject *)pair.m_pProxy0->m_clientObject; + jmeUserPointer *up1 = (jmeUserPointer*)co1 -> getUserPointer(); + + m_env->CallVoidMethod(m_object, jmeClasses::PhysicsGhostObject_addOverlappingObject, up1->javaCollisionObject); + if (m_env->ExceptionCheck()) { + m_env->Throw(m_env->ExceptionOccurred()); + return false; + } + + return false; + } + }; + + /* + * Class: com_jme3_bullet_objects_PhysicsGhostObject + * Method: getOverlappingObjects + * Signature: (J)V + */ + JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsGhostObject_getOverlappingObjects + (JNIEnv *env, jobject object, jlong objectId) { + btPairCachingGhostObject* ghost = (btPairCachingGhostObject*) objectId; + if (ghost == NULL) { + jclass newExc = env->FindClass("java/lang/NullPointerException"); + env->ThrowNew(newExc, "The native object does not exist."); + return; + } + btHashedOverlappingPairCache * pc = ghost->getOverlappingPairCache(); + jmeGhostOverlapCallback cb(env, object); + pc -> processAllOverlappingPairs(&cb, NULL); + } /* * Class: com_jme3_bullet_objects_PhysicsGhostObject * Method: getOverlappingCount diff --git a/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsGhostObject.h b/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsGhostObject.h index a3e18de76..cf98e5970 100644 --- a/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsGhostObject.h +++ b/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsGhostObject.h @@ -105,6 +105,14 @@ JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsGhostObject_getPhysic JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsGhostObject_getPhysicsRotationMatrix (JNIEnv *, jobject, jlong, jobject); +/* + * Class: com_jme3_bullet_objects_PhysicsGhostObject + * Method: getOverlappingObjects + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsGhostObject_getOverlappingObjects + (JNIEnv *, jobject, jlong); + /* * Class: com_jme3_bullet_objects_PhysicsGhostObject * Method: getOverlappingCount diff --git a/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsRigidBody.cpp b/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsRigidBody.cpp index 4429df562..0ee79271f 100644 --- a/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsRigidBody.cpp +++ b/engine/src/bullet/native/com_jme3_bullet_objects_PhysicsRigidBody.cpp @@ -54,6 +54,7 @@ extern "C" { btVector3 localInertia = btVector3(); shape->calculateLocalInertia(mass, localInertia); btRigidBody* body = new btRigidBody(mass, motionState, shape, localInertia); + body->setUserPointer(NULL); return (long) body; } diff --git a/engine/src/bullet/native/jmeClasses.cpp b/engine/src/bullet/native/jmeClasses.cpp index c9e652e48..254ef9d81 100644 --- a/engine/src/bullet/native/jmeClasses.cpp +++ b/engine/src/bullet/native/jmeClasses.cpp @@ -38,6 +38,10 @@ jclass jmeClasses::PhysicsSpace; jmethodID jmeClasses::PhysicsSpace_preTick; jmethodID jmeClasses::PhysicsSpace_postTick; +jmethodID jmeClasses::PhysicsSpace_addCollisionEvent; + +jclass jmeClasses::PhysicsGhostObject; +jmethodID jmeClasses::PhysicsGhostObject_addOverlappingObject; jclass jmeClasses::Vector3f; jmethodID jmeClasses::Vector3f_set; @@ -106,6 +110,18 @@ void jmeClasses::initJavaClasses(JNIEnv* env) { PhysicsSpace_preTick = env->GetMethodID(PhysicsSpace, "preTick_native", "(F)V"); PhysicsSpace_postTick = env->GetMethodID(PhysicsSpace, "postTick_native", "(F)V"); + PhysicsSpace_addCollisionEvent = env->GetMethodID(PhysicsSpace, "addCollisionEvent_native","(Lcom/jme3/bullet/collision/PhysicsCollisionObject;Lcom/jme3/bullet/collision/PhysicsCollisionObject;J)V"); + if (env->ExceptionCheck()) { + env->Throw(env->ExceptionOccurred()); + return; + } + + PhysicsGhostObject = env->FindClass("com/jme3/bullet/objects/PhysicsGhostObject"); + if (env->ExceptionCheck()) { + env->Throw(env->ExceptionOccurred()); + return; + } + PhysicsGhostObject_addOverlappingObject = env->GetMethodID(PhysicsGhostObject, "addOverlappingObject_native","(Lcom/jme3/bullet/collision/PhysicsCollisionObject;)V"); if (env->ExceptionCheck()) { env->Throw(env->ExceptionOccurred()); return; diff --git a/engine/src/bullet/native/jmeClasses.h b/engine/src/bullet/native/jmeClasses.h index b4cbef1f9..ef55e0cec 100644 --- a/engine/src/bullet/native/jmeClasses.h +++ b/engine/src/bullet/native/jmeClasses.h @@ -43,6 +43,9 @@ public: static jclass PhysicsSpace; static jmethodID PhysicsSpace_preTick; static jmethodID PhysicsSpace_postTick; + static jmethodID PhysicsSpace_addCollisionEvent; + static jclass PhysicsGhostObject; + static jmethodID PhysicsGhostObject_addOverlappingObject; static jclass Vector3f; static jmethodID Vector3f_set; diff --git a/engine/src/bullet/native/jmeMotionState.cpp b/engine/src/bullet/native/jmeMotionState.cpp index c37e555d5..0c61f9b2f 100644 --- a/engine/src/bullet/native/jmeMotionState.cpp +++ b/engine/src/bullet/native/jmeMotionState.cpp @@ -38,6 +38,7 @@ jmeMotionState::jmeMotionState() { trans = new btTransform(); + trans -> setIdentity(); worldTransform = *trans; dirty = true; } diff --git a/engine/src/bullet/native/jmePhysicsSpace.cpp b/engine/src/bullet/native/jmePhysicsSpace.cpp index 2ed23e241..634f7f379 100644 --- a/engine/src/bullet/native/jmePhysicsSpace.cpp +++ b/engine/src/bullet/native/jmePhysicsSpace.cpp @@ -31,6 +31,8 @@ */ #include "jmePhysicsSpace.h" #include "jmeBulletUtil.h" +#include "jmeUserPointer.h" +#include /** * Author: Normen Hansen @@ -170,16 +172,33 @@ void jmePhysicsSpace::createPhysicsSpace(jfloat minX, jfloat minY, jfloat minZ, // return true when pairs need collision virtual bool needBroadphaseCollision(btBroadphaseProxy* proxy0, btBroadphaseProxy * proxy1) const { + // bool collides = (proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0; + // collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask); bool collides = (proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0; collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask); - - //add some additional logic here that modified 'collides' + if (collides) { + btCollisionObject* co0 = (btCollisionObject*) proxy0->m_clientObject; + btCollisionObject* co1 = (btCollisionObject*) proxy1->m_clientObject; + jmeUserPointer *up0 = (jmeUserPointer*) co0 -> getUserPointer(); + jmeUserPointer *up1 = (jmeUserPointer*) co1 -> getUserPointer(); + if (up0 != NULL && up1 != NULL) { + collides = (up0->group & up1->groups) != 0; + collides = collides && (up1->group & up0->groups); + + //add some additional logic here that modified 'collides' + return collides; + } + return false; + } return collides; } }; dynamicsWorld->getPairCache()->setOverlapFilterCallback(new jmeFilterCallback()); dynamicsWorld->setInternalTickCallback(&jmePhysicsSpace::preTickCallback, static_cast (this), true); dynamicsWorld->setInternalTickCallback(&jmePhysicsSpace::postTickCallback, static_cast (this)); + if (gContactProcessedCallback == NULL) { + gContactProcessedCallback = &jmePhysicsSpace::contactProcessedCallback; + } } void jmePhysicsSpace::preTickCallback(btDynamicsWorld *world, btScalar timeStep) { @@ -202,6 +221,26 @@ void jmePhysicsSpace::postTickCallback(btDynamicsWorld *world, btScalar timeStep } } +bool jmePhysicsSpace::contactProcessedCallback(btManifoldPoint &cp, void *body0, void *body1) { + // printf("contactProcessedCallback %d %dn", body0, body1); + btCollisionObject* co0 = (btCollisionObject*) body0; + jmeUserPointer *up0 = (jmeUserPointer*) co0 -> getUserPointer(); + btCollisionObject* co1 = (btCollisionObject*) body1; + jmeUserPointer *up1 = (jmeUserPointer*) co1 -> getUserPointer(); + if (up0 != NULL) { + jmePhysicsSpace *dynamicsWorld = up0->space; + if (dynamicsWorld != NULL) { + JNIEnv* env = dynamicsWorld->getEnv(); + env->CallVoidMethod(dynamicsWorld->getJavaPhysicsSpace(), jmeClasses::PhysicsSpace_addCollisionEvent, up0->javaCollisionObject, up1->javaCollisionObject, (jlong) & cp); + if (env->ExceptionCheck()) { + env->Throw(env->ExceptionOccurred()); + return true; + } + } + } + return true; +} + btDynamicsWorld* jmePhysicsSpace::getDynamicsWorld() { return dynamicsWorld; } diff --git a/engine/src/bullet/native/jmePhysicsSpace.h b/engine/src/bullet/native/jmePhysicsSpace.h index 932971ec5..7ba4c06f4 100644 --- a/engine/src/bullet/native/jmePhysicsSpace.h +++ b/engine/src/bullet/native/jmePhysicsSpace.h @@ -46,6 +46,8 @@ #include "BulletMultiThreaded/SpuCollisionTaskProcess.h" #include "BulletMultiThreaded/SequentialThreadSupport.h" #include "BulletCollision/CollisionDispatch/btSimulationIslandManager.h" +#include "BulletCollision/NarrowPhaseCollision/btManifoldPoint.h" +#include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h" /** * Author: Normen Hansen @@ -70,4 +72,5 @@ public: JNIEnv* getEnv(); static void preTickCallback(btDynamicsWorld*, btScalar); static void postTickCallback(btDynamicsWorld*, btScalar); + static bool contactProcessedCallback(btManifoldPoint &, void *, void *); }; \ No newline at end of file diff --git a/engine/src/bullet/native/jmeUserPointer.h b/engine/src/bullet/native/jmeUserPointer.h new file mode 100644 index 000000000..f7d058f8e --- /dev/null +++ b/engine/src/bullet/native/jmeUserPointer.h @@ -0,0 +1,11 @@ +#ifndef _Included_jmeUserPointer +#define _Included_jmeUserPointer +#include +class jmeUserPointer { +public: + jobject javaCollisionObject; + jint group; + jint groups; + jmePhysicsSpace *space; +}; +#endif \ No newline at end of file