LWJGL3根据其自身旋转移动实体



我想根据自己的方向移动对象。该对象具有翻译向量和旋转矢量(度)。我可以调用方法移动并给它一个方向,并且应该移动对象的单位(如果您愿意的话,速度)。如果我给该方法一个方向"向前",而不是朝对象当前面向的方向移动。

我当前有此代码:

/*
 * MIT License
 * 
 * Copyright (c) 2017 Ralph Niemitz
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package de.ralleytn.engine.lunatic;
import javax.vecmath.Vector3f;
/**
 * Merges rotation with translation and adds translation based on rotation.
 * @author Ralph Niemitz/RalleYTN(ralph.niemitz@gmx.de)
 * @version 1.0.0
 * @since 1.0.0
 */
public interface Movable extends Translatable, Rotatable {
    /**
     * Moves the object into the given direction based on its own rotation.
     * @param direction the direction in which the object should be moved
     * @param units the number of units it should be moved
     * @since 1.0.0
     */
    public default void move(Direction direction, float units) {
        float nUnits = -units;
        Vector3f rotation = this.getRotation();
        if(direction == Direction.LEFT) {
            float rY = (float)Math.toRadians(rotation.y);
            float rZ = (float)Math.toRadians(rotation.z);
            float x = (float)Math.cos(rY) * nUnits;
            float y = (float)Math.sin(rZ) * units;
            float z = (float)Math.sin(rY) * nUnits;
            this.translate(x, y, z);
        } else if(direction == Direction.RIGHT) {
            // float rX = (float)Math.toRadians(rotation.x);
            float rY = (float)Math.toRadians(rotation.y);
            float rZ = (float)Math.toRadians(rotation.z);
            float x = (float)Math.cos(rY) * units;
            float y = (float)Math.sin(rZ) * nUnits;
            float z = (float)Math.sin(rY) * units;
            this.translate(x, y, z);
        } else if(direction == Direction.FORWARD) {
            float rX = (float)Math.toRadians(rotation.x);
            float rY = (float)Math.toRadians(rotation.y - 90.0F);
            float x = (float)Math.cos(rY) * units;
            float y = (float)Math.sin(rX) * nUnits;
            float z = (float)Math.sin(rY) * units;
            this.translate(x, y, z);
        } else if(direction == Direction.BACKWARD) {
            float rX = (float)Math.toRadians(rotation.x);
            float rY = (float)Math.toRadians(rotation.y - 90.0F);
            float x = (float)Math.cos(rY) * nUnits;
            float y = (float)Math.sin(rX) * units;
            float z = (float)Math.sin(rY) * nUnits;
            this.translate(x, y, z);
        } else if(direction == Direction.UP) {
            float rX = (float)Math.toRadians(rotation.x);
            float rZ = (float)Math.toRadians(rotation.z);
            float x = (float)Math.sin(rZ) * nUnits;
            float y = (float)Math.cos(rX) * units;
            float z = (float)Math.sin(rX) * nUnits;
            this.translate(x, y, z);
        } else if(direction == Direction.DOWN) {
            float rX = (float)Math.toRadians(rotation.x);
            float rZ = (float)Math.toRadians(rotation.z);
            float x = (float)Math.sin(rZ) * units;
            float y = (float)Math.cos(rX) * nUnits;
            float z = (float)Math.sin(rX) * units;
            this.translate(x, y, z);
        }
    }
}

它并不是我想要的。我是3D的新手,所以我没有那么多知识。到目前为止,我看到的所有解决方案都是为了团结而C#,这并没有真正帮助我。

我正在使用javax.vecmath软件包,如果很重要。

有不同的方法来完成您的要求。

其中之一应该是维护前向量,以及在对象实例中旋转的表示。可能应使用 vec3(0,0,1);

初始化向量向量

我不知道Vector3f rotation = this.getRotation();是什么是欧拉角(Yaw,俯仰和滚动),您应该避免在自我上进行三角学,并使用一些预定义的功能,可以根据这些角度根据这些角度来计算旋转矩阵。/p>

您只能从rx开始,然后在诱人中使用RY,并使用某些方法,例如: glRotatef(value, 0, 1, 0); for ry

glRotatef(value, 1, 0, 0); for rx

现在,每次旋转时,您都应该更新这些旋转值。您从旋转矩阵开始,然后将此旋转矩阵应用于您的 initialforwardvector

m_rotation = Matrix4f.identity().rotateX(Rx).rotateY(Ry);
m_forward  = m_rotation * vec3(0, 0, 1);

现在,当您判断翻译时间

vec3 translationVector = speed * m_forward;
this.translate(translationVector.x, translationVector.y, translationVector.z);

如果要左走,可以通过在旋转矩阵的偏航中添加90°来计算左矢量。我不确定有时候在视频游戏中您想要的是,最好只在XZ飞机上移动。

Matrix4f rotation = Matrix4f.identity().rotateX(Rx /* maybe 0 */).rotateY(Ry + 1.57);
m_left = m_rotation * vec3(0, 0, 1);

您还可以计算您的初始左侧矢量,删除上面的代码并使用相同的旋转矩阵,但应用于左矢量,您的性能将更好。只需尝试与您的初始Forward一起垂直您的InitleftVector。

vec3 initialLeft = vec3(1, 0, 0);
m_left = m_rotation * initialLeft;

最新更新