I am currently working on a FPS and having trouble with aiming with the mouse. I want the way the flyby camera aims without having the flyby camera enabled. The camera is locked to my player via a camNode, therefore the flyby cam is made irrelevant. If someone could provide code in order to solve this problem I would greatly appreciate it. IHere’s the code if you can see a problem.
[java]
/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package mygame;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.cursors.plugins.JmeCursor;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.InputListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Matrix3f;
import com.jme3.math.Vector3f;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.Camera;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.CameraControl.ControlDirection;
import com.jme3.system.AppSettings;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
/**
*
-
@author Nick
*/
public class GameplayControl extends AbstractAppState implements ScreenController,ActionListener,AnalogListener{
private Screen screen;
private Nifty nifty;
private SimpleApplication app;
private InputManager inputManager;
private AssetManager assetManager;
private Node rootNode;
private BulletAppState bulletAppState;
private Camera cam;
private AppSettings settings;private Spatial terrain;
private Spatial testGun;
private Node gunPivot;
private CameraNode camNode;
private int walkSpeed=20;
private CharacterControl player;
float zeroX;
float zeroY;public GameplayControl(){
//contructor to accept parameters//}
public void bind(Nifty nifty, Screen screen) {
this.screen=screen;
this.nifty=nifty;
}public void onStartScreen() {}
public void onEndScreen() {}
@Override
public void initialize(AppStateManager stateManager, Application app){
super.initialize(stateManager,app);
this.app=(SimpleApplication) app;
this.inputManager=this.app.getInputManager();
this.rootNode=this.app.getRootNode();
this.assetManager=this.app.getAssetManager();
this.cam=this.app.getCamera();
this.settings=this.app.getContext().getSettings();
zeroX=settings.getWidth()/2;
zeroY=settings.getHeight()/2;
inputManager.setCursorVisible(false);
//createGui();bulletAppState=new BulletAppState(); stateManager.attach(bulletAppState); createControls(); setupScene(); setUpLight(); testGun = assetManager.loadModel("Models/P416.mesh.j3o"); testGun.scale(.2f); testGun.rotate(0,FastMath.DEG_TO_RAD*-90f,0); camNode = new CameraNode("Camera Node", cam); camNode.setControlDir(ControlDirection.SpatialToCamera); camNode.rotate(0,FastMath.DEG_TO_RAD*-180f,0); camNode.setLocalTranslation(-.475f,.88f,2.1f); CapsuleCollisionShape capsuleShape= new CapsuleCollisionShape(1.5f,6f,1); player=new CharacterControl(capsuleShape,0.05f); player.setPhysicsLocation(camNode.getLocalTranslation()); player.setGravity(200); player.setFallSpeed(70); player.setJumpSpeed(50); bulletAppState.getPhysicsSpace().add(player); gunPivot = new Node("pivot"); gunPivot.attachChild(camNode); gunPivot.attachChild(testGun); rootNode.attachChild(gunPivot);
}
@Override
public void update(float tpf){
gunPivot.setLocalTranslation(player.getPhysicsLocation());
System.out.println(inputManager.getCursorPosition());
aim();
}
@Override
public void setEnabled(boolean enabled){
if(enabled){
//do everything to be done while this state is RUNNING}else{ //remover whatever is not needed while this state is PAUSED nifty.exit(); } //Nifty Gui ScreenControl methods
}
public void createGui(){
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(
app.getAssetManager(),app.getInputManager(),app.getAudioRenderer(),app.getGuiViewPort());
//Create a new NiftyGui object
Nifty nifty = niftyDisplay.getNifty();
//Read your XML and initialize custom ScreenController
nifty.fromXml(“Interface/HudScreen.xml”, “helmetView”, this);
//atach the Nifty display to the gui view port as a processor
app.getGuiViewPort().addProcessor(niftyDisplay);nifty.loadStyleFile("nifty-default-styles.xml"); nifty.loadControlFile("nifty-default-controls.xml");
}
public void onAction(String name, boolean isPressed, float tpf) {
}
public void onAnalog(String name, float value, float tpf) {
if(name.equals(“Left”)){player.setPhysicsLocation(new Vector3f(player.getPhysicsLocation().getX()-walkSpeedvalue,player.getPhysicsLocation().getY(),player.getPhysicsLocation().getZ()));}
if(name.equals(“Right”)){player.setPhysicsLocation(new Vector3f(player.getPhysicsLocation().getX()+walkSpeedvalue,player.getPhysicsLocation().getY(),player.getPhysicsLocation().getZ()));}
if(name.equals(“Forward”)){player.setPhysicsLocation(new Vector3f(player.getPhysicsLocation().getX(),player.getPhysicsLocation().getY(),player.getPhysicsLocation().getZ()-walkSpeedvalue));}
if(name.equals(“Back”)){player.setPhysicsLocation(new Vector3f(player.getPhysicsLocation().getX(),player.getPhysicsLocation().getY(),player.getPhysicsLocation().getZ()+walkSpeedvalue));}
if(name.equals(“Jump”)){player.jump();}}
public void createControls(){
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A), new KeyTrigger(KeyInput.KEY_LEFT)); inputManager.addMapping("Right",new KeyTrigger(KeyInput.KEY_D), new KeyTrigger(KeyInput.KEY_RIGHT)); inputManager.addMapping("Forward", new KeyTrigger(KeyInput.KEY_W), new KeyTrigger(KeyInput.KEY_UP)); inputManager.addMapping("Back", new KeyTrigger(KeyInput.KEY_S), new KeyTrigger(KeyInput.KEY_DOWN)); inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE)); inputManager.addMapping("Shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT)); inputManager.addListener(this, "Left"); inputManager.addListener(this, "Right"); inputManager.addListener(this, "Forward"); inputManager.addListener(this, "Back"); inputManager.addListener(this, "Jump"); inputManager.addListener(this, "Shoot");
}
public void setupScene(){
terrain = assetManager.loadModel(“Models/tempTerrain.j3o”);
terrain.scale(100f);
terrain.setLocalTranslation(new Vector3f(0,-10f,-200f));
CollisionShape sceneShape =
CollisionShapeFactory.createMeshShape(terrain);
terrain.addControl(new RigidBodyControl(sceneShape, 0));
bulletAppState.getPhysicsSpace().add(terrain);
rootNode.attachChild(terrain);
}private void setUpLight() {
// We add light so we see the scene
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.Black.mult(.8f));
rootNode.addLight(al);DirectionalLight dl = new DirectionalLight();
dl.setColor(ColorRGBA.White);
dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
rootNode.addLight(dl);
}private void aim(){
}
}
[/java]