Problem with my Bullet Class

Hey everyone!

I’ve been working in my cookbook on making a simple FPS but I’ve stumbled. I’ve done asteroids on java
and I went through its code to see where I was messing up. And jmonkey is just a whole new level of coding.

I’ve got errors on my: fire method in Bullet class, fire method in the InputAppState class, and in my
update method in the GameCharacterControl class. I’m going to post all three pages and then post the methods individually.

I think these errors are coming from not having any defined camera code and from not instancing variables from the Bullet class to the GameCharacterControl.
Please Help! Thanks.

//update method from GameCharacterControl. statements with errors have comments

public void update(float tpf){
super.update(tpf);

    Bullet b =bullets.get(i); // the incrementing value has an error (i)
    b.update(tpf);
    CollisionResult result = b.checkCollision(targets);
    if(result != null){
        System.out.println("hit" + result);
    }
    if(!b.isAlive()){             // the isAlive() thread has an error
        bullets.remove(b);
        bulletAmount--;     // and error on bulletAmount, I don't know where to initialize this at.
        if(i>0){      //once again the i has an error
            i--;    // and again i is the error
        }
    }

//fire method from InputAppState
public void fire(){
Ray ray = new Ray(app.getCamera().getLocation(), // app has an error: having camera trouble
app.getCamera().getDirection()); // app.getCamera().getDirection();
collResults = new CollisionResults();
for(Geometry g : targets){
g.collideWith(ray, collResults);
}
if(collResults.size()>0){
System.out.println(“hit”+collResults.getClosestCollision()
.getContactPoint());
character.onFire();
}
}

Then there is another fire method in the Bullet class
public void fire(){
bullets.add(new Bullet(cam.getLocation().clone(), //error on cam. should i instantiate a Camera obj?
cam.getDirection().clone())); //error on cam.

}

And what variable is supposed to replace the Ray origin variable:
ray = new Ray(origin, worldPosition); //origin with error. replace it with what?

I will explain my code further if necessary. I just don’t want to post four whole pages of code. totally unnecessary.

What kind of errors are you talking about here?

public class Bullet extends AbstractAppState implements
ActionListener, AnalogListener{

private Ray ray;
private Vector3f worldPosition;
private Vector3f direction;
private float speed = 10;
private final static int RANGE = 10;
private float distance;
private boolean alive = true;



public static void main(String[]args){
    
}
@Override
public void initialize(AppStateManager stateManager, Application app) {
    super.initialize(stateManager, app);
    ray = new Ray(origin, worldPosition);
    ray.setOrigin(worldPosition);
    
    
    //TODO: initialize your AppState, e.g. attach spatials to rootNode
    //this is called on the OpenGL thread after the AppState has been attached
}

@Override
public void update(float tpf) {
    //TODO: implement behavior during runtime
    ray.setLimit(speed*tpf);
    distance += ray.limit;
    worldPosition.addLocal(direction.mult(ray.limit));
    
    if(distance >= RANGE){
        alive=false;
    }
    
}
public void fire(){
    bullets.add(new Bullet(cam.getLocation().clone(),
            cam.getDirection().clone()));
    
}
public CollisionResult checkCollision(ArrayList<Collidable> targets){
    CollisionResults collResults = new CollisionResults();
    for(Collidable g : targets){
        g.collideWith(ray, collResults);
    }
    if(collResults.size() > 0){
        alive = false;
        return collResults.getClosestCollision();
    }
    return null;
}

In other words…How do i create a bullet? in the bullet class… I’ve got errors on the cam.getDirection and errors on cam.getLocation.

I think you need to take one step back and explain what you want to achieve. You have mixed up some classes that shouldn’t be mixed up. It does not make sense for Bullet to extend AppState, for example.

If I were to guess, app is not set in InputAppState and thus yields a NullPointerException when creating the ray. Can you check or post the initialize method from InputAppState?

But you’re also mixing up the examples. The first post contains logic for firing “instant” bullets. You create a ray and check collisions against geometries.

The Bullet class is for “non-instant” firing, ie, you trace a path over time and checks collisions each frame.
I suggest you go with the basics first, so forget about the Bullet class for now.

For all others: This is based on the Cookbook.

I trashed the bullet class. And you got a very valid point. Thank you for the help little dude.