Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've been working in 3DSMax for years now and decided to give XNA 4.0 a try. I've got the majority of it working, however the camera is being a pain to me and I can't discern what's gone wrong. I followed tutorials up to this point and am branching out into newer things.

Basically for this setup, I have a 'grid' in 3D space (all have a Y-component of 0) with spheres located here and there along the grid (spheres are FBX models from 3DS). What I'm trying to do is write a quick move command that'll move the camera above and behind one of the objects and then look at it. By using the keyboard, I can scroll through the list of objects and the camera should immediately jump to the specified position. However, in practice, the camera ends up in strange places, doesn't look where it's supposed to, and I'm not even sure it's actually moving!

The location of the objects is stored within a class object as a Vector2 (hence the Y/Z swap below), as they are all on the same plane (no Y-axis used). The following code, in theory, should return the position of the camera, and it's target, based on that class object's position in the world. m_vPosition is a Vector2 that defines the location of the object. That vector is defined at creation-time and is in the Translation Matrix for the object.

    public void GetCameraHoverLocation(out Vector3 p_vPos, out Vector3 p_vLook)
    {
        p_vPos = new Vector3(0, 0, 0);
        p_vLook = new Vector3(0, 0, 0);
        // Position should be above and behind
        p_vPos.X = m_vPosition.X;
        p_vPos.Y = 1;
        p_vPos.Z = m_vPosition.Y + 10;

        p_vLook.X = m_vPosition.X;
        p_vLook.Y = 0;
        p_vLook.Z = m_vPosition.Y;
    }

The code to move the camera into position is:

    public void SetCameraPosition(Vector3 p_vPos, Vector3 p_vLookAt)
    {
        m_mViewMatrix = Matrix.CreateLookAt(p_vPos, p_vLookAt, Vector3.Up);
        m_vCameraPos = p_vPos;
        m_vCameraView = p_vLookAt;
    }

The Position and LookAt are stored for later usage. The ViewMatrix is then given to the shader and bob's your uncle. However, again, the positioning of the camera is alllll wrong. I'm thinking it might be a complete misunderstanding of the Matrix.CreateLookAt function, but it works as written if I don't bother moving it (up to this point that is). Can anyone point me in the right direction on this one? I'm used to the way 3DS does things with cameras (vector for location, vector for target), so that might be polluting my thought process.

share|improve this question
    
Screenshots? Also, have you got a projection matrix set up with CreatePerspectiveFieldOfView(...) This needs to be set when you adjust the camera too. –  Phil Apr 28 '13 at 10:43
    
I have a CreatePerspectiveFieldOfView() function as well, but as it doesn't have anything in it that changes (field of View, Aspect Ratio, Near/Far plane) I didn't think it needed to be called. The view matrix and projection matrix are multiplied just before rendering. I'll see if I can make some screen shots... –  AzHole Apr 28 '13 at 14:16
add comment

1 Answer

up vote 0 down vote accepted

This was a myriad of problems gone awry. I rewrote my Camera class using another tutorial insight (http://www.madgamedev.com/post/2010/09/05/Article-Simple-3D-Camera-in-XNA.aspx) that helped point out my problems. While the way I had it setup was, in theory, correct, the way in which I was handling it was erroneous. In addition there was an additional cascade issue when I was calculating the Matrix for the position of my models. They were being multiplied a few too many times, so while my target was correct, the actual location physically in the world was wrong. I implemented the design setup in the above link and fixed my location bug, and darn, it looks quite nice now.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.