Note that there are some explanatory texts on larger screens.

plurals
  1. POOnly seeing 1 object out of an array of 3 in XNA
    primarykey
    data
    text
    <p>Can anyone see where I am going wrong here.</p> <p>I have a CameraObject class (its not a camera, simply the Model of a box to represent a "camera") that has a Model and a Position. It also has the usual LoadContent(), Draw() and Update() methods. However, when I draw the array of Models, I only see 1 model on the screen (well, there might be 3 but they might all be in the same location)?</p> <p>The Draw() method for the CameraModel class looks like this:</p> <pre><code>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(); } } </code></pre> <p>Then in my Game1 class I create an array of CameraObject objects:</p> <pre><code>CameraObject[] cameraObject = new CameraObject[3]; </code></pre> <p>Which I Initialize() - so each new object should be at +10 from the previous object</p> <pre><code>for (int i = 0; i &lt; cameraObject.Length; i++) { cameraObject[i] = new CameraObject(); cameraObject[i].Position = new Vector3(i * 10, i * 10, i * 10); } </code></pre> <p>And finally Draw()</p> <pre><code>Matrix view = camera.viewMatrix; Matrix projection = camera.projectionMatrix; for (int i = 0; i &lt; cameraObject.Length; i++) { cameraObject[i].Draw(view, projection); } </code></pre> <p>Where view and projection are from my Camera() class which looks like so:</p> <pre><code>viewMatrix = Matrix.Identity; projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), 16 / 9, .5f, 500f); </code></pre> <p>But I only see 1 object drawn to the screen? I have stepped through the code and all seems well but I cant figure out why I cant see 3 objects?</p> <p>Can anyone spot where I am going wrong?</p> <p>This is the code in my Camera() class to UpdateViewMatrix:</p> <pre><code>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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload