0
\$\begingroup\$

I have a bit of a dilemma ... So i setup my scene how I want it with some bits in it and now I want to move all my camera related code in to a camera class however doing so is proving to be a pain in my ass.

So in my game code (my main game class) I have ...

    protected override void Initialize()
    {
        model = new TerrainModel();
        base.Initialize();

        // Load known core components
        camera = new Camera(this, cameraPosition, new Vector3(10, 10, 10));
    }

Then in update ...

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            this.Exit();

        camera.Update();

        base.Update(gameTime);
    }

Then in draw ...

    protected override void Draw(GameTime gameTime)
    {
        cubeEffect.View = Matrix.CreateLookAt(cameraPosition, new Vector3(10, 10, 10), Vector3.Up);
        cubeEffect.View = camera.View;

        cubeEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 1.0f, 1000.0f);
        cubeEffect.Projection = camera.Projection;
    }

It's these draw lines that have me confused. If i use the first pair (the lines that create the matrices there and then) it all works.

If I use the second pair (pulling the same thing from my camera class) it doesn't and i get a blank screen.

I'm only rendering a single model so there's a chance it could be pointing in the wrong direction but it seems to have done what i expect when i put breakpoints in and look at the variables.

So what have i done wrong?

Here's my camera class ...

public class Camera
{
    #region fields

    Matrix viewMatrix;
    Matrix projectionMatrix;
    Viewport viewPort;
    BoundingFrustum cameraViewableBounds;
    Vector3 currentTarget;
    Vector3 cameraPosition;

    #endregion

    #region Properties

    public Matrix Projection { get { return projectionMatrix; } }
    public Matrix View { get { return viewMatrix; } }
    public Viewport ViewPort { get { return viewPort; } }

    public Vector3 Position
    {
        get { return cameraPosition; }
        set
        {
            cameraPosition = value;
            Update();
        }
    }

    public Vector3 TargetPosition { get { return currentTarget; } }

    public bool CanSee(BoundingBox ObjectBounds)
    {
        if (cameraViewableBounds.Contains(ObjectBounds) != ContainmentType.Disjoint)
            return true;

        return false;
    }

    #endregion

    public Camera(Game game, Vector3 startingPosition, Vector3 startingTarget)
    {
        cameraPosition = startingPosition;

        // creates a new view matrix
        LookAt(startingTarget);

        viewPort = game.GraphicsDevice.Viewport;

        // Set the Projection matrix which defines how we see the scene (Field of view)
        projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 0, 1, 10000);
    }

    #region Camera state updating

    public void Update()
    {
        viewMatrix = Matrix.CreateLookAt(cameraPosition, TargetPosition, Vector3.Up);
        cameraViewableBounds = new BoundingFrustum(viewMatrix * projectionMatrix);
    }

    public void LookAt(Vector3 target)
    {
        currentTarget = target;
        viewMatrix = Matrix.CreateLookAt(Position, TargetPosition, Vector3.Up);
    }

    #endregion
}

}

I'm sure this is something crazy simple that i've missed ... i just can't see it.

NOTE: I have omitted a lot of code here to try and simplify my question but I'm pretty sure the rest of my code is fine because it works unless I use this new camera class, however I can provide more if its needed.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Have a look at this line in the Camera class again:

projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 0, 1, 10000);

You have your aspect ratio as zero (which won't work)

For what it's worth I think the call to LookAt is redundant/confusing in the Camera constructor -- if anything in the scene is going to ever move you will be needing to recreate the View matrix on Update anyway.

Last thing, have you heard of Auto Implemented properties?

\$\endgroup\$
2
  • \$\begingroup\$ how come it works in the draw method in the main game class? but the exact same line don't work in a separate class (even though its passed to the same effect at runtime). Yeh I agree with you on both those other points too (this is by no means "complete code" it's probably one of the dirtiest hacks i've ever put together but the camera logic is not really what I want to be thinking about right now. \$\endgroup\$
    – War
    Commented Mar 15, 2013 at 22:21
  • \$\begingroup\$ ah nice you were absolutely spot on .. i passed it the viewports aspect ration (already had this to hand in the class) and it worked!! thank you :) \$\endgroup\$
    – War
    Commented Mar 15, 2013 at 22:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .