Problem with bullet linearVelocity

I was trying to build my bullet Class, nothing further that a simple extending of PhysicsNode [“proiettile” menas bullet]:

[java]

public class Proiettile extends PhysicsNode {

private static final Sphere bullet;

private static final SphereCollisionShape bulletCollisionShape;

private static Geometry bulletg;

private static Material mat;



static {

bullet = new Sphere(32, 32, 0.4f, true, false); // Forma

bullet.setTextureMode(TextureMode.Projected); // Texture

bulletCollisionShape = new SphereCollisionShape(0.4f); // Forma per le collisioni

bulletg = new Geometry(“bullet”, bullet);

AssetManager assetManager = null;

assetManager = JmeSystem.newAssetManager(Thread.currentThread().getContextClassLoader().getResource(“com/jme3/asset/Desktop.cfg”));

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



}



public Proiettile() {

bulletg.setMaterial(mat);

this.setShadowMode(ShadowMode.Cast);

this.attachChild(bulletg);

this.collisionShape=bulletCollisionShape;

this.mass=1;

}

}[/java]

So, in the onAction method I could use:

[java]

else if(binding.equals(“Shoot”) && isPressed) {

System.out.print(“Bang!n”);

proiettile = new Proiettile();

proiettile.setLocalTranslation(cam.getLocation());

proiettile.setLinearVelocity(cam.getDirection().mult(25));

rootNode.attachChild(proiettile);

bulletAppState.getPhysicsSpace().add(proiettile);

}[/java]

but if I shoot, i’ll get

Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at com.jme3.bullet.nodes.PhysicsNode.setLinearVelocity(PhysicsNode.java:457)

even if cam direction isn't null...
Why?

Generally, never use parameterless constructors with jme3 - they are used for deserialization only AFAIK.



In this case, you should pass colissionShape to super (Physics node) in constructor instead of setting it manually.



If you really have to do it manually (which you don’t), at least use setCollisionShape(…) instead of direct field access.

Invocation:

[java]proiettile = new Proiettile(cam.getLocation(),cam.getDirection().mult(25));[/java]

Constructor:

[java] Proiettile(Vector3f location, Vector3f mult) {

System.out.println("location="+location+" mult="+mult);

bulletg.setMaterial(mat);

this.setShadowMode(ShadowMode.Cast);

this.attachChild(bulletg);

super.setCollisionShape(bulletCollisionShape);

this.setMass(1);

this.setLocalTranslation(location);

this.setLinearVelocity(mult);

}[/java]

Now when I shot, no problems shows up, but nothing is rendered onto the display, no bullet…

Which is the problem?

Ok, I found. I’ve forgot to set the material, so it was invisible:

[java]

public class MyBulletClass extends PhysicsNode {

private static final Sphere bullet;

private static final SphereCollisionShape bulletCollisionShape;

private static Geometry bulletg;

private static Material mat;



static {

bullet = new Sphere(32, 32, 0.4f, true, false); // Forma

bullet.setTextureMode(TextureMode.Projected); // Texture

bulletCollisionShape = new SphereCollisionShape(0.4f); // Forma per le collisioni

bulletg = new Geometry(“bullet”, bullet);

AssetManager assetManager = null;

assetManager = JmeSystem.newAssetManager(Thread.currentThread().getContextClassLoader().getResource(“com/jme3/asset/Desktop.cfg”));

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

}



public MyBulletClass (Vector3f pos, Vector3f vel, int mass) {

// System.out.println(“location=”+location+" mult="+mult);

mat.setColor(“m_Color”, ColorRGBA.Red);

bulletg.setMaterial(mat);

// this.setShadowMode(ShadowMode.Cast);

this.attachChild(bulletg);

this.setCollisionShape(bulletCollisionShape);

this.setMass(mass);

this.setLocalTranslation(pos);

this.setLinearVelocity(vel);

}

}[/java]

[java] private void bang() {

System.out.print(“Bang!n”);

myBullet= new MyBulletClass (cam.getLocation(),cam.getDirection().mult(25),1);

rootNode.attachChild(myBullet);

bulletAppState.getPhysicsSpace().add(myBullet);

}[/java]