gpt4 book ai didi

java - jMonkeyEngine - 像在第一人称游戏中一样用鼠标旋转

转载 作者:行者123 更新时间:2023-11-30 11:19:34 25 4
gpt4 key购买 nike

有人可以给我可以轮换我的播放器的代码示例吗?我想实现与经典 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
}

最佳答案

你能试试类似示例中的方法吗:

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();
}
}

关于java - jMonkeyEngine - 像在第一人称游戏中一样用鼠标旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23167665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com