问题描述
|
当我控制1轴的飞船时,一切都很好,即,深度(z)和z平面上的旋转360度,即2轴。我后面也有一个摄像头,必须保持其位置。当第三名到位时,一切都会变糟。让我给你看一些代码:
这是失败的部分:
飞船的绘制方法:
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);
}
}
最后是Game类更新的方法:
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角度方法具有固有的弱点。
我的建议是您转移范例,并考虑以矩阵或四元数形式存储和操纵方向。一旦学会了操纵矩阵或四元数,您尝试要做的就变得非常简单。