使用LWJGL创建相机并需要停止旋转

时间:2013-10-01 23:11:11

标签: java opengl camera

我正在创建一个游戏,我想尽可能少地提供代码,即使它只是准备阶段。

基本上,我已经创建了一个相机,并且能够向用户想要的左右两边看(当我到达窗口边缘时我重置了鼠标x的位置),但我是什么d like是玩家只能沿着球场上下移动90度(x旋转)。

以下是MouseControl.java文件中的一些代码,我认为它应该是其中的一部分:

public static void mouseMove(Camera cam, int x, int y)
{
    int originX = Display.getWidth() / 2;
    int originY = Display.getHeight() / 2;
    int curY = Mouse.getY();
    int curX = Mouse.getX();

    boolean canMoveDown = true;
    boolean canMoveUp = true;

    if (isMouseRight() || isMouseLeft())
    {
        Mouse.setCursorPosition(originX, curY);
        x = originX;
    }

    if (isMouseTop() || isMouseBottom())
    {
        Mouse.setCursorPosition(curX, originY);
        y = originY;
    }

    if (!isMouseStill(x, y))
    {
        // Mouse is to the left of the old position
        if (Mouse.getX() < x)
        {
            if (Mouse.getX() < (x - ((1 / 10) * Display.getWidth())))
            {
                cam.rotateY(-0.35f);
            }
            if (Mouse.getX() < (x - ((2 / 10) * Display.getWidth())))
            {
                cam.rotateY(-0.40f);
            }
            if (Mouse.getX() < (x - ((3 / 10) * Display.getWidth())))
            {
                cam.rotateY(-0.45f);
            }
            if (Mouse.getX() < (x - ((4 / 10) * Display.getWidth())))
            {
                cam.rotateY(-0.50f);
            }
            if (Mouse.getX() < (x - ((5 / 10) * Display.getWidth())))
            {
                cam.rotateY(-0.55f);
            }
        }

        // Mouse is to the right of the old position
        if (Mouse.getX() > x)
        {
            if (Mouse.getX() > (x + ((1 / 10) * Display.getWidth())))
            {
                cam.rotateY(0.35f);
            }
            if (Mouse.getX() > (x + ((2 / 10) * Display.getWidth())))
            {
                cam.rotateY(0.40f);
            }
            if (Mouse.getX() > (x + ((3 / 10) * Display.getWidth())))
            {
                cam.rotateY(0.45f);
            }
            if (Mouse.getX() > (x + ((4 / 10) * Display.getWidth())))
            {
                cam.rotateY(0.50f);
            }
            if (Mouse.getX() > (x + ((5 / 10) * Display.getWidth())))
            {
                cam.rotateY(0.55f);
            }
        }

        // Player pitch (X axis rotation) is between 90 and -90
        // If the player can move up and down (head angle)
        if (canMoveDown && canMoveUp)
        {
            if (y == 90)
            {
                canMoveUp = false;
            }
            else if (y == -90)
            {
                canMoveDown = false;
            }
            else
            {
                // Mouse is above the old position
                if (Mouse.getY() > y)
                {
                    if (Mouse.getY() > (y + ((1 / 10) * Display.getHeight())))
                    {
                        cam.rotateX(0, -0.35f);
                    }
                    if (Mouse.getY() > (y + ((2 / 10) * Display.getHeight())))
                    {
                        cam.rotateX(0, -0.40f);
                    }
                    if (Mouse.getY() > (y + ((3 / 10) * Display.getHeight())))
                    {
                        cam.rotateX(0, -0.45f);
                    }
                    if (Mouse.getY() > (y + ((4 / 10) * Display.getHeight())))
                    {
                        cam.rotateX(0, -0.50f);
                    }
                    if (Mouse.getY() > (y + ((5 / 10) * Display.getHeight())))
                    {
                        cam.rotateX(0, -0.55f);
                    }
                }

                // Mouse is below the old position
                if (Mouse.getY() < y)
                {
                    if (Mouse.getY() < (y - ((1 / 10) * Display.getHeight())))
                    {
                        cam.rotateX(0, 0.35f);
                    }
                    if (Mouse.getY() < (y - ((2 / 10) * Display.getHeight())))
                    {
                        cam.rotateX(0, 0.40f);
                    }
                    if (Mouse.getY() < (y - ((3 / 10) * Display.getHeight())))
                    {
                        cam.rotateX(0, 0.45f);
                    }
                    if (Mouse.getY() < (y - ((4 / 10) * Display.getHeight())))
                    {
                        cam.rotateX(0, 0.50f);
                    }
                    if (Mouse.getY() < (y - ((5 / 10) * Display.getHeight())))
                    {
                        cam.rotateX(0, 0.55f);
                    }
                }
            }
        }
        else if (canMoveDown == true && canMoveUp == false)
        {
            // Mouse is below the old position
            if (Mouse.getY() < y)
            {
                if (Mouse.getY() < (y - ((1 / 10) * Display.getHeight())))
                {
                    cam.rotateX(0, 0.35f);
                    canMoveUp = true;
                }
                if (Mouse.getY() < (y - ((2 / 10) * Display.getHeight())))
                {
                    cam.rotateX(0, 0.40f);
                    canMoveUp = true;
                }
                if (Mouse.getY() < (y - ((3 / 10) * Display.getHeight())))
                {
                    cam.rotateX(0, 0.45f);
                    canMoveUp = true;
                }
                if (Mouse.getY() < (y - ((4 / 10) * Display.getHeight())))
                {
                    cam.rotateX(0, 0.50f);
                    canMoveUp = true;
                }
                if (Mouse.getY() < (y - ((5 / 10) * Display.getHeight())))
                {
                    cam.rotateX(0, 0.55f);
                    canMoveUp = true;
                }
            }
        }
        else if (canMoveUp == true && canMoveDown == false)
        {
            // Mouse is above the old position
            if (Mouse.getY() > y)
            {
                if (Mouse.getY() > (y + ((1 / 10) * Display.getHeight())))
                {
                    cam.rotateX(0, -0.35f);
                    canMoveDown = true;
                }
                if (Mouse.getY() > (y + ((2 / 10) * Display.getHeight())))
                {
                    cam.rotateX(0, -0.40f);
                    canMoveDown = true;
                }
                if (Mouse.getY() > (y + ((3 / 10) * Display.getHeight())))
                {
                    cam.rotateX(0, -0.45f);
                    canMoveDown = true;
                }
                if (Mouse.getY() > (y + ((4 / 10) * Display.getHeight())))
                {
                    cam.rotateX(0, -0.50f);
                    canMoveDown = true;
                }
                if (Mouse.getY() > (y + ((5 / 10) * Display.getHeight())))
                {
                    cam.rotateX(0, -0.55f);
                    canMoveDown = true;
                }
            }
        }
    }
}

如果您需要更多属于此的代码,请询问并发布,我真的需要解决此问题。

这是我的Camera.java类:

package com.RedJax.Game;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;

import org.lwjgl.input.Mouse;


public class Camera 
{
    // Position Variables
    private float x;
    private float y;
    private float z;

    // Rotation Variables
    private float pitch;//Rotation X
    private float yaw;//Rotation Y
    private float roll;//Rotation Z

    // Vision Variables
    private float fov;//Field of View
    private float aspect;//Aspect Ratio
    private float near;//Near Clip
    private float far;//Render Distance

    public Camera(float fieldOfView, float aspectRatio, float nearClip, float renderDist)
    {
        x = 0;
        y = 0;
        z = 0;

        pitch = 0;
        yaw = 0;
        roll = 0;

        fov = fieldOfView;
        aspect = aspectRatio;
        near = nearClip;
        far = renderDist;

        initProjection();
    }

    public void useView()
    {
        glRotatef(pitch, 1, 0, 0);
        glRotatef(yaw, 0, 1, 0);
        glRotatef(roll, 0, 0, 1);
        glTranslatef(x, y, z);
    }

    private void initProjection()
    {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(fov, aspect, near, far);
        glMatrixMode(GL_MODELVIEW);

        glEnable(GL_DEPTH_TEST);
        glEnable(GL_TEXTURE_2D);
    }

    public float getX()
    {
        return x;
    }
    public float getY()
    {
        return y;
    }
    public float getZ()
    {
        return z;
    }
    public float getPitch()
    {
        return pitch;
    }
    public float getYaw()
    {
        return yaw;
    }
    public float getRoll()
    {
        return roll;
    }
    public void setX(float x)
    {
        this.x = x;
    }
    public void setY(float y)
    {
        this.y = y;
    }
    public void setZ(float z)
    {
        this.z = z;
    }
    public void setPitch(float x)
    {
        this.pitch = x;
    }
    public void setYaw(float y)
    {
        this.yaw = y;
    }
    public void setRoll(float z)
    {
        this.roll = z;
    }

    public void move(Direction dir, float amount)
    {
        int intDir = dir.getDirection();

        if (intDir != 0)
        {
            z += amount * Math.sin(Math.toRadians(yaw + 90 * intDir));
            x += amount * Math.cos(Math.toRadians(yaw + 90 * intDir));
        }
        else
        {
            z += Direction.getLeftRight(amount, dir) * Math.sin(Math.toRadians(yaw + 90 * intDir));
            x += Direction.getLeftRight(amount, dir) * Math.cos(Math.toRadians(yaw + 90 * intDir));
        }
    }

    public void vert(float amount)
    {
        y += amount; 
    }

    public void rotateY(float amount)
    {
        yaw += amount;

        if (yaw == 360)
        {
            yaw = 0;
        }

        if (yaw == -360)
        {
            yaw = 0;
        }
    }

    public void rotateX(float dir, float amount)
    {
        if ((pitch >= -90) && (pitch <= 90))
        {
            pitch += amount;
        }
        else
        {
            //System.out.println(Mouse.getY());
        }
    }
}

0 个答案:

没有答案