我想写一个Funktion,我可以将网格转换为3D空间中的点。
但是我找不到任何解决方案的例子。
它应该类似于以下代码:
public void RotateMesh(Vector3 MeshPos, Vector3 TargedPos)
{
... Do something
}
但我不知道如何将MeshPos旋转到TargetPos。
如果有人能解释它会很好。
我想我需要一个矩阵作为返回值,因为我需要在我的SelectedPoint矩阵的绘图函数中添加一个矩阵。
绘制功能:
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;
}
我尝试了下面的内容,但是事情已经倒了..
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;
现在我尝试了其他东西......但是网格确实会旋转并变形!怎么了?
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);
}
}
左手和右手矩阵有什么不同?我想我有严格的事情。
好吧它不会工作.. 我应该解释一下我的程序..有一个位置为“Vector3(0,0,0)”
的Sphere如果我点击球体上的某个位置......城堡将放置在相交坐标上。
目前最好的解决方案是创建LookAtL矩阵,但Castle不会面对相交矢量中的Vector(0,0,0)。
更新1.5.2013
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;
答案 0 :(得分:0)
要使对象面向特定方向,请使用摄像机视图矩阵。该矩阵将物体从面向观察方向的空间转换为面向z轴的空间。
出于您的目的,您应该反转此矩阵,它会将Z轴映射到用于创建视图矩阵的lookdirection。
伪代码:
ObjectMatrix = Matrix.Invert(Matrix.CreateLookAtLH(dir,up,right));
这将使对象-z轴面向dir方向。
答案 1 :(得分:0)
嗯,我明白了!
这就是我的解决方案:
public Matrix GetRotateToTargetMatrix(Vector3 TargetVector)
{
Matrix mBase = Matrix.Invert(Matrix.LookAtLH(new Vector3(0, 0, 0), TargetVector, new Vector3(0, 1, 0)));
mBase.M44 = 1;
mBase.M14 = 0;
mBase.M24 = 0;
mBase.M34 = 0;
mBase.M41 = 0;
mBase.M42 = 0;
mBase.M43 = 0;
return = mBase;
}
感谢Gnietschow和MHGameWork