Camera Node - Gun!

Hey Guys,

I searched for hours now but I still don’t find anything .__. So I have a Terrain with Houses etc…A Physics Player -> CharacterControl and the cam’s Location is set to the physical player location. Now I want to attach a Gun to the screen (basically to the player). But how? With a camera Node I can’t move the view with my mouse anymore…There must be a simple way to do this :open_mouth: Heeeeelp me guys! :slight_smile:

yeh i need this too :slight_smile: also i need gun models too :smiley:

Best way may be to create another viewport. In the following link is a thread about it and with code doing just that.
http://hub.jmonkeyengine.org/forum/topic/can-i-render-an-object-ontop-of-everything-but-under-gui/

Other possibilities: application.getGuiNode / tonegodGUI / nifty

Thanks, but the problem is that I don’t know how (where) to attach my gun model. This can’t be so difficult :open_mouth: I mean I could attach it to a node attached to the cam. Then I adjust the rotation and location of the gun. But how can I adjust the rotation of it?

I need this too, if someone find the answer please reply for us. I already look for this since 2 day ago and i didn’t found nothing.

Edit:

I found this: http://jmonkeyengine.googlecode.com/svn/trunk/engine/src/test/jme3test/input/TestCameraNode.java

But in this case the Object is a Geometry, on my case i got a CharacterControl as my player, a Vector3f as my WalkDirection and a Spatial as my Weapon, so i can’t put both in the same Node.

I created something like this for a ludum dare competition (it was adapted from the CamNode):

https://code.google.com/p/jme-ld24/source/browse/trunk/src/com/teamjmonkey/controls/WeaponFollowCamControl.java

Edit: Created a testcase for you guys

[java]package com.mmm.util.test;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;

public class TestFPSGun extends SimpleApplication {

private Spatial weapon;

public static void main(String[] args) {
    new TestFPSGun().start();
}

@Override
public void simpleInitApp() {

    // Weapon
    Box box = new Box(0.5f, 0.5f, 0.5f);
    weapon = new Geometry("geometry", box);
    Material unshadedMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    weapon.setMaterial(unshadedMat);
    rootNode.attachChild(weapon);

    // Random thing
    Box box1 = new Box(2, 2, 2);
    Spatial box2 = new Geometry("geometry", box1);
    Material unshadedMat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    unshadedMat1.setColor("Color", ColorRGBA.Blue);
    box2.setMaterial(unshadedMat1);
    rootNode.attachChild(box2);
}

public void simpleUpdate(float tpf) {
    Vector3f vectorDifference = new Vector3f(cam.getLocation().subtract(weapon.getWorldTranslation()));
    weapon.setLocalTranslation(vectorDifference.addLocal(weapon.getLocalTranslation()));

    Quaternion worldDiff = new Quaternion(cam.getRotation().mult(weapon.getWorldRotation().inverse()));
    weapon.setLocalRotation(worldDiff.multLocal(weapon.getLocalRotation()));

    // Move it to the bottom right of the screen
    weapon.move(cam.getDirection().mult(3));
    weapon.move(cam.getUp().mult(-0.8f));
    weapon.move(cam.getLeft().mult(-1f));
    weapon.rotate(0.3f, FastMath.PI, 0);
}

}[/java]

Edit again: changed color of other box, so you can see easier

4 Likes

OMG!
I love you! Haha It works :slight_smile:

Thaaaaaaaaaank you!

Works for me too,

Wezrule really thanks :wink:

thank you man! it really works!