有人可以给我一些代码示例吗?我希望在经典fps(w,a,s,d +鼠标)中实现相同的控制。 FlyByCamera的东西,但我不知道如何添加它。代码没有错误。感谢。
这里是与控制
相关的方法public class Main extends SimpleApplication
implements ActionListener {
private Spatial platforms[];
private Spatial trees[];
private Node sceneNode;
private Node playerNode;
private Node platformNode;
private Node treeNode;
private CameraNode camNode;
private BulletAppState bullet;
private RigidBodyControl scenePhysics;
private BetterCharacterControl player;
private Vector3f walkDir = new Vector3f(0, 0, 0);
private Vector3f viewDir = new Vector3f(0, 0, 1);
private boolean rotateLeft = false, rotateRight = false, forward = false,
backward = false, strafeLeft = false, strafeRight = false;
private float moveSpeed = 70;
public static void main(String[] args) {
Main app = new Main();
AppSettings settings = new AppSettings(true);
app.setSettings(settings);
settings.setTitle(“RUSHY”);
settings.setSettingsDialogImage(“Interface/intro.png”);
app.start();
}
public void simpleInitApp() {
bullet = new BulletAppState();
stateManager.attach(bullet);
setUpKeys();
scenePhysics = new RigidBodyControl(0f);
sceneNode.addControl(scenePhysics);
bullet.getPhysicsSpace().add(sceneNode);
rootNode.attachChild(sceneNode);
bullet.getPhysicsSpace().setGravity(new Vector3f(0, -50.0f, 0));
bullet.getPhysicsSpace().setAccuracy(0.016f);
playerNode = new Node(“player”);
playerNode.setLocalTranslation(new Vector3f(0, 10, 0)); //spawn position
rootNode.attachChild(playerNode);
player = new BetterCharacterControl(1.5f, 7f, 30f);
player.setJumpForce(new Vector3f(0, 1200f, 0));
player.setGravity(new Vector3f(0.0f, -10.0f, 0.0f));
playerNode.addControl(player);
bullet.getPhysicsSpace().add(player);
}
private void setUpKeys() {
inputManager.addMapping(“Forward”,
new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping(“Backward”,
new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping(“Rotate Left”,
new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping(“Rotate Right”,
new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping(“Strafe Right”,
new KeyTrigger(KeyInput.KEY_E));
inputManager.addMapping(“Strafe Left”,
new KeyTrigger(KeyInput.KEY_Q));
inputManager.addMapping(“Jump”,
new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this, “Rotate Left”, “Rotate Right”,
“Strafe Right”, “Strafe Left”, “Forward”, “Backward”, “Jump”);
}
public void onAction(String binding, boolean isPressed, float tpf) {
if (binding.equals(“Rotate Left”)) {
rotateLeft = isPressed;
} else if (binding.equals(“Rotate Right”)) {
rotateRight = isPressed;
} else if (binding.equals(“Strafe Left”)) {
strafeLeft = isPressed;
} else if (binding.equals(“Strafe Right”)) {
strafeRight = isPressed;
} else if (binding.equals(“Forward”)) {
forward = isPressed;
} else if (binding.equals(“Backward”)) {
backward = isPressed;
} else if (binding.equals(“Jump”)) {
player.jump();
}
}
@Override
public void simpleUpdate(float tpf) {
camNode = new CameraNode(“CamNode”, cam);
camNode.setControlDir(CameraControl.ControlDirection.SpatialToCamera);
camNode.setLocalTranslation(new Vector3f(0, 6, 0));
Quaternion quat = new Quaternion();
quat.lookAt(Vector3f.UNIT_Z, Vector3f.UNIT_Y);
camNode.setLocalRotation(quat);
playerNode.attachChild(camNode);
camNode.setEnabled(true);
Vector3f modelForwardDir = playerNode.getWorldRotation().mult(Vector3f.UNIT_Z);
Vector3f modelLeftDir = playerNode.getWorldRotation().mult(Vector3f.UNIT_X);
walkDir.set(0, 0, 0);
if (forward) {
walkDir.addLocal(modelForwardDir.mult(moveSpeed));
} else if (backward) {
walkDir.addLocal(modelForwardDir.mult(moveSpeed).
negate());
} else if (strafeLeft) {
walkDir.addLocal(modelLeftDir.mult(moveSpeed));
} else if (strafeRight) {
walkDir.addLocal(modelLeftDir.mult(moveSpeed).negate());
}
player.setWalkDirection(walkDir); // walk
if (rotateLeft) {
Quaternion rotateL = new Quaternion().
fromAngleAxis(FastMath.PI * tpf, Vector3f.UNIT_Y);
rotateL.multLocal(viewDir);
} else if (rotateRight) {
Quaternion rotateR = new Quaternion().
fromAngleAxis(-FastMath.PI * tpf, Vector3f.UNIT_Y);
rotateR.multLocal(viewDir);
}
player.setViewDirection(viewDir); // turn
}
}
答案 0 :(得分:1)
你可以尝试一些例子中的方法:
private BulletAppState bulletAppState;
private Node gameLevel;
private PhysicsCharacter player;
private Vector3f walkDirection = new Vector3f();
private boolean left=false,right=false,up=false,down=false;
@Override
public void simpleUpdate(float tpf) {
Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);
walkDirection.set(0,0,0);
if(left)
walkDirection.addLocal(camLeft);
if(right)
walkDirection.addLocal(camLeft.negate());
if(up)
walkDirection.addLocal(camDir);
if(down)
walkDirection.addLocal(camDir.negate());
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
}
private void setupKeys() {
inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this,"Lefts");
inputManager.addListener(this,"Rights");
inputManager.addListener(this,"Ups");
inputManager.addListener(this,"Downs");
inputManager.addListener(this,"Space");
}
public void onAction(String binding, boolean value, float tpf) {
if (binding.equals("Lefts")) {
if(value)
left=true;
else
left=false;
} else if (binding.equals("Rights")) {
if(value)
right=true;
else
right=false;
} else if (binding.equals("Ups")) {
if(value)
up=true;
else
up=false;
} else if (binding.equals("Downs")) {
if(value)
down=true;
else
down=false;
} else if (binding.equals("Space")) {
player.jump();
}
}