2017-11-26 37 views
0

オブジェクトをその方向に基づいて移動したい。 オブジェクトには、平行移動ベクトルと回転ベクトル(度)があります。 私はメソッドの移動を呼び出すことができますし、方向とオブジェクトを移動する必要があります(スピードを望む場合)。このメソッドに、オブジェクトが現在向いている方向に移動するよりも「進む」方向を与えるとします。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([email protected]) 
* @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の新しいので、あまり知識がありません。 これまで私が見てきた解決策は、UnityとC#のためのもので、本当に助けにはなりません。

重要な場合は、javax.vecmathパッケージを使用しています。

+0

そこでは特に最後の3つのリンク(https://stackoverflow.com/a/28084380/2521214)[理解4x4の均質な変換行列]を参照してください... – Spektre

答えて

0

あなたが求めていることを達成するさまざまな方法があります。

これらのうちの1つは、前方ベクトルと、オブジェクトインスタンス内での回転の表現を維持することです。順方向ベクトルは、vec3(0,0,1)で初期化されるべきである。

わかりませんVector3f rotation = this.getRotation();オイラー角(ヨー、ピッチ、ロール)の場合は、自分で三角法を実行しないようにして、これらの角度に基づいて回転行列を計算できる所定の関数を使用してください。hereあなたが更新する必要があります回転を達成今たびに、Rxの

ため Ryの

ため glRotatef(value, 0, 1, 0);

glRotatef(value, 1, 0, 0);

あなたはbeguiningにのみRxおよびRyを始めて、のようないくつかの方法を使用することができますそれらの回転値。 回転行列で始まり、この回転行列をInitialForwardvectorに適用します。

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

あなたはあなたがあなたの回転行列のヨーに90°を追加することで、左ベクトルを計算することができ、左に行きたい場合は

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

を翻訳するその時間を判断します。私はそれがあなたがビデオで時々欲しいものであるかどうかはわかりません.XZ平面上を移動するほうがいいです。

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

あなたは、あなたがより良い性能を持つことになります、また、あなたの最初の左のベクトルを計算し、上記のコードを削除して、同じ回転行列を使用しますが、あなたの左のベクトルに適用することができます。 initLeftVectorをinitialForwardオブジェクトと垂直にしてみてください。

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

私は本当にglRotatefのような非推奨メソッドを使用しないようにしたいです()。 私はOpenGL4.6とプログラマブルなPipeline(Shaders and stuff)を扱います。 はい回転はオイラーです。 javax.vecmathには多くの機能がありません。私は自分自身がいくつかの古いLWJGL2コードをコピーすることを余儀なくされるのを見ました。 – RalleYTN

+0

私はまだ、これらの前方、左などのベクターがどのように機能するのか完全に理解していません。 それはカメラを動かすのではなく、すべての軸の回転に基づいて3D世界のオブジェクトを動かすことです。 正確な内容と正しく計算する方法を説明する記事がありますか?あなたがリンクした記事は、私がすでに適用した変換と投影の基礎を私に示しました。 – RalleYTN

+0

すべてのオブジェクトがそのすべての変換が配置されている独自のモデルマトリックスを持っていると良いでしょう。各オブジェクトには独自の回転行列とinitialForwardVectorが必要です。この方法でWorldSpaceで順方向を計算し、カメラの前に同じスペースで翻訳することができます。私は自分自身を打ちのめす人ですが、さまざまなゲームで使用される良いコンセプトは、オブジェクトが他人からの変形を継承するシーングラフです。https://www.opengl.org/discussion_boards/showthread.php/177194-What-is-a-シーングラフ –

関連する問題