0
我試過把它們一起旋轉的方法。我試圖將它們添加到相同的位置並添加一個旋轉,但它們具有不同的旋轉軸。如果有人知道更好的xna並找到解決方案,請告訴我。這是我的代碼:如何將相機和模型一起旋轉?
我的相機
// Get the new keyboard and mouse state
MouseState mouseState = Mouse.GetState();
KeyboardState keyState = Keyboard.GetState();
// Determine how much the camera should turn
float deltaX = (float)lastMouseState.X - (float)mouseState.X;
float deltaY = (float)lastMouseState.Y - (float)mouseState.Y;
// Rotate the camera
((FreeCamera)camera).Rotate(deltaX * .003f, deltaY * .003f);
Vector3 translation = Vector3.Zero;
// Determine in which direction to move the camera
if (keyState.IsKeyDown(Keys.W)) translation += Vector3.Forward;
if (keyState.IsKeyDown(Keys.S)) translation += Vector3.Backward;
if (keyState.IsKeyDown(Keys.A)) translation += Vector3.Left;
if (keyState.IsKeyDown(Keys.D)) translation += Vector3.Right;
// Move 3 units per millisecond, independent of frame rate
translation *= 4 * (float)gameTime.ElapsedGameTime.
TotalMilliseconds;
// Move the camera
((FreeCamera)camera).Move(translation);
// Update the camera
camera.Update();
// Update the mouse state
lastMouseState = mouseState;
我的模型類
class CModel
{
public Vector3 Position { get; set; }
public Vector3 Rotation { get; set; }
public Vector3 Scale { get; set; }
public Model Model {get; private set;}
private Matrix[] modelTransforms;
private GraphicsDevice graphicsDevice;
private BoundingSphere boundingSphere;
public CModel(Model Model, Vector3 Position, Vector3 Rotation, Vector3 Scale, GraphicsDevice graphicsDevice)
{
this.Model = Model;
modelTransforms = new Matrix[Model.Bones.Count];
Model.CopyAbsoluteBoneTransformsTo(modelTransforms);
buildBoundingSphere();
this.Position = Position;
this.Rotation = Rotation;
this.Scale = Scale;
this.graphicsDevice = graphicsDevice;
}
public void Draw(Matrix View, Matrix Projection)
{
// Calculate the base transformation by combining
// translation, rotation, and scaling
Matrix baseWorld = Matrix.CreateScale(Scale) * Matrix.CreateFromYawPitchRoll(Rotation.X, Rotation.Y, Rotation.Z) * Matrix.CreateTranslation(Position);
foreach (ModelMesh mesh in Model.Meshes)
{
Matrix localWorld = modelTransforms[mesh.ParentBone.Index] * baseWorld;
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
BasicEffect effect = (BasicEffect)meshPart.Effect;
effect.World = localWorld;
effect.View = View;
effect.Projection = Projection;
effect.EnableDefaultLighting();
}
mesh.Draw();
}
您可能想用此問題前往gamedev.stackexchange.com – Jroosterman