Moving bullet forwards

hi im trying to attach an update controller to a bullet



if (KeyBindingManager.getKeyBindingManager().
                 isValidCommand("fire",false))
        {
             Box bullet = new Box("bullet", new Vector3f(), 1, 1, 1);
              bullet.setModelBound(new BoundingBox());
              bullet.updateModelBound();
              playerNode.attachChild(bullet);
              MaterialState ms = display.getRenderer().createMaterialState();
              ms.setDiffuse(ColorRGBA.green);
              bullet.setRenderState(ms);
              rootNode.attachChild(bullet);
              bullet.getLocalTranslation().set(playerNode.getLocalTranslation());
             bullet.getLocalRotation().set(playerNode.getLocalRotation());
              bullet.addController(new Controller() {
                  float speed = 5;
                  @Override
                  public void update(float time) {
                      // move bullet forward
                      bullet.getLocalTranslation().addLocal(
                      bullet.getLocalRotation().getRotationColumn(2).mult(time*speed));
                  }
              });
        }



The error is on the line: bullet.getLocalTranslation().addlocal(

I get an error :
Cannot refer to non-final variable bullet inside an inner class defined in a different method

here is the full code:



import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.input.InputHandler;
import com.jme.input.controls.GameControlManager;
import com.jme.input.controls.binding.MouseAxisBinding;
import com.jme.input.controls.controller.Axis;
import com.jme.input.controls.controller.RotationController;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.*;
import com.jme.scene.shape.Arrow;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.MaterialState;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;



public class TestSimpleRotation extends SimpleGame {
    private Node playerNode;
    private Box bullet;
    @Override
    protected void simpleInitGame() {
        // remove simple games controls
        input = new InputHandler();

        // create the floor and attach it to the scene
        Box floor = new Box("floor", new Vector3f(), 100, 1, 100);
        floor.setModelBound(new BoundingBox());
        floor.updateModelBound();
       
        MaterialState ms = display.getRenderer().createMaterialState();
        ms.setDiffuse(ColorRGBA.green);
        floor.setRenderState(ms);
        rootNode.attachChild(floor);
       
        // create the player and attach it to the scene
        playerNode = createPlayer();
        rootNode.attachChild(playerNode);
       
        // create a simple anonymous controller to move the player continously forward
        playerNode.addController(new Controller() {
            float speed = 5;
            @Override
            public void update(float time) {
                // move player forward
                playerNode.getLocalTranslation().addLocal(
                        playerNode.getLocalRotation().getRotationColumn(2).mult(time*speed));
            }
        });
       
        // set up game controls using the mouse axis (try MouseOffsetBinding as an alternative)
        GameControlManager gm = new GameControlManager();
       
        KeyBindingManager.getKeyBindingManager().add("fire",KeyInput.KEY_U);

        gm.addControl("rot_left").addBinding(new MouseAxisBinding(MouseAxisBinding.AXIS_X, true));
        gm.addControl("rot_right").addBinding(new MouseAxisBinding(MouseAxisBinding.AXIS_X, false));
        gm.addControl("rot_up").addBinding(new MouseAxisBinding(MouseAxisBinding.AXIS_Y,true));
        gm.addControl("rot_down").addBinding(new MouseAxisBinding(MouseAxisBinding.AXIS_Y,false));
        // create a rotation controller using the game controls
        RotationController rotatePlayer = new RotationController(playerNode,
        gm.getControl("rot_left"), gm.getControl("rot_right"), 1, Axis.Y);
        RotationController rotatePlayer2 = new RotationController(playerNode,
        gm.getControl("rot_down"), gm.getControl("rot_up"), 1, Axis.X);
       
        playerNode.addController(rotatePlayer);
        playerNode.addController(rotatePlayer2);
        // set the cam above the player and look at him
        cam.setLocation(new Vector3f(0, 20, -5));
        cam.lookAt(playerNode.getLocalTranslation().clone(), Vector3f.UNIT_Y);
        cam.update();
        CameraNode camNode = new CameraNode("Camera Node", cam);
        camNode.setLocalTranslation(new Vector3f(0, 3, -4));
        camNode.updateWorldData(0);
        
        rootNode.attachChild(camNode);
        playerNode.attachChild(camNode);
       
       
    }

    // create a blue sphere with a Arrow pointing into the players direction
    private Node createPlayer() {
        playerNode = new Node("playerNode");
        Sphere player = new Sphere("player", new Vector3f(), 10, 10, 1);
        player.setModelBound(new BoundingBox());
        player.updateModelBound();
       
        Arrow arrow = new Arrow("arr", 2, 0.3f);
        arrow.setLocalRotation(new Quaternion().fromAngleAxis(FastMath.DEG_TO_RAD * 90, Vector3f.UNIT_X));
        arrow.setModelBound(new BoundingBox());
        arrow.updateModelBound();
        arrow.getLocalTranslation().addLocal(0, 0, 2);
        playerNode.attachChild(arrow);

        MaterialState msp = display.getRenderer().createMaterialState();
        msp.setDiffuse(ColorRGBA.blue);
        player.setRenderState(msp);

        playerNode.attachChild(player);
        playerNode.getLocalTranslation().addLocal(0, 2f, 0);
       
        return playerNode;
    }
   
    static Vector3f playervect;
   
    @Override
    protected void simpleUpdate() {
       
       //playervect=playerNode.getLocalTranslation();
       
       //cam.setLocation(playervect);
       
       //cam.setUp(playerNode.g);
       cam.lookAt(playerNode.getLocalTranslation().clone(), Vector3f.UNIT_Y);
        cam.update();
       
        if (KeyBindingManager.getKeyBindingManager().
                 isValidCommand("fire",false))
        {
             Box bullet = new Box("bullet", new Vector3f(), 1, 1, 1);
              bullet.setModelBound(new BoundingBox());
              bullet.updateModelBound();
              playerNode.attachChild(bullet);
              MaterialState ms = display.getRenderer().createMaterialState();
              ms.setDiffuse(ColorRGBA.green);
              bullet.setRenderState(ms);
              rootNode.attachChild(bullet);
              bullet.getLocalTranslation().set(playerNode.getLocalTranslation());
             bullet.getLocalRotation().set(playerNode.getLocalRotation());
              bullet.addController(new Controller() {
                  float speed = 5;
                  @Override
                  public void update(float time) {
                      // move bullet forward
                      bullet.getLocalTranslation().addLocal(
                      bullet.getLocalRotation().getRotationColumn(2).mult(time*speed));
                  }
              });
        }
       
    }

       
   
   
    public static void main(String[] args) {
        TestSimpleRotation game = new TestSimpleRotation();
        game.setConfigShowMode(ConfigShowMode.AlwaysShow);
        game.start();
    }
}

Well, try to declare Bullet final :slight_smile:



Or create a real Class 'BulletController extends Controller' where you can pass the bullet reference as a Parameter instead a anonymous inner class.

(then you can re use it for other movable objects)



something like that:

http://code.google.com/p/jme-demos/source/browse/trunk/stardust/src/com/jmedemos/stardust/scene/projectile/ProjectileMover.java

I forgot to mention that delaring it final didnt work,



thanks for the other suggestion ill try it out

that works for me

final Box bullet = new Box("bullet", new Vector3f(), 1, 1, 1);



set the bullet speed to 500 instead 5 tho

thanks its working now

oh yeah it works thanks

OK I decided to make a bullet class:



   static Box create (Node parentNode,Node rootNode)
   {
          final Box bullet = new Box("bullet", new Vector3f(), 1, 1, 1);
         bullet.setModelBound(new BoundingBox());
         bullet.updateModelBound();
         parentNode.attachChild(bullet);
         MaterialState ms = display.getRenderer().createMaterialState();
         ms.setDiffuse(ColorRGBA.green);
         bullet.setRenderState(ms);
         rootNode.attachChild(bullet);
         bullet.getLocalTranslation().set(parentNode.getLocalTranslation());
        bullet.getLocalRotation().set(parentNode.getLocalRotation());
    
      return bullet;
   }



the class doesnt know what 'display' is, do I have to import a file? or put display in the 'create' method arguments?

You can get the DisplaySystem by its getter method.

DisplaySystem display = DisplaySystem.getDisplaySystem();



thanks!





the bullet doesnt seem to move

here is my Class:


public class bullet extends Controller
{
   
   float speed = 50;
   public Box bulletNode;
       @Override
    public void update(float time) {
        // move bullet forward
        bulletNode.getLocalTranslation().addLocal(
        bulletNode.getLocalRotation().getRotationColumn(2).mult(time*speed));
    }
   
   static bullet create (Node parentNode,Node rootNode)
   {
       bullet b=new bullet();
          Box bulletmodel = new Box("bullet", new Vector3f(), 1, 1, 1);
         bulletmodel.setModelBound(new BoundingBox());
         bulletmodel.updateModelBound();
         parentNode.attachChild(bulletmodel);
         DisplaySystem display = DisplaySystem.getDisplaySystem();
         MaterialState ms = display.getRenderer().createMaterialState();
         ms.setDiffuse(ColorRGBA.green);
         bulletmodel.setRenderState(ms);
         rootNode.attachChild(bulletmodel);
         bulletmodel.getLocalTranslation().set(parentNode.getLocalTranslation());
        bulletmodel.getLocalRotation().set(parentNode.getLocalRotation());
     b.bulletNode=bulletmodel;
      return b;
   }
}



also the bullets are blue,  when they are supposed to be green
slenkar said:

also the bullets are blue,  when they are supposed to be green

Whenever you attach a object to the scene, do a rootNode.updateRenderState(). or you'll get some funky effects.

Try to organize your classes different, now your controller class creates a Bullet, which seems strange to me.
You should create a Bullet and add a controller to that bullet which moves it around.