Bullet Movement

Hi everyone,



I have another annoying problem which I cant solve. My spaceship fires bullets coming from the ship. This goes fine when I simply fly straight forward when I start the program. I dont move anywhere else, just straight forward. This can be seen from the image:







There are some bullets in the far distance. That works fine.



But whenever I move the ship and I fire, the bullets first seem to be ok, coming from the ship. But then whenever I fire they simply seem to be moving further to the right and not into the direction the ship is pointing. Basically, the bullets dont go into the right direction :S



Here is the code that makes the bullet, and the bulletmover.



Fire a bullet!



 public void fire(){
                       
        model = new Sphere("bullet" + numBullets++, 8, 8, .25f);
        model.setModelBound(new BoundingBox());
        model.updateModelBound();
        model.setLocalTranslation(new Vector3f(camera.getDirection()));       
        model.updateGeometricState(0, true);
        model.addController(new WeaponMover(this,model,new Vector3f(ship.getModel().getWorldTranslation())));
       
        this.attachChild(model);
        model.updateRenderState();
    }



Move the bullet:


public void update(float time){
        lifeTime-=time;
       
        if (lifeTime < 0) {
            weapon.detachChild(this.bullet);
            weapon.removeController(this);
            return;
        }
       
        Vector3f currPos = this.bullet.getLocalTranslation();
       
        currPos.addLocal(direction.mult(time * speed));
        System.out.println(direction + " " + time + " " + speed);
        this.bullet.setLocalTranslation(currPos);                                       
    }



Thanks for the furball code btw :)
I think the problem lies with the getMode.getWorldTranslation. But the model is pointing forward, just like in the image or another direction. And I cant use the camera.getDirection, this lets the bullets also behave irratically.

Regards,
Frank

Argh, in some instances the bullets even move away from the camera. I thought when a chase camera is always positioned behind the ship the direction is always the same direction the ship is pointing to.

hiya,

a few things:

  • before reading ship.getWorldTranslation() you need to call ship.updateWorldVectors().
  • the direction for your WeaponMover would be ship.getLocalRotation().getRotationColumn(2).

     
  • i think your fire() method is a bit strange.

      you should create a new Bullet, set the bullets transaltion to you ships localTranslation and then add the bullet the the scenes rootNode or a separete bullet Node.

    You can't add the bullet to your ship, because then the bullet will move with your ship.





    I am also making a space shooter much like yours, if you want you can look at the source here:

    http://www.stardust.ch/bugs/browser/branches/milestone-0.2/src

    (only the milestone-0.2 branch is relevant, the head is not useful right now)

Thank you very much! I'll look into it straight away! (Yes, I was refreshing this page a lot… :slight_smile: )

Oh and the this.attachChild refers to a seperate Weapon node. So I add it to the currently selected weapon node which is not attached to the ship in any way.

model = new Sphere("bullet" + numBullets++, 8, 8, .25f);


are you sure you want to reuse a member for this? you might accidently use this.model elsewhere and won't know for sure which bullet is referenced by it.

model.setLocalTranslation(new Vector3f(camera.getDirection()));       


um... :? are you sure you want to do this?
why do you want to translate the model by a small amount into the direction the camera is looking at?

also, i never see you set the bullets rotation. how does it know in which direction to fly?
<hint>use the ships direction</hint>

Well actually the gun system is still somewhat sloppy :slight_smile: I just wanted to see whether it would work which it does, but not in the right angle.