我想寫一個函數,我可以在三維空間中將網格投影到一個點上。C#託管DirectX - 我如何旋轉網格到任何矢量?
但我無法找到我的解決方案的任何示例。
它應該看起來像下面的代碼:
public void RotateMesh(Vector3 MeshPos, Vector3 TargedPos)
{
... Do something
}
但我不知道我怎麼可以旋轉MeshPos到TargetPos。
如果有人能解釋它會很好。
我想我需要一個Matrix作爲返回值,因爲我需要爲我的SelectedPoint矩陣的繪製函數添加一個Matrix。
繪圖功能:
Matrix SelectedPoint = Matrix.Scaling(0.1f, 0.1f, 0.1f) * Matrix.Translation(IntersectVector) * OldMatrix;
gameengine.m_Device.Transform.World = SelectedPoint;
for (int i = 0; i < Burg.MeshObject.NumberAttributes; i++)
{
gameengine.m_Device.Material = Burg.material[i];
gameengine.m_Device.SetTexture(0, Burg.texture[i]);
Burg.MeshObject.DrawSubset(0);
}
public Matrix RotateMesh(Vector3 MeshPos, Vector3 TargedPos)
{
... Do something
return AMatrix;
}
那麼我tryed以下,但財產以後已經倒..
Vector3 target =new Vector3(0.0001f, 0.0001f, 0.0001f);
Vector3 vFront = target - IntersectVector;
vFront.Normalize();
Vector3 vLeft = Vector3.Cross(vFront, new Vector3(0, 1, 0));
vLeft.Normalize();
Vector3 vUp = Vector3.Cross(vLeft, vFront);
vUp.Normalize();
Matrix mBase = Matrix.Identity;
mBase.M11 = vLeft.X;
mBase.M21 = vLeft.Y;
mBase.M31 = vLeft.Z;
mBase.M12 = vUp.X;
mBase.M22 = vUp.Y;
mBase.M32 = vUp.Z;
mBase.M13 = vFront.X;
mBase.M23 = vFront.Y;
mBase.M33 = vFront.Z;
Matrix TestMatrix = gameengine.camera._viewMatrix;
TestMatrix.Invert();
gameengine.m_Device.RenderState.DitherEnable = true;
gameengine.m_Device.RenderState.ZBufferEnable = true;
gameengine.m_Device.VertexFormat = SEarth.MeshObject.VertexFormat;
gameengine.m_Device.RenderState.CullMode = Cull.None;
if (IntersectVector != Vector3.Empty)
{
Matrix SelectedPoint = Matrix.Scaling(0.1f, 0.1f, 0.1f) * mBase * Matrix.Translation(IntersectVector) * OldMatrix;
現在好了,我tryed其他的東西..但網不旋轉,變形!怎麼了?
OldMatrix *= Matrix.RotationX(CubeRotX) * Matrix.RotationY(CubeRotY);
Vector3 target =new Vector3(0.0001f, 0.0001f, 0.0001f);
Vector3 vFront = target - IntersectVector;
vFront.Normalize();
Vector3 vLeft = Vector3.Cross(vFront, new Vector3(0, 1, 0));
vLeft.Normalize();
Vector3 vUp = Vector3.Cross(vLeft, vFront);
vUp.Normalize();
Matrix mBase = Matrix.Identity;
mBase.M11 = vLeft.X;
mBase.M12 = vUp.X;
mBase.M13 = vFront.X;
mBase.M14 = 0.0f;
mBase.M21 = vLeft.Y;
mBase.M22 = vUp.Y;
mBase.M23 = vFront.Z;
mBase.M24 = 0.0f;
mBase.M31 = vLeft.Z;
mBase.M32 = vUp.Z;
mBase.M33 = vFront.Z;
mBase.M34 = 0.0f;
//mBase.M41 = IntersectVector.X;
//mBase.M42 = IntersectVector.Y;
//mBase.M43 = IntersectVector.Z;
mBase.M41 = 0;
mBase.M42 = 0;
mBase.M43 = 0;
mBase.M44 = 1.0f;
if (IntersectVector != Vector3.Empty)
{
Matrix SelectedPoint = Matrix.Scaling(0.1f, 0.1f, 0.1f) * mBase * Matrix.Translation(IntersectVector) * OldMatrix;
gameengine.m_Device.Transform.World = SelectedPoint;
for (int i = 0; i < Burg.MeshObject.NumberAttributes; i++)
{
gameengine.m_Device.Material = Burg.material[i];
gameengine.m_Device.SetTexture(0, Burg.texture[i]);
Burg.MeshObject.DrawSubset(0);
}
}
左手和右手矩陣有什麼不同?我想我是非常棒的。
我應該解釋一下我PROGRAMM確定它不會工作.. ..有與位置「的Vector3(0,0,0)」
如果我在球上點擊某處..一城一球將放置在相交座標上。
目前的最佳解決方案是創建LookAtL矩陣,但城堡不會面對相交向量中的向量(0,0,0)。
更新2013年5月1日
Planet with Wire and the Castle on the Click position
Planet with Solid and the Castle on the Click position
這裏是當前功能
gameengine.m_Device.RenderState.FillMode = FillMode.Solid;
OldMatrix *= Matrix.RotationX(CubeRotX) * Matrix.RotationY(CubeRotY);
Vector3 target =new Vector3(0.0001f, 0.0001f, 0.0001f);
Vector3 vFront = target - IntersectVector;
vFront.Normalize();
Vector3 vLeft = Vector3.Cross(vFront, new Vector3(0, 1, 0));
vLeft.Normalize();
Vector3 vUp = Vector3.Cross(vLeft, vFront);
vUp.Normalize();
Vector3 vRigth = Vector3.Cross(vUp, vFront);
Matrix mBase = Matrix.Identity;
mBase.M11 = vLeft.X;
mBase.M12 = vUp.X;
mBase.M13 = vFront.X;
mBase.M21 = vLeft.Y;
mBase.M22 = vUp.Y;
mBase.M23 = vFront.Z;
mBase.M31 = vLeft.Z;
mBase.M32 = vUp.Z;
mBase.M33 = vFront.Z;
//Matrix ObjectMatrix = Matrix.LookAtLH(IntersectVector, new Vector3(0, 0, 0), new Vector3(0, 1, 0));
Matrix ObjectMatrix = Matrix.LookAtLH(vFront, vUp, vLeft);
if (IntersectVector != Vector3.Empty)
{
// Translation * Base * Rotation * Scaling
Matrix SelectedPoint = Matrix.Identity * ObjectMatrix *Matrix.Scaling(0.3f, 0.3f, 0.3f) * Matrix.Translation(IntersectVector) *OldMatrix;
//SelectedPoint.M41 = IntersectVector.X;
//SelectedPoint.M42 = IntersectVector.Y;
//SelectedPoint.M43 = IntersectVector.Z;
//SelectedPoint *= OldMatrix;
gameengine.m_Device.Transform.World = SelectedPoint;
for (int i = 0; i < Burg.MeshObject.NumberAttributes; i++)
{
gameengine.m_Device.Material = Burg.material[i];
gameengine.m_Device.SetTexture(0, Burg.texture[i]);
Burg.MeshObject.DrawSubset(0);
}
}
Material deviceMat = gameengine.m_Device.Material;
//gameengine.m_Device.Material.AmbientColor = ColorValue.FromArgb(161613);
//testsphere.DrawSubset(0);
gameengine.m_Device.Material = deviceMat;
//gameengine.m_Device.RenderState.FillMode = FillMode.WireFrame;
你真的是旋轉?或者你想要一個網格來面對目標位置?因此,您可以使用基礎變換,您可以將網格和目標之間的矢量作爲前部,向上矢量和左右兩邊的交叉點構建。 – Gnietschow 2013-04-29 22:10:03
嗨Gnietschow,是的,我的意思是面對目標位置的網格。以及杜ü意味着與「網和taget之間的向量作爲正面且載體」,我需要從MeshVector.left和TargetVector.Left跨MeshVector.Up和TargetVector.Up的dotproduct? – ZeraTFK 2013-04-29 22:20:39
我的意思是'Front =(目標位置).Normalize; Left = Cross(Front,Vector3(0,1,0))。 Up = Cross(Left,Front).Normalize;'在這個基礎上,你可以建立一個基礎變換的矩陣,它將你的網格映射到正確的方向。 – Gnietschow 2013-04-29 22:26:28