我在這裏遇到了一些問題,希望得到一些幫助。我試圖找到一個旋轉角度,讓我能夠讓對象跟隨玩家(始終面對它)。 我第一次嘗試做交叉產品,點和旋轉,但只是不能正常工作。 然後我開始將它分解爲兩個旋轉(偏航和俯仰)和鎖定滾動。它現在可行,但僅適用於某些方向(我相信四分之一圓,但可能更少)。 這是我當前的代碼:關注3D中的對象
XMVECTOR newDir = Vec3DtoDX((Player->Position - InternalEntity->Position).getNormalized());
XMVECTOR orig = XMVectorSet(0,0,1,0);
XMVECTOR xNewDir,xOrigDir,yNewDir,yOrigDir;
xOrigDir = XMVectorSet(0,1,0,0); // (y,z)
yOrigDir = XMVectorSet(0,1,0,0); // (x,z)
xNewDir = XMVectorSet(newDir.m128_f32[1],newDir.m128_f32[2],0,0);
yNewDir = XMVectorSet(newDir.m128_f32[0],newDir.m128_f32[2],0,0);
float xAngle = XMVector2AngleBetweenVectors(xNewDir,xOrigDir).m128_f32[0];
float yAngle = XMVector2AngleBetweenVectors(yNewDir,yOrigDir).m128_f32[0];
XMVECTOR rotDX = XMQuaternionRotationRollPitchYaw(xAngle,yAngle,0);
PxQuat rot = VecDXto4D<PxQuat>(rotDX);
這裏的正常工作,如果我靠近面對對象的Z軸 http://imgur.com/oNNNRXo
如果我多運動,事實證明,這樣的事情: http://imgur.com/xFIEzdg
任何幫助將不勝感激。
編輯: 我已經嘗試從十字架和點的角度創建四元數。事實上,這是我第一次。問題是它無法正常工作。下面是我如何做它:
PxVec3 newDir = (Player->Position - InternalEntity->Position).getNormalized();
PxVec3 orig(0,0,1);
PxVec3 axis = newDir.cross(orig);
float angle = XMVector3AngleBetweenVectors(Vec3DtoDX(newDir),Vec3DtoDX(orig)).m128_f32[0];
PxQuat rot(angle,axis);
其中XMVector3AngleBetweenVectors是:
XMVECTOR L1 = XMVector3ReciprocalLength(V1);
XMVECTOR L2 = XMVector3ReciprocalLength(V2);
XMVECTOR Dot = XMVector3Dot(V1, V2);
L1 = XMVectorMultiply(L1, L2);
XMVECTOR CosAngle = XMVectorMultiply(Dot, L1);
CosAngle = XMVectorClamp(CosAngle, g_XMNegativeOne.v, g_XMOne.v);
return XMVectorACos(CosAngle);
這將導致以下屏幕: http://imgur.com/vlMPAwG 人和:http://imgur.com/PEz1aMc
任何幫助嗎?
你能看到我編輯的問題嗎? –
問題是,您始終使用相同的原始矢量,並且應該隨着對象的旋轉更改而改變。您必須使用模型將此原始矢量轉換爲世界矩陣。 –
但是,如果我將原始數據轉換爲世界空間,是否需要與先前的旋轉相乘,因爲它將相對於之前的方向? –