C# 在XNA的3个轴上控制一艘宇宙飞船
当我在1轴上控制宇宙飞船时,一切都很好,也就是说,深度(z)和z平面上360度的旋转,所以这是2轴。我也有一个摄像头就在它后面,我必须保持它的位置。当第三个到位时,所有的东西都坏了。让我向您展示一些代码: 以下是失败的部分: 飞船的绘制方法:C# 在XNA的3个轴上控制一艘宇宙飞船,c#,visual-studio,xna,C#,Visual Studio,Xna,当我在1轴上控制宇宙飞船时,一切都很好,也就是说,深度(z)和z平面上360度的旋转,所以这是2轴。我也有一个摄像头就在它后面,我必须保持它的位置。当第三个到位时,所有的东西都坏了。让我向您展示一些代码: 以下是失败的部分: 飞船的绘制方法: public void Draw(Matrix view, Matrix projection) { public float ForwardDirection { get; set; } public float Vert
public void Draw(Matrix view, Matrix projection)
{
public float ForwardDirection { get; set; }
public float VerticalDirection { get; set; }
Matrix[] transforms = new Matrix[Model.Bones.Count];
Model.CopyAbsoluteBoneTransformsTo(transforms);
Matrix worldMatrix = Matrix.Identity;
Matrix worldMatrix2 = Matrix.Identity;
Matrix rotationYMatrix = Matrix.CreateRotationY(ForwardDirection);
Matrix rotationXMatrix = Matrix.CreateRotationX(VerticalDirection); // me
Matrix translateMatrix = Matrix.CreateTranslation(Position);
worldMatrix = rotationYMatrix * translateMatrix;
worldMatrix2 = rotationXMatrix * translateMatrix;
//worldMatrix*= rotationXMatrix;
foreach (ModelMesh mesh in Model.Meshes) //NEED TO FIX THIS
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World =
worldMatrix * transforms[mesh.ParentBone.Index]; ; //position
//effect.World =
//worldMatrix2 * transforms[mesh.ParentBone.Index]; ; //position
effect.View = view; //camera
effect.Projection = projection; //2d to 3d
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;
}
mesh.Draw();
}
}
对于额外的轴,实现了worldMatrix2,我不知道如何与另一个轴组合。我要乘它吗?此外:
摄影机更新方法存在类似的问题:
public void Update(float avatarYaw,float avatarXaw, Vector3 position, float aspectRatio)
{
//Matrix rotationMatrix = Matrix.CreateRotationY(avatarYaw);
Matrix rotationMatrix2 = Matrix.CreateRotationX(avatarXaw);
//Vector3 transformedheadOffset =
//Vector3.Transform(AvatarHeadOffset, rotationMatrix);
Vector3 transformedheadOffset2 = Vector3.Transform(AvatarHeadOffset, rotationMatrix2);
//Vector3 transformedheadOffset2 = Vector3.Transform(transformedheadOffset, rotationMatrix2);
//Vector3 transformedReference =
//Vector3.Transform(TargetOffset, rotationMatrix);
Vector3 transformedReference2 = Vector3.Transform(TargetOffset, rotationMatrix2);
//Vector3 transformedReference2 = Vector3.Transform(transformedReference, rotationMatrix2);
Vector3 cameraPosition = position + transformedheadOffset2; /** + transformedheadOffset; */
Vector3 cameraTarget = position + transformedReference2; /** + transformedReference; */
//Calculate the camera's view and projection
//matrices based on current values.
ViewMatrix =
Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
ProjectionMatrix =
Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(GameConstants.ViewAngle), aspectRatio,
GameConstants.NearClip, GameConstants.FarClip);
}
}
最后是游戏类更新的方法:
spaceship.Update(currentGamePadState,
currentKeyboardState); // this will be cahnged when asteroids are placed in the game, by adding a new parameter with asteroids.
float aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
gameCamera.Update(spaceship.ForwardDirection,spaceship.VerticalDirection,
spaceship.Position, aspectRatio);
在代码中的某个地方,您可能有一种方法来操作和设置变量“ForwardDirection”和“VerticalDirection”,这两个变量似乎分别表示Y轴和X轴周围的角度值。您可能还打算使用一个变量来表示围绕Z轴的角度值。我还假设(你的代码暗示)这些变量最终是你从一帧到另一帧存储宇宙飞船方向的方式 只要你继续尝试用这些角度来表示方向,你就会发现很难控制你的宇宙飞船 有几种类型的方向表示法可用。当涉及到在3维中组合旋转时,3角方法具有固有的弱点
我建议你改变范式,考虑用矩阵或四元数形式存储和操纵你的方向。一旦你学会了操作矩阵或四元数,你要做的事情就变得难以置信的简单。在你的代码中,你可能有一种方法来操作和设置变量“ForwardDirection”和“VerticalDirection”,这两个变量似乎分别代表Y轴和X轴周围的角度值。您可能还打算使用一个变量来表示围绕Z轴的角度值。我还假设(你的代码暗示)这些变量最终是你从一帧到另一帧存储宇宙飞船方向的方式 只要你继续尝试用这些角度来表示方向,你就会发现很难控制你的宇宙飞船 有几种类型的方向表示法可用。当涉及到在3维中组合旋转时,3角方法具有固有的弱点
我建议你改变范式,考虑用矩阵或四元数形式存储和操纵你的方向。一旦你学会了操纵矩阵或四元数,你想做的事情就会变得难以置信的简单。+1当你使用四元数时,一切都变得简单了。花一天时间阅读数学知识。+1当你使用四元数时,一切都变得容易了。花一天时间复习数学。