Some of my old code has ended up with a bunch of nasty hacks to get things to work "correctly", in terms of moving objects around and the camera, such as having to take "std::sin(-yaw)" rather than "std::sin(yaw)" when implementing equations found elsewhere online, and such and has generally just made everything confusing to the point of trail and error in many cases.
Working with D3D11 and the DirectXMath stuff (so left hand coordinates and row major?), what exactly is the intended coordinate system e.g. assuming the camera is at the origin and looking along the yellow vector in the image with no rotation, are the labels correct?.
And then given that, and a camera described by (x,y,z) and pitch (y-axis mouse/control), yaw (x-axis mouse/control), and assuming there is not some other way I should be doing even that...
What is the correct function to get the view matrix (currently im multiplying a translate and 2 rotate matrices, multiplying with the projection, then any world matrices for the object in question, and transposing the result to use as a single shader constant).
What is the equation to get the vector the camera is looking along (current multiplying a (0,0,1) vector by the matrix from 2).
- ... and the "up" and "right" vectors (since even if not using a lookat matrix view function, seems frustum culling needs to know those). Again currently multiplying with the matrix from 2.
- Calculate correct pitch and yaw scalars/components from a direction vector (e.g. for a turret with separate pitch/yaw joints).
EDIT: Code samples:
//Moving a floating object with only yaw forwards (moveX,moveY,moveZ).
//Negative yaw seems wrong?
auto c = std::cosf(-yaw);
auto s = std::sinf(-yaw);
pos.x += moveX * c - moveZ * s;
pos.y += moveY;
pos.z += moveX * s + moveZ * c;
//Gets the vector the camera is looking along
//This time yaw is positive, but pitch is negative?
float c = std::cos(-pitch);
Vector3F facing(
c * std::sinf(yaw),
std::sinf(-pitch),
c * std::cosf(yaw));
//Creating the view transform matrix, everything is negative
XMMATRIX xmviewrot;
xmviewrot = XMMatrixRotationY(-yaw);
xmviewrot*= XMMatrixRotationX(-pitch);
XMMATRIX xmview;
xmview = XMMatrixTranslation(-x, -y, -z);
xmview *= xmviewrot;
XMStoreFloat4x4A(&view, xmview);
//Other vectors needed for frustum culling
XMVECTOR xmup = XMVector3Transform(XMLoadFloat4A(&UP), xmview);
XMVECTOR xmright = XMVector3Transform(XMLoadFloat4A(&RIGHT), xmview);
//Matrix for stuff that is already in world space (e.g. terrain)
XMMATRIX xmviewProj = xmview * xmproj;
//Apparently needs transposing before use on the GPU...
XMStoreFloat4x4A(&constants.transform, XMMatrixTranspose(xmviewProj));
//In the shaders
output.pos = mul(input.pos, transform);
//vertex positions for an upwards facing square with triangle strip
v0 = (x1, y, z1);
v1 = (x1, y, z2);
v2 = (x2, y, z2);
v3 = (x2, y, z1);
So seems to me Ive done something fundamentally wrong here to need the -yaw and +yaw, -pitch and +pitch in different places? And some of those functions I ended up doing with trail and error to get that bit right, online samples didnt use negative.