2016-10-17 18 views
0

レイキャスティング計算に多くの問題があり、問題がどこにあるのか分かりません。私はDirectXのMathライブラリを使ってベクトルなどを作成しています。ここでDirectX11でのC++レイキャスティング

は、これまでの私のコードです:私は、正規化デバイス座標が正しいことを確認している

auto normalizedDeviceCoords = glm::vec2((float(a_frontend->GetMousePos().x)/float(a_frontend->GetWidth())) * 2.f - 1.f, 
             -((float(a_frontend->GetMousePos().y)/float(a_frontend->GetHeight())) * 2.f - 1.f)); 
auto mouseOrigin = DirectX::XMVectorSet(normalizedDeviceCoords.x, normalizedDeviceCoords.y, 0.0f, 1.0f); 
auto mouseEnd = DirectX::XMVectorSet(normalizedDeviceCoords.x, normalizedDeviceCoords.y, 1.0f, 1.0f); 

auto viewproj = DirectX::XMMatrixMultiply(a_frontend->GetViewMatrix(), a_frontend->GetProjMatrix()); 
auto determinant = DirectX::XMMatrixDeterminant(a_camera.ViewProj()); 
auto inverseviewproj = DirectX::XMMatrixInverse(&determinant, a_camera.ViewProj()); 
auto rayOrigin = DirectX::XMVector4Transform(mouseOrigin, inverseviewproj); 
auto rayEnd = DirectX::XMVector4Transform(mouseEnd, inverseviewproj); 
auto raySubtraction = DirectX::XMVectorSubtract(rayEnd, rayOrigin); 
auto rayDirection = DirectX::XMVector3Normalize(raySubtraction); 

auto planeNormal = DirectX::XMVectorSet(0.f, 1.f, 0.f, 0.f); 
auto pointOnPlane = DirectX::XMVectorSet(0.f, -0.1f, 0.f, 0.f); 

DirectX::XMFLOAT3 denominator; 
DirectX::XMStoreFloat3(&denominator, DirectX::XMVector3Dot(planeNormal, rayDirection)); 
if (fabs(denominator.x) <= 0.0001f) 
{ 
    return; 
} 

auto pointMinusRay = DirectX::XMVectorSubtract(pointOnPlane, rayOrigin); 

DirectX::XMFLOAT3 t; 
auto almostT = DirectX::XMVector3Dot(pointMinusRay, planeNormal); 
DirectX::XMStoreFloat3(&t, DirectX::XMVectorScale(almostT, 1.f/denominator.x)); 

if (t.x < 0) 
{ 
    return; 
} 

auto rayDirectionLength = DirectX::XMVector3Length(rayDirection); 
auto rayPoint = DirectX::XMVectorAdd(rayOrigin, DirectX::XMVectorScale(rayDirection, t.x)); 

、線方向の長さも一つであり、かつ光線方向は、によって伝えるのは難しい(正しいように見えます数字だけ)。

すべての情報が役立ちます。私は既にスタックオーバーフローや他のフォーラムでいくつかの情報を見てきましたが、どこにもいませんでした。

+0

平面上にある光線の点は、y軸上でのみ正しいことを忘れています(正しい-0.1です)。 xとzの位置は5でなければなりませんが、0.1です。 –

+0

これらのテストで '' DirectXCollision.h''関数を試しましたか?また、 '' .cpp''ファイルで ''名前空間DirectX; ''を使うことを検討してください。 –

+0

私はそのヘッダーをチェックして、そこに何らかの有用な機能がないかどうかを確認します。チャックに感謝します。しかし、私は前のプログラマーのスタイルをコーディングして、コードを流暢にしようとしています。 –

答えて

0

私のチームメイトはこの問題を解決できました。彼はちょうどDirectX Math関数Vector3Unprojectを使い始めました。これは、マウスの座標を取って、原点と終点の両方を使用してワールド座標に変換します(したがって、z座標が0、次に1のマウス座標を取る)。次に、それらを互いに引き離して、世界のどこかを指すベクトルを取得します。その方向をカメラの現在のy位置を方向ベクトルのyの負値で割った値に変換することができました。ここにすべてのコードがあります:

DirectX::XMVECTOR mouseNear = DirectX::XMVectorSet((float)a_mousePos.x, (float)a_mousePos.y, 0.0f, 0.0f); 
DirectX::XMVECTOR mouseFar = DirectX::XMVectorSet((float)a_mousePos.x, (float)a_mousePos.y, 1.0f, 0.0f); 
DirectX::XMVECTOR unprojectedNear = DirectX::XMVector3Unproject(mouseNear, 0, 0, a_width, a_height, a_nearZ, a_farZ, 
                   a_projection, a_view, DirectX::XMMatrixIdentity()); 
DirectX::XMVECTOR unprojectedFar = DirectX::XMVector3Unproject(mouseFar, 0, 0, a_width, a_height, a_nearZ, a_farZ, 
                   a_projection, a_view, DirectX::XMMatrixIdentity()); 
DirectX::XMVECTOR result = DirectX::XMVector3Normalize(DirectX::XMVectorSubtract(unprojectedFar, unprojectedNear)); 
DirectX::XMFLOAT3 direction; 
DirectX::XMStoreFloat3(&direction, result); 
return direction; 

//Get the distance to the ground. 
DirectX::XMFLOAT3 cameraPosition = a_camera.GetPosition(); 

//Get the point on the ground. 
cameraPosition.x += direction.x * (cameraPosition.y/-direction.y); 
cameraPosition.z += direction.z * (cameraPosition.y/-direction.y); 

将来的にはこれが役立つことが期待されます。

関連する問題