Problems with the physics (solved)

hello, I’m trying to learn to work with jme3 and after going through the beginner tutorials i decided to make some experiments, i tried creating a few boxes and applying physics to them but for some reasons i can’t get it to work, the boxes fall with gravity but when they collide i get a NullPointerException (sometimes this only happens after they have completely gone through each other).

I’m using the nightly JME3 version.



[java]

package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.bullet.BulletAppState;

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.scene.Geometry;

import com.jme3.scene.shape.Box;





public class newTest extends SimpleApplication

{

public static void main (String[] args)

{

newTest app = new newTest();

app.start();

}



private Geometry box1, box2, box3, floor;

private BulletAppState bAS;



@Override

public void simpleInitApp()

{

speed = 0.1f;

this.flyCam.setMoveSpeed(10f);



this.bAS = new BulletAppState();

this.stateManager.attach(this.bAS);

this.bAS.getPhysicsSpace().enableDebug(assetManager);



floor = createBox(“floor”, 0,-1,0, 10,0.01f,10);

floor.getControl(RigidBodyControl.class).setMass(0);

box1 = createBox(“box1”, 0,2,0, 1,1,1);

box2 = createBox(“box2”, 2.1f,2,0, 1,0.5f,0.5f);

box3 = createBox(“box3”, 1,4,0, 2,0.3f,1);



rootNode.attachChild(box1);

rootNode.attachChild(box2);

rootNode.attachChild(box3);

rootNode.attachChild(floor);

}



private Geometry createBox(String name, float x, float y, float z, float sizeX, float sizeY, float sizeZ)

{

Box box = new Box(new Vector3f(x,y,z), sizeX, sizeY, sizeZ);

Geometry geo = new Geometry(name, box);

Material mat = new Material(assetManager, “Common/MatDefs/Misc/SolidColor.j3md”);

mat.setColor(“Color”, ColorRGBA.randomColor());

geo.setMaterial(mat);



CollisionShape colShape = CollisionShapeFactory.createMeshShape(geo);

RigidBodyControl rbc = new RigidBodyControl(colShape, 1);

geo.addControl(rbc);

bAS.getPhysicsSpace().add(rbc);



return geo;

}

}

[/java]

i appreciate any help you can provide :slight_smile:

First of all you are moving the box mesh out of the center, you should set the x,y,z location to the Geometry. To help more I’d need the error output.

this is the part of the output i get when they collide, related to the error:



[java]

21/Fev/2011 18:26:40 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.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:338)

at com.jme3.bullet.PhysicsSpace.update(PhysicsSpace.java:325)

at com.jme3.bullet.BulletAppState.render(BulletAppState.java:189)

at com.jme3.app.state.AppStateManager.render(AppStateManager.java:162)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:248)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:146)

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:203)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:208)

at java.lang.Thread.run(Thread.java:619)

BUILD SUCCESSFUL (total time: 10 seconds)

[/java]

also, i didn’t quite get what you where referring to, i set the position of the boxes when i create a new Box

You deform the mesh so that it seems that the box is at the location you want it to be. Note that mesh vs mesh collision does not work, mesh is only for static objects, use a BoxCollisionShape or HullCollisionShape for dynamic meshes.

oooohh! so that was the problem, thats what i get for looking for an example in HelloCollision instead of HelloPhysics facepalm

thank you very much for the help :smiley:

Its generally a good idea to just do all tutorials no matter what you want to do initially. After doing that a lot of things should be quite self explanatory.