XMFLOAT3 Transform::GetForwardVector() { XMVECTOR forward = XMVector3Rotate(XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f), XMQuaternionRotationRollPitchYawFromVector(XMLoadFloat3(&rotation))); XMFLOAT3 realForward; XMStoreFloat3(&realForward, forward); return realForward; }
void Transform::MoveRelative(float addX, float addY, float addZ) { isDirty = true; XMVECTOR dir = XMVector3Rotate(XMVectorSet(addX, addY, addZ, 0.0f), XMQuaternionRotationRollPitchYawFromVector(XMLoadFloat3(&rotation))); XMStoreFloat3(&position, XMVectorAdd(dir, XMLoadFloat3(&position))); }
Box::Box(float x, float y, float z, XMVECTOR position, float mass, bool fixed, XMVECTOR orientation) { this->position = position; // its the center position of the box this->velocity = XMVectorSet(0.f, 0.f, 0.f, 0.f); length = XMVectorSet(x, y, z, 0.f); centerOfMass = Point(position, false); this->transform = XMMatrixTranslationFromVector(this->position); this->centerOfMass.fixed = fixed; this->orientation = XMQuaternionRotationRollPitchYawFromVector(orientation); if (fixed) { centerOfMass.mass = 1.0f; massInverse = 0.0f; intertiaTensorInverse = XMMATRIX(); } else { centerOfMass.mass = mass; massInverse = 1.0f / mass; float prefix = (1.0f / 12.0f) * centerOfMass.mass; XMMATRIX matrix = XMMATRIX(XMVectorSet(prefix*(y*y + z*z), 0.f, 0.f, 0.f), XMVectorSet(0.f, prefix * (x*x + z*z), 0.f, 0.f), XMVectorSet(0.f, 0.f, prefix*(x*x + y*y),0.f), XMVectorSet(0.f, 0.f, 0.f, 1.f)); intertiaTensorInverse = XMMatrixInverse(&XMMatrixDeterminant(matrix), matrix); } XMVECTOR xLength = XMVectorSet(x, 0.f, 0.f, 0.f) / 2; XMVECTOR yLength = XMVectorSet(0.f, y, 0.f, 0.f) / 2; XMVECTOR zLength = XMVectorSet(0.f, 0.f, z, 0.f) / 2; for (int i = 0; i < 8; i++) { corners[0] = XMVECTOR(); } this->angularMomentum = XMVECTOR(); this->angularVelocity = XMVECTOR(); this->torqueAccumulator = XMVECTOR(); corners[0] = -xLength - yLength - zLength; corners[1] = xLength - yLength - zLength; corners[2] = -xLength + yLength - zLength; corners[3] = xLength + yLength - zLength; corners[4] = -xLength - yLength + zLength; corners[5] = xLength - yLength + zLength; corners[6] = -xLength + yLength + zLength; corners[7] = xLength + yLength + zLength; }
XMFLOAT4X4 Transform::RecalculateWorldMatrix() { //TODO: figure out a better way to handle rotaions if (!GetIsDirty()) return worldMatrix;//Dont know if I want to keep it. XMMATRIX allignedWorldMatrix = XMLoadFloat4x4(&worldMatrix); if (parent == nullptr) { XMMATRIX calculatedWorldMatrix = XMMatrixScalingFromVector(XMLoadFloat3(&scale)) * //XMMatrixRotationRollPitchYawFromVector(XMLoadFloat3(&rotation)) * //XMMatrixRotationX(rotation.x) * XMMatrixRotationY(rotation.y) * XMMatrixRotationZ(rotation.z) * XMMatrixRotationQuaternion(XMQuaternionRotationRollPitchYawFromVector(XMLoadFloat3(&rotation))) * XMMatrixTranslationFromVector(XMLoadFloat3(&position)); XMStoreFloat4x4(&worldMatrix, XMMatrixTranspose(calculatedWorldMatrix)); } else { XMMATRIX calculatedWorldMatrix = XMMatrixScalingFromVector(XMLoadFloat3(&scale)) * //XMMatrixRotationRollPitchYawFromVector(XMLoadFloat3(&rotation)) * XMMatrixRotationQuaternion(XMQuaternionRotationRollPitchYawFromVector(XMLoadFloat3(&rotation))) * XMMatrixTranslationFromVector(XMLoadFloat3(&position)); XMStoreFloat4x4(&worldMatrix, XMMatrixTranspose( XMMatrixMultiply(XMMatrixTranspose(XMLoadFloat4x4(&parent->GetWorldMatrix())), calculatedWorldMatrix))); } return worldMatrix; }
void D3DCamera::Render(XMVECTOR translate, XMVECTOR rotate, bool move, bool playback) { XMVECTOR upVector, lookAtVector, rightVector; XMMATRIX rotationMatrix; rightVector = XMVectorSet(1.0f, 0.0f, 0.0f, 0.0f); upVector = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); lookAtVector = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); if (move) { XMVECTOR quaternion = XMQuaternionRotationRollPitchYawFromVector(rotate); quaternion = XMQuaternionNormalize(quaternion); m_rotation = XMQuaternionMultiply(quaternion, m_rotation); } if (playback) m_rotation = rotate; rotationMatrix = XMMatrixRotationQuaternion(m_rotation); // Transform the lookAt and up vector by the rotation matrix so the view is correctly rotated at the origin. lookAtVector = XMVector3TransformCoord(lookAtVector, rotationMatrix); upVector = XMVector3TransformCoord(upVector, rotationMatrix); rightVector = XMVector3TransformCoord(upVector, rotationMatrix); if (move) { translate = XMVector3TransformCoord(translate, rotationMatrix); translate = XMVectorSetW(translate, 0); m_position += translate; } if (playback) m_position = translate; // Translate the rotated camera position to the location of the viewer. lookAtVector = XMVectorAdd(m_position, lookAtVector); m_lookAtVector.x = lookAtVector.m128_f32[0]; m_lookAtVector.y = lookAtVector.m128_f32[1]; m_lookAtVector.z = lookAtVector.m128_f32[2]; // Finally create the view matrix from the three updated vectors. m_viewMatrix = XMMatrixLookAtLH(m_position, lookAtVector, upVector); return; }