I created a camera which is a LookAt camera. I do move it around by changing the position and the target position at the same time. I also added a rotation value for every axis and this is how the get Matrix looks like:
Camera::Camera() : m_pos(XMFLOAT3(0.0f, 0.0f, 0.0f)), m_target(XMFLOAT3(0.0f, 0.0f, 0.0f)), m_upVec(XMFLOAT3(0.0f, 1.0f, 0.0f)), m_rotation(XMFLOAT3(0.0f, 0.0f, 0.0f))
{
}
Camera::Camera(XMFLOAT3 position, XMFLOAT3 target) : m_pos(position), m_target(target), m_upVec(XMFLOAT3(0.0f, 1.0f, 0.0f))
{
}
Camera::~Camera()
{
}
XMMATRIX Camera::getViewMatrix()
{
XMMATRIX lookAt = XMMatrixLookAtLH(XMLoadFloat3(&m_pos), XMLoadFloat3(&m_target), XMLoadFloat3(&m_upVec));
//XMMATRIX translation = XMMatrixTranslation(m_pos.x, m_pos.y, m_pos.z); //tryed adding this
XMMATRIX rotationX = XMMatrixRotationX(m_rotation.x);
XMMATRIX rotationY = XMMatrixRotationY(m_rotation.y);
XMMATRIX rotationZ = XMMatrixRotationZ(m_rotation.z);
return lookAt * rotationX * rotationY * rotationZ;
}
void Camera::setPos(XMFLOAT3 pos, XMFLOAT3 target)
{
m_target = target;
m_pos = pos;
}
void Camera::setRotation(XMFLOAT3 rot)
{
m_rotation = rot;
}
XMFLOAT3 Camera::getPosition()
{
return m_pos;
}
XMFLOAT3 Camera::getRotation()
{
return m_rotation;
}
XMFLOAT3 Camera::getTarget()
{
return m_target;
}
I am trying to move the camera depending on it's position but it always moves along the axis. Not in "it's coordinate system".
Here is an example for movement and rotation:
if (m_keyBoardKeys[DIK_UP] & 0x80)
{
m_camera->setPos(XMFLOAT3(m_camera->getPosition().x,
m_camera->getPosition().y - movementspeed*delta, m_camera->getPosition().z),
XMFLOAT3(m_camera->getTarget().x, m_camera->getTarget().y - movementspeed*delta,
m_camera->getTarget().z));
}
//check if pressed
if (m_keyBoardKeys[DIK_C] & 0x80)
{
m_camera->setRotation(XMFLOAT3(m_camera->getRotation().x,
m_camera->getRotation().y + movementspeed*delta/4, m_camera->getRotation().z));
}
This matrix get added to the shader every rendercycle and multiplys correct.
PS_Input VS_Main(VS_Input vertex)
{
PS_Input vsOut = (PS_Input)0;
float4 worldPos = mul(vertex.pos, worldMatrix); //rotation and position of the current rendered object
vsOut.pos = mul(worldPos, viewMatrix); // camera matrix
vsOut.pos = mul(vsOut.pos, projMatrix); // perspective projection matrix
vsOut.tex0 = vertex.tex0;
vsOut.norm = mul(vertex.norm, (float3x3)worldMatrix);
vsOut.norm = normalize(vsOut.norm);
float3 lightPos = float3(0.0f, 500.0f, 50.0f);
vsOut.lightVec = normalize(lightPos - worldPos);
vsOut.viewVec = normalize(cameraPos - worldPos);
return vsOut;
}
Question is: How to i get the camera moved in it's space not in the global space but still rotate it by it's axis?
You will need to create a rotation matrix based on the rotation, transform the target into that rotation matrix and add that new vector to the eyes position.
Here is an example of how I do it, this will move the camera forward or back based on the delta value passed in
XMVECTOR Pos = XMVectorSet( this->pos.x, this->pos.y, this->pos.z, this->pos.w );
XMVECTOR At = XMVectorSet( this->targetMagnitude.x , this->targetMagnitude.y, this->targetMagnitude.z, this->targetMagnitude.w );
XMMATRIX RotationMatrix( XMMatrixRotationRollPitchYaw( this->RadianPitch , this->RadianYaw, this->RadianRoll ));
At = XMVector3TransformCoord( At, RotationMatrix );
At = XMVector4Normalize(At);
Pos += At * delta;
XMStoreFloat4(&this->pos, Pos);