void WorldNode::Rotate(const Quaternion& Rotation, const Mezzanine::TransformSpace& TS) { Quaternion NormRot = Rotation.GetNormalizedCopy(); switch(TS) { case Mezzanine::TS_World: SetLocalOrientation(NormRot * GetLocalOrientation()); break; case Mezzanine::TS_Parent:// This...I don't even... SetLocalOrientation(GetLocalOrientation() * GetOrientation().GetInverse() * NormRot * GetOrientation()); break; default: case Mezzanine::TS_Local: SetLocalOrientation(GetLocalOrientation() * NormRot); break; } }
void Transform::LookAt(float x, float y, float z, float lookAtX, float lookAtY, float lookAtZ) { Vector3 up(0.f, 1.f, 0.f); Vector3 from(x, y, z); Vector3 to(lookAtX, lookAtY, lookAtZ); // create the look at matrix. cachedTransform = Matrix4::CreateLookAt(from, to, up); // Set the orientation in the transform from the look at matrix SetLocalOrientation(cachedTransform.GetRotation()); SetPosition(from); needUpdate(true); }
// // 设置世界朝向 // VOID CSceneNode::SetWorldOrientation(FLOAT x, FLOAT y, FLOAT z, FLOAT w) { if (m_pParentNode) { // 算法: // worldOrientation = localOrientation * parentOrientation // localOrientation = worldOrientation * parentOrientationInv QUAT parentOrientationInv; QuatInverse(&parentOrientationInv, m_pParentNode->GetWorldOrientation()); QUAT localOrientation; QUAT worldOrientation; QuatSet(&worldOrientation, x, y, z, w); QuatMul(&localOrientation, &worldOrientation, &parentOrientationInv); x = localOrientation[0]; y = localOrientation[1]; z = localOrientation[2]; w = localOrientation[3]; } SetLocalOrientation(x, y, z, w); }
void Transform::FPS(Vector3 pos, float pitch, float yaw) { /* float cosPitch = cos(pitch); float sinPitch = sin(pitch); float cosYaw = cos(yaw); float sinYaw = sin(yaw); Vector3 xaxis(cosYaw, 0, -sinYaw); Vector3 yaxis(sinYaw * sinPitch, cosPitch, cosYaw * sinPitch); Vector3 zaxis(sinYaw * cosPitch, -sinPitch, cosPitch * cosYaw); cachedTransform = Matrix4( xaxis.x, xaxis.y, xaxis.z, -xaxis.Dot(pos), yaxis.x, yaxis.y, yaxis.z, -yaxis.Dot(pos), zaxis.x, zaxis.y, zaxis.z, -zaxis.Dot(pos), 0, 0, 0, 1); SetPosition(cachedTransform.GetTranslation()); SetLocalOrientation(cachedTransform.GetRotation()); Quaternion currentRot = derivedOrientation; Vector3 xAxis, yAxis, zAxis; currentRot.ToAxes(xAxis, yAxis, zAxis); Quaternion yawQ = Quaternion(yAxis, yaw); Quaternion pitchQ = Quaternion(xAxis, pitch); Quaternion finalRot = pitchQ * yawQ; finalRot = finalRot * currentRot; finalRot.Normalise(); */ // Calculate using glm glm::vec3 gPos(pos.x, pos.y, pos.z); glm::vec3 dir( cos(pitch) * sin(yaw), sin(pitch), cos(pitch) * cos(yaw) ); //glm::vec3 up(up.x, up.y, up.z); glm::vec3 right( sin(yaw - 3.14159f/2.0f), 0, cos(yaw - 3.14159/2.0f) ); glm::vec3 up = glm::cross(right, dir); glm::mat4 vMat = glm::lookAt(gPos, gPos + dir, up); Matrix4 gViewMat; int i = 0; for (int r = 0; r < 4; r++) { glm::vec4 row = vMat[r]; for (int c = 0; c < 4; c++) { gViewMat[i++] = row[c]; } } SetPosition(pos); SetLocalOrientation(gViewMat); //derivedOrientation = finalRot; needUpdate(true); }