Displaying model correctly in front of my camera


Right now I have imported my own gun.jme model into my pile of code and gave the location of my cam. As of now the gun is in the camera's sight and nicely following untill I move my camera.

The gun is on a fixed position from my cam but I want to move it with my cam when I look up, down left or right.

What approach do I need to take to solve this problem?

Thanks! :slight_smile:

edit: Im guessing, judging from how the FaderQuad sticks to the camera, I have to use something like

getLocalTranslation().set(DisplaySystem.getDisplaySystem().getWidth() / 2, DisplaySystem.getDisplaySystem().getHeight() / 2, 0.0f);

to get it exactly centered however, I believe using that will place the object at a fixed x/y-position.

hardcore way that works but might not be the best cause of writing from the top of my head…

    Vector3f camPos = new Vector3f();
    Vector3f gunBaseOffset = new Vector3f(5.0f,2.0f,20.0f); //offset from cam when looking straight ahead
    Vector3f tmpOffset = new Vector3f();
    Quaternion camRot = new Quaternion();

    protected void simpleUpdate() {
        camRot.fromAxes(cam.getLeft(), cam.getUp(), cam.getDirection());



You could also use CameraNode instead of a straight camera and attach the gun to the camera node.

but that's way too easy! XD


It works great, the gun now has a fixed position in my screen no matter where I look. I was wondering though how I could change the rotation of the model.

What I did was import the .jme model in a node(createMP5()) and initialise the node in my main class(via function initGuns()). Now whenever I rotate the node in the main class, nothing happends (I use the SetLocalRotation function). When I rotate the model inside the creation function(SetLocalRotation within createMP5), nothing happends either.

 public static Node createMP5()
        URL model;

            model = new File("jmetest/data/model/mp5.jme").toURL();//"models/guns/mp5.jme").toURL();

            JmeBinaryReader jbr = new JmeBinaryReader();

            int texKit = r.nextInt(textureKitCounter) + 1;

                URL tex = new File("jmetest/data/textures/guns/kit" + String.valueOf(texKit))
                jbr.setProperty("texurl", tex);
                jbr.setProperty("bound", "box");

                Node gun = jbr.loadBinaryFormat(model.openStream());
                // shrink this baby down some

                gun.setName("mp5" + i);
               // initMP5Sounds();
                return gun;


init the gun in the mainclass

   private void initGuns()
   gunMP5 = WeaponMachineGun.createMP5();

Added your update code

protected void update(float interpolation)
      interpolation = timer.getTimePerFrame();
      if (KeyBindingManager.getKeyBindingManager().isValidCommand("exit"))
         finished = true;
        /** Update tpf to time per frame according to the Timer. */
        tpf = timer.getTimePerFrame();
        /** Check for key/mouse updates. */
        input.update( tpf );

            camRot.fromAxes(cam.getLeft(), cam.getUp(), cam.getDirection());


   scene.updateGeometricState(tpf, true);

Whenever I change the camRot values (with use of the set() and other functions) I completely ruin it. I'd suppose changing it in the createMP5() function would be sufficient but Im not certain why he doesnt like it.


What advantages does it bring when using a cameranode instead?

Thanks again for your time.

well,  gunMP5.getLocalRotation().set(camRot); in the update method overwrites whatever rotation you have set earlier on it…that code rotates the gun to point in the direction of the camera…

if you want to add some extra rotation on top of the "follow the camera" rotation just multiply in an extra quaternion…

something like this:

    Quaternion extraRot = new Quaternion();

        extraRot.fromAngleAxis(timer.getTimeInSeconds(), Vector3f.UNIT_Y); //dummy: just spin the gun around it's y-axis


Didn't know you could actually add multiple Quarternions to a node.

Ill try to post a screeny when i've succesfully alligned the model! :slight_smile:

Thanks a million