Hello,
Following along with the collision tutorial, I decided to try it in my own app, and I got this :
[xml]Aug 18, 2011 2:32:58 PM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at com.jme3.bullet.objects.PhysicsRigidBody.setLinearVelocity(PhysicsRigidBody.java:457)
at mygame.BoxManager.addPhysics(BoxManager.java:52)
at mygame.Main$1.onAction(Main.java:148)
at com.jme3.input.InputManager.invokeActions(InputManager.java:183)[/xml]
The line it is referring to is any one of the 3 fully commented out ones here :
[java]public void addPhysics(int id, float mass, Vector3f camDir){
RigidBodyControl phy = new RigidBodyControl(mass);
System.out.println(phy.toString()); //com.jme3.bullet.control.RigidBodyControl@12d15a9
System.out.println(camDir.toString()); //(0.0, -17.67767, 17.677671)
System.out.println(gal.get(id).toString()); //Box (Geometry)
//phy.setLinearVelocity(Vector3f.UNIT_Y);
//phy.setLinearVelocity(new Vector3f(0f,-10f,0f));
//phy.setLinearVelocity(camDir);
gal.get(id).addControl(phy);
bulletAppState.getPhysicsSpace().add(phy);
}[/java]
Basically, Im trying to add physics to something on the fly.
Set a collision shape or add the control to the spatial so a RigidBody can be created.
Hey there,
I thought addControl([RigidBodyControl]) did this automagically :
RigidBodyControl(float mass)
When using this constructor, the CollisionShape for the RigidBody is generated automatically when the Control is added to a Spatial.
RigidBodyControl phy = new RigidBodyControl(mass);
gal.get(id).addControl(phy);
gal is a ArrayList [Geometry] (yeah I should use a linked list…)
This also occurs when I do anything (not just setLinearVelocity)
phy.setRestitution(2.0f); //same null pointer error
however, the object is not null.
System.out.println(phy.toString()); // puts out com.jme3.bullet.control.RigidBodyControl@1f3aa07
java.lang.NullPointerException
at com.jme3.bullet.objects.PhysicsRigidBody.setRestitution(PhysicsRigidBody.java:407)
at mygame.BoxManager.addPhysics(BoxManager.java:65)
line 65 : phy.setRestitution(2.0f);
Thats meant for static objects you should create a simple/compound collisionshape instead.
Refer to the collision tutorial :
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_physics
CollisionShape is not used there. Also it works, boxes fall out of the sky.
I found a ‘solution’. The order matters, this works :
RigidBodyControl phy = new RigidBodyControl(mass);
gal.get(id).addControl(phy);
//phy.setRestitution(2.0f); //not here!
bulletAppState.getPhysicsSpace().add(phy);
phy.setRestitution(2.0f); //this gotta go here!
It works there because they used simple shapes, box/sphere, and its given a box/sphere simple collisionshape automatically.
o man, these are fun to play with
phy.setRestitution(.8f);
phy.setLinearVelocity(camDir);
phy.setGravity(new Vector3f(0f, -40f, 0f));
phy.setFriction(7f);
I got a floor box, and giant rubber balls bouncing all around, its like a racketball court on steroids, this is so cool
http://i.imgur.com/m0rCu.jpg
<3 JME