gpt4 book ai didi

java - 如何让我的第一人称相机流畅?

转载 作者:行者123 更新时间:2023-11-30 08:41:48 26 4
gpt4 key购买 nike

我已经想出如何制作一个简单的第一人称相机,但我想让相机窒息,任何方法都可行(I.E 加速)。

我还没有找到任何简单的方法来增加相机的平滑度,希望你们能帮忙。

相机类(WIP)

package entities;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.util.vector.Vector3f;

import renderEngine.DisplayManager;
import terrains.Terrain;

public class Camera
{
private float distanceFromPlayer = 50;
private float angleAroundPlayer = 0;
private boolean FPS = false;

private Vector3f position = new Vector3f(110, 10, -52);
private float pitch = 20;
private float yaw;
private float roll;

private static final float RUN_SPEED = 20;
private static final float GRAVITY = -50f;
private static final float JUMP_POWER = 30;

private int clickedOnPlayer = 0;

private float upwardsSpeed = 0;

private boolean isInAir = false;

private Player player;

public Camera(Player player)
{
this.player = player;
}

public void move(Terrain terrain)

{

Mouse.setGrabbed(true);
this.yaw = (Mouse.getX());
float MouseY = Mouse.getY();

if(MouseY > 360)
{
pitch = 465 - MouseY;
}

if (yaw >= 1079)
{
Mouse.setCursorPosition(Display.getWidth() / 2 + 80,
Display.getHeight() / 2);
this.yaw = 720;
}
else if (yaw <= 359)
{
Mouse.setCursorPosition(Display.getWidth() / 2 + 80,
Display.getHeight() / 2);
this.yaw = 720;
}

if(pitch <= -100)
{
pitch = -100;
}

float theta = yaw - 720;

float offsetX = (float) (2 * Math.sin(Math.toRadians(theta)));
float offsetZ = (float) (2 * Math.cos(Math.toRadians(theta)));
moveCamera();
upwardsSpeed += GRAVITY * DisplayManager.getFrameTimeSeconds();
position.y += upwardsSpeed * DisplayManager.getFrameTimeSeconds();
player.getPosition().y += upwardsSpeed * DisplayManager.getFrameTimeSeconds();
float terrainHeight = terrain.getHeightOfTerain(player.getPosition().x, player.getPosition().z);
if (position.y - 10 <= terrainHeight)
{
upwardsSpeed = 0;
isInAir = false;
position.y = terrainHeight + 10;
player.getPosition().y = terrainHeight;
}

player.getPosition().x = position.x - offsetX;
player.getPosition().z = position.z + offsetZ;
player.setRotY(180 - theta);

}




private void jump()
{
if (!isInAir)
{
this.upwardsSpeed = JUMP_POWER;
isInAir = true;
}
}

public void moveCamera()
{
float theta = yaw - 720;
float offsetX = (float) (2 * Math.sin(Math.toRadians(theta)));
float offsetZ = (float) (2 * Math.cos(Math.toRadians(theta)));
if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT))
{
if (Keyboard.isKeyDown(Keyboard.KEY_W))
{
position.z -= ((offsetZ * RUN_SPEED * 3) * DisplayManager.getFrameTimeSeconds());
position.x += ((offsetX * RUN_SPEED * 3) * DisplayManager.getFrameTimeSeconds());
}
else if (Keyboard.isKeyDown(Keyboard.KEY_S))
{
position.z += ((offsetZ * RUN_SPEED * 3) * DisplayManager.getFrameTimeSeconds());
position.x -= ((offsetX * RUN_SPEED * 3) * DisplayManager.getFrameTimeSeconds());
}
else
{
position.x = position.x;
position.z = position.z;
}
if (Keyboard.isKeyDown(Keyboard.KEY_D))
{
position.z += ((offsetX * RUN_SPEED * 3) * DisplayManager.getFrameTimeSeconds());
position.x += ((offsetZ * RUN_SPEED * 3) * DisplayManager.getFrameTimeSeconds());
}
else if (Keyboard.isKeyDown(Keyboard.KEY_A))
{
position.z -= ((offsetX * RUN_SPEED * 3) * DisplayManager.getFrameTimeSeconds());
position.x -= ((offsetZ * RUN_SPEED * 3) * DisplayManager.getFrameTimeSeconds());
}
else
{
position.x = position.x;
position.z = position.z;
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE))
{
jump();
}
}
else
{
if (Keyboard.isKeyDown(Keyboard.KEY_W))
{

position.z -= ((offsetZ * RUN_SPEED) * DisplayManager.getFrameTimeSeconds());
position.x += ((offsetX * RUN_SPEED) * DisplayManager.getFrameTimeSeconds());
}
else if (Keyboard.isKeyDown(Keyboard.KEY_S))
{

position.z += ((offsetZ * RUN_SPEED) * DisplayManager.getFrameTimeSeconds());
position.x -= ((offsetX * RUN_SPEED) * DisplayManager.getFrameTimeSeconds());
}
else
{
position.x = position.x;
position.z = position.z;
}

if (Keyboard.isKeyDown(Keyboard.KEY_D))
{
position.z += ((offsetX * RUN_SPEED) * DisplayManager.getFrameTimeSeconds());
position.x += ((offsetZ * RUN_SPEED) * DisplayManager.getFrameTimeSeconds());
}
else if (Keyboard.isKeyDown(Keyboard.KEY_A))
{
position.z -= ((offsetX * RUN_SPEED) * DisplayManager.getFrameTimeSeconds());
position.x -= ((offsetZ * RUN_SPEED) * DisplayManager.getFrameTimeSeconds());
}
else
{
position.x = position.x;
position.z = position.z;
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE))
{
jump();
}
}
}

public void invertPitch(){
this.pitch = -pitch;
}




public boolean getFPS()
{
return FPS;
}

public Vector3f getPosition()
{
return position;
}

public void setPosition(Vector3f position)
{
this.position = position;
}

public float getPitch()
{
return pitch;
}

public float getYaw()
{
return yaw;
}

public float getRoll()
{
return roll;
}

private void calculateCameraPosition(float horizDistance,
float verticDistance)
{
float theta = player.getRotY() + angleAroundPlayer;
float offsetX = (float) (horizDistance * Math
.sin(Math.toRadians(theta)));
float offsetZ = (float) (horizDistance * Math
.cos(Math.toRadians(theta)));
position.x = player.getPosition().x - offsetX;
position.z = player.getPosition().z - offsetZ;
position.y = player.getPosition().y + verticDistance;
}

private float calculateHorizontalDistance()
{
return (float) (distanceFromPlayer * Math.cos(Math.toRadians(pitch)));
}

private float calculateVerticalDistance()
{
return (float) (distanceFromPlayer * Math.sin(Math.toRadians(pitch)));
}

private void calculateZoom()
{
float zoomLevel = Mouse.getDWheel() * 0.1f;
distanceFromPlayer -= zoomLevel;
}

private void calculatePitch()
{
if (Mouse.isButtonDown(0))
{
float pitchChange = Mouse.getDY() * 0.1f;
pitch -= pitchChange;
}
}

private void calculateAngleAroundPlayer()
{
if (Mouse.isButtonDown(0))
{
float angleChange = Mouse.getDX() * 0.3f;
angleAroundPlayer -= angleChange;
}
}
}

最佳答案

其实很简单。 首先,将move() 方法更改为如下所示:

 public void move() {
calculatePitch();
calculateAngleAroundPlayer();
this.yaw = 180 - (player.getRotY() + angleAroundPlayer);
yaw %= 360;
}

其次,将distanceFromPlayer 更改为0

private float distanceFromPlayer = 0;

希望这有帮助!(可能会)

关于java - 如何让我的第一人称相机流畅?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34843398/

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