Displaying model correctly in front of my camera

Hi!



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() {
        camPos.set(cam.getLocation());
        camRot.fromAxes(cam.getLeft(), cam.getUp(), cam.getDirection());

        tmpOffset.set(gunBaseOffset);
        camRot.multLocal(tmpOffset);
        camPos.addLocal(tmpOffset);

        gun.getLocalTranslation().set(camPos);
        gun.getLocalRotation().set(camRot);
    }

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

@MrCoder:



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;

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

            JmeBinaryReader jbr = new JmeBinaryReader();

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

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

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

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

            }



init the gun in the mainclass


   private void initGuns()
   {
   gunMP5 = WeaponMachineGun.createMP5();
        gunMP5.setName("MP5Node");
        scene.attachChild(gunMP5);
       
   }



Added your update code



protected void update(float interpolation)
   {
      timer.update();
      interpolation = timer.getTimePerFrame();
      input.update(interpolation);
   
      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 );

            cam.update();
      
            camPos.set(cam.getLocation());
            camRot.fromAxes(cam.getLeft(), cam.getUp(), cam.getDirection());
           
            tmpOffset.set(gunBaseOffset);
            camRot.multLocal(tmpOffset);
            camPos.addLocal(tmpOffset);
           

            gunMP5.getLocalTranslation().set(camPos);
            gunMP5.getLocalRotation().set(camRot);
           

   scene.updateGeometricState(tpf, true);
   scene.updateRenderState();
   
      
   }



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.


@Mojomonk

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();
...
...
        gun.getLocalTranslation().set(camPos);

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

Awesome!!!

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