Hi all,
I think I am doing something wrong but I can’t quite work it out.
Here is the code that is giving me problems. It crashes the moment there is a collision! Error messages are also below. It looks like NodeA and NodeB are both null and this doesn’t make sense to me.
[java]package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
public class Main extends SimpleApplication {
private Node pivot;
private Node pivot2;
RigidBodyControl rb1;
RigidBodyControl rb2;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
//setup cam
cam.setLocation(new Vector3f(0f, 30f, 0f));
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
//setup first object
Box b1 = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom1 = new Geometry(“Box”, b1);
Material mat1 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat1.setColor(“Color”, ColorRGBA.Blue);
geom1.setMaterial(mat1);
geom1.setLocalTranslation(new Vector3f(Vector3f.ZERO));
Box b2 = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom2 = new Geometry(“Box”, b2);
Material mat2 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat2.setColor(“Color”, ColorRGBA.Blue);
geom2.setMaterial(mat2);
geom2.setLocalTranslation(new Vector3f(2f, 0f, 0f));
pivot = new Node(“Pivot”);
pivot.attachChild(geom1);
pivot.attachChild(geom2);
pivot.setLocalTranslation(new Vector3f(-6f, 0f, 0f));
//setup second object
Box b3 = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom3 = new Geometry(“Box”, b3);
Material mat3 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat3.setColor(“Color”, ColorRGBA.Red);
geom3.setMaterial(mat3);
geom3.setLocalTranslation(Vector3f.ZERO);
Box b4 = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom4 = new Geometry(“Box”, b4);
Material mat4 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat4.setColor(“Color”, ColorRGBA.Red);
geom4.setMaterial(mat4);
geom4.setLocalTranslation(new Vector3f(2f, 0f, 0f));
pivot2 = new Node(“Pivot”);
pivot2.attachChild(geom3);
pivot2.attachChild(geom4);
pivot2.setLocalTranslation(Vector3f.ZERO);
//start physics engine and enable debugging
BulletAppState bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
//create collision shape for first object and add to physics engine
CollisionShape s1 = CollisionShapeFactory.createMeshShape(pivot);
rb1 = new RigidBodyControl(s1, 1f);
rb1.setKinematic(true);
bulletAppState.getPhysicsSpace().add(rb1);
//create collision shape for second object and add to physics engine
CollisionShape s2 = CollisionShapeFactory.createMeshShape(pivot2);
rb2 = new RigidBodyControl(s2, 1f);
rb2.setKinematic(true);
bulletAppState.getPhysicsSpace().add(rb2);
//add objects to root node
rootNode.attachChild(pivot);
rootNode.attachChild(pivot2);
//start collision listener
bulletAppState.getPhysicsSpace().addCollisionListener(physicsCollisionListener);
}
@Override
public void simpleUpdate(float tpf) {
Vector3f tempVector = rb1.getPhysicsLocation();
Vector3f tempVector2 = pivot.getLocalTranslation();
tempVector = tempVector2; //ensure that the node location and physics location are the same
tempVector = new Vector3f(tempVector.x + 1.0f*tpf, tempVector.y, tempVector.z);
rb1.setPhysicsLocation(tempVector);
pivot.setLocalTranslation(tempVector);
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
private PhysicsCollisionListener physicsCollisionListener = new PhysicsCollisionListener() {
@Override
public void collision(PhysicsCollisionEvent event) {
//System.out.println(event.getNodeA().getName());
//System.out.println(event.getNodeB().getName());
//System.out.println(event);
//System.out.println(event.getNodeA());
//System.out.println(event.getNodeB());
System.out.println(“Collision!!!”);
}
};
}
[/java]
[java]init:
Deleting: /MD0/Android/jmonkeyplatformworkspace/BasicGame2/build/built-jar.properties
deps-jar:
Updating property file: /MD0/Android/jmonkeyplatformworkspace/BasicGame2/build/built-jar.properties
compile:
run:
30/10/2011 4:15:45 PM com.jme3.system.JmeSystem initialize
INFO: Running on jMonkeyEngine 3.0.0 Beta
30/10/2011 4:15:45 PM com.jme3.system.Natives getExtractionDir
WARNING: Working directory is not writable. Using home directory instead.
30/10/2011 4:15:45 PM com.jme3.system.Natives extractNativeLibs
INFO: Extraction Directory: /home/cmicallef/.jme3/natives_136ebd45
30/10/2011 4:15:45 PM com.jme3.system.lwjgl.LwjglAbstractDisplay run
INFO: Using LWJGL 2.8.1
30/10/2011 4:15:45 PM com.jme3.system.lwjgl.LwjglDisplay createContext
INFO: Selected display mode: 800 x 600 x 0 @0Hz
30/10/2011 4:15:46 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Adapter: null
30/10/2011 4:15:46 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Driver Version: null
30/10/2011 4:15:46 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Vendor: NVIDIA Corporation
30/10/2011 4:15:46 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: OpenGL Version: 3.3.0 NVIDIA 280.10.01.04
30/10/2011 4:15:46 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Renderer: GeForce 8400 GS/PCI/SSE2
30/10/2011 4:15:46 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: GLSL Ver: 3.30 NVIDIA via Cg compiler
30/10/2011 4:15:46 PM com.jme3.system.lwjgl.LwjglTimer <init>
INFO: Timer resolution: 1,000 ticks per second
30/10/2011 4:15:46 PM com.jme3.renderer.lwjgl.LwjglRenderer initialize
INFO: Caps: [FrameBuffer, FrameBufferMRT, FrameBufferMultisample, TextureMultisample, OpenGL20, OpenGL21, OpenGL30, OpenGL31, OpenGL32, ARBprogram, GLSL100, GLSL110, GLSL120, GLSL130, GLSL140, GLSL150, VertexTextureFetch, TextureArray, TextureBuffer, FloatTexture, FloatColorBuffer, FloatDepthBuffer, PackedFloatTexture, SharedExponentTexture, PackedFloatColorBuffer, TextureCompressionLATC, NonPowerOfTwoTextures, MeshInstancing, VertexBufferArray]
30/10/2011 4:15:46 PM com.jme3.asset.DesktopAssetManager <init>
INFO: DesktopAssetManager created.
30/10/2011 4:15:46 PM com.jme3.renderer.Camera <init>
INFO: Camera created (W: 800, H: 600)
30/10/2011 4:15:46 PM com.jme3.renderer.Camera <init>
INFO: Camera created (W: 800, H: 600)
30/10/2011 4:15:46 PM com.jme3.input.lwjgl.LwjglMouseInput initialize
INFO: Mouse created.
30/10/2011 4:15:46 PM com.jme3.input.lwjgl.LwjglKeyInput initialize
INFO: Keyboard created.
30/10/2011 4:15:46 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: AudioRenderer supports 64 channels
30/10/2011 4:15:46 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio effect extension version: 1.0
30/10/2011 4:15:46 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio max auxilary sends: 4
30/10/2011 4:15:46 PM com.jme3.material.MaterialDef <init>
INFO: Loaded material definition: Unshaded
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (BitmapFont) attached to this node (null)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (null) attached to this node (Gui Node)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (BitmapFont) attached to this node (null)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (null) attached to this node (Statistics View)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (BitmapFont) attached to this node (null)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (null) attached to this node (Statistics View)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (BitmapFont) attached to this node (null)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (null) attached to this node (Statistics View)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (BitmapFont) attached to this node (null)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (null) attached to this node (Statistics View)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (BitmapFont) attached to this node (null)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (null) attached to this node (Statistics View)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (BitmapFont) attached to this node (null)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (null) attached to this node (Statistics View)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (BitmapFont) attached to this node (null)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (null) attached to this node (Statistics View)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (BitmapFont) attached to this node (null)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (null) attached to this node (Statistics View)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (BitmapFont) attached to this node (null)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (null) attached to this node (Statistics View)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (BitmapFont) attached to this node (null)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (null) attached to this node (Statistics View)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (BitmapFont) attached to this node (null)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (null) attached to this node (Statistics View)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (BitmapFont) attached to this node (null)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (null) attached to this node (Statistics View)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (BitmapFont) attached to this node (null)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (null) attached to this node (Statistics View)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (Statistics View) attached to this node (Gui Node)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (Box) attached to this node (Pivot)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (Box) attached to this node (Pivot)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (Box) attached to this node (Pivot)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (Box) attached to this node (Pivot)
30/10/2011 4:15:46 PM com.jme3.bullet.PhysicsSpace addRigidBody
INFO: Adding RigidBody com.bulletphysics.dynamics.RigidBody@10c10de0 to physics space.
30/10/2011 4:15:46 PM com.jme3.bullet.PhysicsSpace addRigidBody
INFO: Adding RigidBody com.bulletphysics.dynamics.RigidBody@60c017e9 to physics space.
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (Pivot) attached to this node (Root Node)
30/10/2011 4:15:46 PM com.jme3.scene.Node attachChild
INFO: Child (Pivot) attached to this node (Root Node)
30/10/2011 4:15:46 PM com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation
INFO: Uniform m_VertexColor is not declared in shader [ShaderSource[name=Common/MatDefs/Misc/Unshaded.vert, defines, type=Vertex], ShaderSource[name=Common/MatDefs/Misc/Unshaded.frag, defines, type=Fragment]].
warning CollisionDispatcher.needsCollision: static-static collision!
30/10/2011 4:15:48 PM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at com.bulletphysics.collision.dispatch.CollisionDispatcher.freeCollisionAlgorithm(CollisionDispatcher.java:119)
at com.bulletphysics.collision.dispatch.CompoundCollisionAlgorithm.destroy(CompoundCollisionAlgorithm.java:76)
at com.bulletphysics.collision.dispatch.CollisionDispatcher.freeCollisionAlgorithm(CollisionDispatcher.java:120)
at com.bulletphysics.collision.dispatch.CompoundCollisionAlgorithm.destroy(CompoundCollisionAlgorithm.java:76)
at com.bulletphysics.collision.dispatch.CollisionDispatcher.freeCollisionAlgorithm(CollisionDispatcher.java:120)
at com.bulletphysics.collision.broadphase.HashedOverlappingPairCache.cleanOverlappingPair(HashedOverlappingPairCache.java:219)
at com.bulletphysics.collision.broadphase.HashedOverlappingPairCache.removeOverlappingPair(HashedOverlappingPairCache.java:91)
at com.bulletphysics.collision.broadphase.DbvtBroadphase.collide(DbvtBroadphase.java:145)
at com.bulletphysics.collision.broadphase.DbvtBroadphase.calculateOverlappingPairs(DbvtBroadphase.java:235)
at com.bulletphysics.collision.dispatch.CollisionWorld.performDiscreteCollisionDetection(CollisionWorld.java:139)
at com.bulletphysics.dynamics.DiscreteDynamicsWorld.internalSingleStepSimulation(DiscreteDynamicsWorld.java:378)
at com.bulletphysics.dynamics.DiscreteDynamicsWorld.stepSimulation(DiscreteDynamicsWorld.java:339)
at com.jme3.bullet.PhysicsSpace.update(PhysicsSpace.java:342)
at com.jme3.bullet.PhysicsSpace.update(PhysicsSpace.java:329)
at com.jme3.bullet.BulletAppState.render(BulletAppState.java:189)
at com.jme3.app.state.AppStateManager.render(AppStateManager.java:169)[/java]
Fixed the problem how it crashes on collision. Amended code below.
I shouldn’t have used createMeshShape() as that is used for static objects.
However, I’m still getting null pointers for NodeA and NodeB. This doesn’t make sense to me.
[java]package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
public class Main extends SimpleApplication {
private Node pivot;
private Node pivot2;
RigidBodyControl rb1;
RigidBodyControl rb2;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
//setup cam
cam.setLocation(new Vector3f(0f, 30f, 0f));
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
//setup first object
Box b1 = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom1 = new Geometry(“Box”, b1);
Material mat1 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat1.setColor(“Color”, ColorRGBA.Blue);
geom1.setMaterial(mat1);
geom1.setLocalTranslation(new Vector3f(Vector3f.ZERO));
Box b2 = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom2 = new Geometry(“Box”, b2);
Material mat2 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat2.setColor(“Color”, ColorRGBA.Blue);
geom2.setMaterial(mat2);
geom2.setLocalTranslation(new Vector3f(2f, 0f, 0f));
pivot = new Node(“Pivot”);
pivot.attachChild(geom1);
pivot.attachChild(geom2);
pivot.setLocalTranslation(new Vector3f(-6f, 0f, 0f));
//setup second object
Box b3 = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom3 = new Geometry(“Box”, b3);
Material mat3 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat3.setColor(“Color”, ColorRGBA.Red);
geom3.setMaterial(mat3);
geom3.setLocalTranslation(Vector3f.ZERO);
Box b4 = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom4 = new Geometry(“Box”, b4);
Material mat4 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat4.setColor(“Color”, ColorRGBA.Red);
geom4.setMaterial(mat4);
geom4.setLocalTranslation(new Vector3f(2f, 0f, 0f));
pivot2 = new Node(“Pivot”);
pivot2.attachChild(geom3);
pivot2.attachChild(geom4);
pivot2.setLocalTranslation(Vector3f.ZERO);
//start physics engine and enable debugging
BulletAppState bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
//create collision shape for first object and add to physics engine
//CollisionShape s1 = CollisionShapeFactory.createMeshShape((Node)pivot);
CollisionShape s1 = CollisionShapeFactory.createBoxShape((Node)pivot);
rb1 = new RigidBodyControl(s1, 1f);
//rb1.setKinematic(true);
bulletAppState.getPhysicsSpace().add(rb1);
//create collision shape for second object and add to physics engine
//CollisionShape s2 = CollisionShapeFactory.createMeshShape((Node)pivot2);
CollisionShape s2 = CollisionShapeFactory.createBoxShape((Node)pivot2);
rb2 = new RigidBodyControl(s2, 1f);
//rb2.setKinematic(true);
bulletAppState.getPhysicsSpace().add(rb2);
//add objects to root node
rootNode.attachChild(pivot);
rootNode.attachChild(pivot2);
//start collision listener
bulletAppState.getPhysicsSpace().addCollisionListener(physicsCollisionListener);
}
@Override
public void simpleUpdate(float tpf) {
Vector3f tempVector = rb1.getPhysicsLocation();
Vector3f tempVector2 = pivot.getLocalTranslation();
//tempVector = tempVector2; //ensure that the node location and physics location are the same
tempVector = new Vector3f(tempVector.x + 1.0ftpf, tempVector.y, tempVector.z);
tempVector2 = new Vector3f(tempVector2.x + 1.0ftpf, tempVector2.y, tempVector2.z);
rb1.setPhysicsLocation(tempVector);
pivot.setLocalTranslation(tempVector2);
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
private PhysicsCollisionListener physicsCollisionListener = new PhysicsCollisionListener() {
@Override
public void collision(PhysicsCollisionEvent event) {
//System.out.println(event.getNodeA().getName());
//System.out.println(event.getNodeB().getName());
System.out.println(event);
System.out.println(event.getNodeA());
System.out.println(event.getNodeB());
System.out.println(“Collision!!!”);
}
};
}
[/java]
To see what I am talking about, uncomment lines 129 and 130 in the code above.
Fixed!
createDynamicMeshShape() was the trick!
[java]package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.material.Material;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
public class Main extends SimpleApplication {
private Node pivot;
private Node pivot2;
RigidBodyControl rb1;
RigidBodyControl rb2;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
//setup cam
cam.setLocation(new Vector3f(0f, 30f, 0f));
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
//setup first object
Box b1 = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom1 = new Geometry("Box", b1);
ColorRGBA testCol = ColorRGBA.White;
testCol.a = 0.5f;
ColorRGBA testCol2 = ColorRGBA.Red;
testCol2.a = 0.5f;
Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat1.setColor("Color", testCol);
mat1.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
geom1.setMaterial(mat1);
mat1.setTransparent(true);
geom1.setLocalTranslation(new Vector3f(Vector3f.ZERO));
Box b2 = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom2 = new Geometry("Box", b2);
Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat2.setColor("Color", testCol);
mat2.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
geom2.setMaterial(mat2);
geom2.setLocalTranslation(new Vector3f(2f, 0f, 0f));
pivot = new Node("Pivot");
pivot.attachChild(geom1);
pivot.attachChild(geom2);
pivot.setLocalTranslation(new Vector3f(-10f, 0f, 0f));
//setup second object
Box b3 = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom3 = new Geometry("Box", b3);
Material mat3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat3.setColor("Color", testCol2);
mat3.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
geom3.setMaterial(mat3);
geom3.setLocalTranslation(Vector3f.ZERO);
Box b4 = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom4 = new Geometry("Box", b4);
Material mat4 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat4.setColor("Color", testCol2);
mat4.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
geom4.setMaterial(mat4);
geom4.setLocalTranslation(new Vector3f(2f, 0f, 0f));
pivot2 = new Node("Pivot2");
pivot2.attachChild(geom3);
pivot2.attachChild(geom4);
pivot2.setLocalTranslation(Vector3f.ZERO);
//start physics engine and enable debugging
BulletAppState bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
//create collision shape for first object and add to physics engine
CollisionShape s1 = CollisionShapeFactory.createDynamicMeshShape((Node)pivot);
//CollisionShape s1 = CollisionShapeFactory.createBoxShape((Node)pivot);
//BoxCollisionShape s1 = new BoxCollisionShape(new Vector3f (0.5f, 0.5f, 0.5f));
// CompoundCollisionShape ss1 = new CompoundCollisionShape();
// ss1.addChildShape(CollisionShapeFactory.createMeshShape(geom1), geom1.getLocalTranslation());
// ss1.addChildShape(CollisionShapeFactory.createMeshShape(geom2), geom2.getLocalTranslation());
rb1 = new RigidBodyControl(s1, 1f);
//rb1 = new RigidBodyControl(1f);
rb1.setKinematic(true);
pivot.addControl(rb1);
//geom1.addControl(rb1);
//geom2.addControl(rb1);
bulletAppState.getPhysicsSpace().add(rb1);
//create collision shape for second object and add to physics engine
//CollisionShape s2 = CollisionShapeFactory.createMeshShape((Node)pivot2);
CollisionShape s2 = CollisionShapeFactory.createDynamicMeshShape((Node) pivot2);
// CompoundCollisionShape ss2 = new CompoundCollisionShape();
// ss2.addChildShape(CollisionShapeFactory.createMeshShape(geom3), geom3.getLocalTranslation());
// ss2.addChildShape(CollisionShapeFactory.createMeshShape(geom4), geom4.getLocalTranslation());
rb2 = new RigidBodyControl(s2, 1f);
//rb2 = new RigidBodyControl(1f);
rb2.setKinematic(true);
pivot2.addControl(rb2);
//geom3.addControl(rb2);
//geom4.addControl(rb2);
bulletAppState.getPhysicsSpace().add(rb2);
//add objects to root node
rootNode.attachChild(pivot);
rootNode.attachChild(pivot2);
//start collision listener
bulletAppState.getPhysicsSpace().addCollisionListener(physicsCollisionListener);
}
@Override
public void simpleUpdate(float tpf) {
Vector3f tempVector = rb1.getPhysicsLocation();
Vector3f tempVector2 = pivot.getLocalTranslation();
Vector3f tempVector3 = rb2.getPhysicsLocation();
Vector3f tempVector4 = pivot2.getLocalTranslation();
tempVector3 = tempVector4;
//rb2.setPhysicsLocation(tempVector3);
//pivot2.setLocalTranslation(tempVector4);
//System.out.println(tempVector);
//System.out.println(tempVector2);
tempVector = tempVector2; //ensure that the node location and physics location are the same
tempVector = new Vector3f(tempVector.x + 1.0f * tpf, tempVector.y, tempVector.z);
tempVector2 = new Vector3f(tempVector2.x + 1.0f * tpf, tempVector2.y, tempVector2.z);
//rb1.setPhysicsLocation(tempVector);
pivot.setLocalTranslation(tempVector2);
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
private PhysicsCollisionListener physicsCollisionListener = new PhysicsCollisionListener() {
@Override
public void collision(PhysicsCollisionEvent event) {
//Spatial NodeA = event.getNodeA().getParent();
//Spatial NodeB = event.getNodeB().getParent();
//System.out.println(NodeA);
//System.out.println(NodeB);
// System.out.println(event.getNodeA().getName());
// System.out.println(event.getNodeB().getName());
System.out.println(event);
if (event.getNodeA() != null) {
System.out.println(event.getNodeA());
System.out.println(event.getObjectA());
} else {
System.out.println("Node A is null!");
System.out.println(event.getObjectA());
}
if (event.getNodeB() != null) {
System.out.println(event.getNodeB());
System.out.println(event.getObjectB());
} else {
System.out.println("Node B is null!");
}
System.out.println("Collision!!!");
}
};
}
[/java]