Sign in to follow this  
john13to

Screen goes blank when strafing the camera

Recommended Posts

As I continue with the Glass Prism project in XNA, I am trying to implement the ability to strafe the camera in left and right direction. I have followed the 5th tutorial in the insidegamers.org page on how to implement the WASD-controls. I have implemented it, as far as I can see, exactly as it is written in the tutorial. But when I run the application and press the A-key to strafe the camera to the left, the screen goes completely black. And I cannot see the created object in front of the screen at all. I earlier did implement it in a different way according to another tutorial on the net but I got the same result. What could be wrong?

Share this post


Link to post
Share on other sites
Possibilities:
- Your camera might be moving too much. Try moving in *MUCH* smaller increments first to find out.
- You might have corrupted your camera matrix somehow (multiplying by NaN for example).
- There could be a typo hiding somewhere in the code that's doing something else completely.


It would be helpful if you could show us your code:
- The part where you check to see if the key is down
- How you store/change your camera position



From glancing at this document (I think it's the one you're referring to), One possible problem is that you're not initializing that "transRef" variable he's using. If that's the case, when you create the new view matrix in UpdatePosition, it will be a camera with using identical position and look-at points. This will result in a camera matrix that can't be used to render properly.

Make sure transRef is always non-zero!

The most likely place for a typo in your code is this one:


lookAt = transRef = new Vector3(0.0f, 0.0f, 1.0f);


If you missed transRef on that line, it'll behave like I mentioned above.

Share this post


Link to post
Share on other sites
Yes it was that tutorial, here is the part of the code where I am implementing the strafe camera:


public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

//Camera movement and rotation speeds
public float rotationSpeed = 0.05f;
private float forwardSpeed = 0.05f;

//Amount to turn as a float
public float turnAmt;

//Position and reference vectors
private Vector3 cameraPos;
private Vector3 transRef;
private Vector3 cameraRef;
private Vector3 lookAt;

//Any camera rotations
private Matrix cameraRotation;

BasicEffect basicEffect;
Matrix world;
Matrix view;
Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 600f, 0.01f, 100f);
......

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
font1 = Content.Load<SpriteFont>("SpriteFont1");
font1Pos = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2,
graphics.GraphicsDevice.Viewport.Height / 2);

basicEffect = new BasicEffect(GraphicsDevice, null);

cameraPos = new Vector3(0.0f, 0.0f, 3.0f);
lookAt = new Vector3(0.0f, 0.0f, 1.0f);
transRef = new Vector3(0.0f, 0.0f, 1.0f);
cameraRef = new Vector3(0.0f, 0.0f, 1.0f);

cameraRotation = Matrix.Identity;

world = Matrix.CreateTranslation(0, 0, 0);
view = Matrix.CreateLookAt(cameraPos, lookAt, Vector3.Up);

......

}

protected override void Update(GameTime gameTime)
{
if (keyState.IsKeyDown(Keys.A))
{

Vector3 v = new Vector3(forwardSpeed, 0.0f, 0.0f);

UpdatePosition(v);

}
.......

}


private void UpdatePosition(Vector3 newPos)
{
//Create a new rotation matrix about the Y-Axis
Matrix yRotation = Matrix.CreateRotationY(turnAmt);

Vector3 currPos = Vector3.Transform(newPos, yRotation);

//Set position of our camera to the v vector's new values
cameraPos.X += currPos.X;
cameraPos.Y += currPos.Y;
cameraPos.Z += currPos.Z;

//Update our lookAt Matrix
lookAt = cameraPos + transRef;

view = Matrix.CreateLookAt(cameraPos, lookAt, Vector3.Up);
}


protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);

basicEffect.Begin();

basicEffect.World = world;
basicEffect.View = view;
basicEffect.Projection = projection;
basicEffect.VertexColorEnabled = true;

GraphicsDevice.VertexDeclaration = new VertexDeclaration(GraphicsDevice,
VertexPositionColor.VertexElements);

.....

}

}

But it turns out the same, as I have checked that transRef is assigned as properly.

Share this post


Link to post
Share on other sites
It seems to do with when the vector lookAt is updated and then is inserted to the Matrix.CreateLookAt function:

lookAt = cameraPos + transRef;

view = Matrix.CreateLookAt(cameraPos, lookAt, Vector3.Up);

If I leave lookAt unchanged, then the camera will move around the unchanged lookAt position
and it wont go blank. But when it is changed then the screen will become completely black.

Even if I write:

view = Matrix.CreateLookAt(cameraPos, lookAt + new Vector3(0,0,1), Vector3.Up);

it still becomes completely black. Why is that?

Share this post


Link to post
Share on other sites
Now I have solved it and it turned out to be the vector lookAt that wasnt updated the correct way. The vector lookAt shouldnt be updated the way that is written in the tutorial of some reason:

lookAt = cameraPos + transRef;

But instead it should be updated this way instead:

lookAt += currPos;

To sum it up the correct implementation of the UpdatePosition should be written this way:

private void UpdatePosition(Vector3 newPos)
{
//Create a new rotation matrix about the Y-Axis
Matrix yRotation = Matrix.CreateRotationY(turnAmt);

Vector3 currPos = Vector3.Transform(newPos, yRotation);

//Set position of our camera to the v vector's new values
cameraPos.X += currPos.X;
cameraPos.Y += currPos.Y;
cameraPos.Z += currPos.Z;

//Update our lookAt Matrix
lookAt += currPos;

view = Matrix.CreateLookAt(cameraPos, lookAt, Vector3.Up);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this