Click here to Skip to main content
15,908,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
okay, i've been fumbling with this for awhile now, i had a class, that did exactly what i'm trying to do, that worked for d3d 9, but when i do something similar, with just a slight difference in framework, it doesn't update the new vectors at all. Here are all the members that manipulate the vectors and view matrix.

C++
void PlayerClass::Render(float dt, TBYTE * keyboardState, DIMOUSESTATE2 mouseState)
{
    m_KeyboardState = keyboardState;
    Walk(dt);
    Strafe(dt);

    Pitch(mouseState.lX * m_AngularSpeed);
    Yaw(mouseState.lY * m_AngularSpeed);
    BuildView();
}

void PlayerClass::BuildView()
{
    D3DXVECTOR3 & up = m_Up;
    D3DXVECTOR3 &position = m_Pos;
    D3DXVECTOR3 &lookAt = m_Look;
    D3DXVECTOR3 &right = m_Right;

    D3DXVec3Normalize(&lookAt, &lookAt);

    D3DXVec3Cross(&right, &up, &lookAt);
    D3DXVec3Normalize(&right, &right);

    D3DXVec3Cross(&up, &lookAt, &right);
    D3DXVec3Normalize(&up, &up);

    m_viewMatrix(0,0) = right.x;
    m_viewMatrix(1,0) = right.y;
    m_viewMatrix(2,0) = right.z;
    m_viewMatrix(3,0) = -D3DXVec3Dot(&position, &right);

    m_viewMatrix(0,1) = up.x;
    m_viewMatrix(1,1) = up.y;
    m_viewMatrix(2,1) = up.z;
    m_viewMatrix(3,1) = -D3DXVec3Dot(&position, &up);

    m_viewMatrix(0,2) = lookAt.x;
    m_viewMatrix(1,2) = lookAt.y;
    m_viewMatrix(2,2) = lookAt.z;
    m_viewMatrix(3,2) = -D3DXVec3Dot(&position, &lookAt);

    m_viewMatrix(0,3) = 0.0f;
    m_viewMatrix(1,3) = 0.0f;
    m_viewMatrix(2,3) = 0.0f;
    m_viewMatrix(3,3) = 1.0f;
}

void PlayerClass::Walk(float dt)
{
    if (ReturnStateKey(DIK_W))
    {
        m_Pos.z += m_Look.z * m_LinearSpeed * dt;
        m_Pos.x += m_Look.x * m_LinearSpeed & dt;
    }
    else if(ReturnStateKey(DIK_S))
    {
        m_Pos.z -= m_Look.z * m_LinearSpeed * dt;
        m_Pos.x += m_Look.x * m_LinearSpeed & dt;
    }
}

void PlayerClass::Strafe(float dt)
{
    if (ReturnStateKey(DIK_A))
    {
        m_Pos.x -= m_Right.x * m_LinearSpeed * dt;
        m_Pos.z -= m_Right.x * m_LinearSpeed * dt;
    }
    else if(ReturnStateKey(DIK_D))
    {
        m_Pos.x += m_Right.x * m_LinearSpeed * dt;
        m_Pos.z += m_Right.z * m_LinearSpeed * dt;
    }
}

void PlayerClass::Yaw(float dr)
{
    D3DXMATRIX R;
    D3DXMatrixRotationAxis(&R, &m_Right, dr);
    D3DXVec3TransformCoord(&m_Look, &m_Look, &R);
    D3DXVec3TransformCoord(&m_Up, &m_Up, &R);

}

void PlayerClass::Pitch(float dr)
{
    D3DXMATRIX R;
    D3DXMatrixRotationY(&R, dr);
    D3DXVec3TransformCoord(&m_Look, &m_Look, &R);
    D3DXVec3TransformCoord(&m_Right, &m_Right, &R);
}


keyboardState is a pointer to the buffer containing the current keystrokes, updated every frame, mouseState is the xy axis position currently of the mouse, updated every frame, and dt is the frame time, also updated every frame. I've checked and these are all received correctly. Problem is, when the program runs, the only thing that moves correctly is the position vector. when i press forward, it moves along the z axis of the world, and when i press right, it moves along the x axis of the world, but when i move the mouse, the camera's look at vector orientation moves correctly, like the camera looks up and down and left and right, but the vectors aren't updating correctly, like, if i look to the left, and then press forward, it still moves along the z axis of the world, and not along the lookAt vector of the camera, you see what i mean? Any help here?

EDIT: yeh, it occurred to me that the way i described it may have been a little confusing, so here is a video illustrating my problem. http://www.youtube.com/watch?v=vFXQFo7bZFg[^] As you can see, pressing forward moves along the original orientation, and not along the new lookat axis when i press WASD.

EDIT: Code updated to show the solution, my solution below :\
Posted
Updated 3-Oct-11 3:45am
v6

1 solution

Interesting, i continued fumbling with it, and i found my own solution :\ . It didn't really occur to me, that when I told it to walk along the z of the look at vector that the position would move only along the z of that vector. so i added a line to each of the movement functions that made it displace along the x, as well z axis of the lookat vector and viola, first person camera :P . my code has been updated to show this. If you were raking through this trying to help me out, sorry for being an alarmist :\. Also, it only occurs to me now, am i aloud to post direct x related questions on this site?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900