Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在XNA中仅看到3个数组中的1个对象_C#_Arrays_Xna_Draw - Fatal编程技术网

C# 在XNA中仅看到3个数组中的1个对象

C# 在XNA中仅看到3个数组中的1个对象,c#,arrays,xna,draw,C#,Arrays,Xna,Draw,有人能看出我哪里出了问题吗 我有一个CameraObject类(它不是camera,只是表示“camera”的框的模型),它有一个模型和一个位置。它还有常用的LoadContent()、Draw()和Update()方法。 然而,当我绘制模型数组时,我在屏幕上只看到1个模型(可能有3个,但它们可能都在同一位置) CameraModel类的Draw()方法如下所示: public void Draw(Matrix view, Matrix projection) { t

有人能看出我哪里出了问题吗

我有一个CameraObject类(它不是camera,只是表示“camera”的框的模型),它有一个模型和一个位置。它还有常用的LoadContent()、Draw()和Update()方法。 然而,当我绘制模型数组时,我在屏幕上只看到1个模型(可能有3个,但它们可能都在同一位置)

CameraModel类的Draw()方法如下所示:

public void Draw(Matrix view, Matrix projection) 
    { 
        transforms = new Matrix[CameraModel.Bones.Count]; 
        CameraModel.CopyAbsoluteBoneTransformsTo(transforms); 

        // Draw the model 
        foreach(ModelMesh myMesh in CameraModel.Meshes) 
        { 
            foreach (BasicEffect myEffect in myMesh.Effects) 
            { 
                myEffect.World = transforms[myMesh.ParentBone.Index]; 
                myEffect.View = view; 
                myEffect.Projection = projection; 

                myEffect.EnableDefaultLighting(); 
                myEffect.SpecularColor = new Vector3(0.25f); 
                myEffect.SpecularPower = 16; 
            } 
            myMesh.Draw(); 
        } 
    } 
for (int i = 0; i < cameraObject.Length; i++) 
{ 
    cameraObject[i].Draw(view, projection, cameraObject[i].Position); 
} 


//later in the draw method
public void Draw(Matrix view, Matrix projection, Vector3 pos)
{
  // ...
  myEffect.World = transforms[myMesh.ParentBone.Index] * Matrix.CreateTranslation(pos);
  // ...
}
然后在Game1类中,我创建了一个CameraObject对象数组:

CameraObject[] cameraObject = new CameraObject[3]; 
我初始化了它()-所以每个新对象都应该是前一个对象的+10

for (int i = 0; i < cameraObject.Length; i++) 
        { 
            cameraObject[i] = new CameraObject(); 
            cameraObject[i].Position = new Vector3(i * 10, i * 10, i * 10); 
        } 
但我只看到一个物体画在屏幕上?我已经通过代码,一切似乎都很好,但我不明白为什么我不能看到3个对象

谁能看出我哪里出了问题

这是UpdateViewMatrix的my Camera()类中的代码:

private void UpdateViewMatrix(Matrix chasedObjectsWorld)
    {
        switch (currentCameraMode)
        {
            case CameraMode.free:
                // To be able to rotate the camera and and not always have it looking at the same point
                // Normalize the cameraRotation’s vectors, as those are the vectors that the camera will rotate around
                cameraRotation.Forward.Normalize();
                cameraRotation.Up.Normalize();
                cameraRotation.Right.Normalize();

                // Multiply the cameraRotation by the Matrix.CreateFromAxisAngle() function, 
                // which rotates the matrix around any vector by a certain angle
                // Rotate the matrix around its own vectors so that it works properly no matter how it’s rotated already
                cameraRotation *= Matrix.CreateFromAxisAngle(cameraRotation.Right, pitch);
                cameraRotation *= Matrix.CreateFromAxisAngle(cameraRotation.Up, yaw);
                cameraRotation *= Matrix.CreateFromAxisAngle(cameraRotation.Forward, roll);

                // After the matrix is rotated, the yaw, pitch, and roll values are set back to zero
                yaw = 0.0f;
                pitch = 0.0f;
                roll = 0.0f;

                // The target is changed to accommodate the rotation matrix
                // It is set at the camera’s position, and then cameraRotation’s forward vector is added to it
                // This ensures that the camera is always looking in the direction of the forward vector, no matter how it’s rotated
                target = Position + cameraRotation.Forward;

                break;

            case CameraMode.chase:
                // Normalize the rotation matrix’s forward vector because we’ll be using that vector to roll around
                cameraRotation.Forward.Normalize();
                chasedObjectsWorld.Right.Normalize();
                chasedObjectsWorld.Up.Normalize();

                cameraRotation = Matrix.CreateFromAxisAngle(cameraRotation.Forward, roll);

                // Each frame, desiredTarget will be set to the position of whatever object we’re chasing
                // Then set the actual target equal to the desiredTarget, can then change the target’s X and Y coordinates at will
                desiredTarget = chasedObjectsWorld.Translation;
                target = desiredTarget;

                target += chasedObjectsWorld.Right * yaw;
                target += chasedObjectsWorld.Up * pitch;

                // Always want the camera positioned behind the object, 
                // desiredPosition needs to be transformed by the chased object’s world matrix
                desiredPosition = Vector3.Transform(offsetDistance, chasedObjectsWorld);

                // Smooth the camera’s movement and transition the target vector back to the desired target
                Position = Vector3.SmoothStep(Position, desiredPosition, .15f);

                yaw = MathHelper.SmoothStep(yaw, 0f, .1f);
                pitch = MathHelper.SmoothStep(pitch, 0f, .1f);
                roll = MathHelper.SmoothStep(roll, 0f, .1f);

                break;

            case CameraMode.orbit:
                // Normalizing the rotation matrix’s forward vector, and then cameraRotation is calculated
                cameraRotation.Forward.Normalize();

                //  Instead of yawing and pitching over cameraRotation’s vectors, we yaw and pitch over the world axes
                // By rotating over world axes instead of local axes, the orbiting effect is achieved
                cameraRotation = Matrix.CreateRotationX(pitch) * Matrix.CreateRotationY(yaw) * Matrix.CreateFromAxisAngle(cameraRotation.Forward, roll);

                desiredPosition = Vector3.Transform(offsetDistance, cameraRotation);
                desiredPosition += chasedObjectsWorld.Translation;
                Position = desiredPosition;

                target = chasedObjectsWorld.Translation;

                roll = MathHelper.SmoothStep(roll, 0f, .2f);

                break;
        }

        // Use this line of code to set up the View Matrix
        // Calculate the view matrix
        // The up vector is based on how the camera is rotated and not off the standard Vector3.Up
        // The view matrix needs an up vector to fully orient itself in 3D space, otherwise,
        // the camera would have no way of knowing whether or not it’s upside-down
        viewMatrix = Matrix.CreateLookAt(Position, target, cameraRotation.Up);
    }

CameraObject[1]和[2]位于比camera[0]更大的正Z值处,并且视图矩阵位于原点并沿负Z方向查看(请记住,视图矩阵是世界矩阵的反向等价物)。 与其将viewMatrix设置为Matrix.Identity,不如将其设置为此,您可能会看到这三个选项:

viewMatrix = Matrix.CreateLookAt(new Vector3(0,0,75), Vector3.Zero, Vector3.Up);

我没有在代码中看到cameraObject[n].Position(这可能是唯一区别这三个模型位置的东西)影响effect.World属性

effect.World = transforms[myMesh.ParentBone.Index];
通常不考虑单个模型位置

试着这样做:

public void Draw(Matrix view, Matrix projection) 
    { 
        transforms = new Matrix[CameraModel.Bones.Count]; 
        CameraModel.CopyAbsoluteBoneTransformsTo(transforms); 

        // Draw the model 
        foreach(ModelMesh myMesh in CameraModel.Meshes) 
        { 
            foreach (BasicEffect myEffect in myMesh.Effects) 
            { 
                myEffect.World = transforms[myMesh.ParentBone.Index]; 
                myEffect.View = view; 
                myEffect.Projection = projection; 

                myEffect.EnableDefaultLighting(); 
                myEffect.SpecularColor = new Vector3(0.25f); 
                myEffect.SpecularPower = 16; 
            } 
            myMesh.Draw(); 
        } 
    } 
for (int i = 0; i < cameraObject.Length; i++) 
{ 
    cameraObject[i].Draw(view, projection, cameraObject[i].Position); 
} 


//later in the draw method
public void Draw(Matrix view, Matrix projection, Vector3 pos)
{
  // ...
  myEffect.World = transforms[myMesh.ParentBone.Index] * Matrix.CreateTranslation(pos);
  // ...
}
for(int i=0;i
摄像机的位置是什么?尝试只改变所创建对象的x位置并将相机向后移动。好的,我试试。摄像机处于:位置=新矢量3(0、10、70);是的,我试过了,但是运气不好,抱歉。请参阅上面我的文章的编辑,我已经包含了我的Camera()类UpdateViewMatrix()方法。第一次在sorryYip的时候就应该包括它,这正是我的问题。非常感谢史蒂夫的帮助