bool TransformComponent::Init(TiXmlElement* pData) { CB_ASSERT(pData); Vec3 yawPitchRoll = m_Transform.GetYawPitchRoll(); yawPitchRoll.x = RADIANS_TO_DEGREES(yawPitchRoll.x); yawPitchRoll.y = RADIANS_TO_DEGREES(yawPitchRoll.y); yawPitchRoll.z = RADIANS_TO_DEGREES(yawPitchRoll.z); Vec3 position = m_Transform.GetPosition(); TiXmlElement* pPositionElement = pData->FirstChildElement("Position"); if (pPositionElement) { double x = 0; double y = 0; double z = 0; pPositionElement->Attribute("x", &x); pPositionElement->Attribute("y", &y); pPositionElement->Attribute("z", &z); position = Vec3(x, y, z); } TiXmlElement* pOrientationElement = pData->FirstChildElement("YawPitchRoll"); if (pOrientationElement) { double yaw = 0; double pitch = 0; double roll = 0; pOrientationElement->Attribute("x", &yaw); pOrientationElement->Attribute("y", &pitch); pOrientationElement->Attribute("z", &roll); yawPitchRoll = Vec3(yaw, pitch, roll); } Mat4x4 translation; translation.BuildTranslation(position); Mat4x4 rotation; rotation.BuildYawPitchRoll((float)DEGREES_TO_RADIANS(yawPitchRoll.x), (float)DEGREES_TO_RADIANS(yawPitchRoll.y), (float)DEGREES_TO_RADIANS(yawPitchRoll.z)); m_Transform = rotation * translation; return true; }
// create a game object from lua int LuaInternalScriptExports::CreateGameObject(const char* objectArchetype, LuaPlus::LuaObject luaPosition, LuaPlus::LuaObject luaYawPitchRoll) { // lua position must be a table if (!luaPosition.IsTable()) { CB_ERROR("Invalid object passed to CreateGameObject(). Type = " + std::string(luaPosition.TypeName())); return INVALID_GAMEOBJECT_ID; } // lua yawPitchRoll must be a table if (!luaYawPitchRoll.IsTable()) { CB_ERROR("Invalid object passed to CreateGameObject(). Type = " + std::string(luaYawPitchRoll.TypeName())); return INVALID_GAMEOBJECT_ID; } Vec3 position(luaPosition["x"].GetFloat(), luaPosition["y"].GetFloat(), luaPosition["z"].GetFloat()); Vec3 yawPitchRoll(luaYawPitchRoll["x"].GetFloat(), luaYawPitchRoll["y"].GetFloat(), luaYawPitchRoll["z"].GetFloat()); // build the transform for the object Mat4x4 transform; transform.BuildYawPitchRoll(yawPitchRoll.x, yawPitchRoll.y, yawPitchRoll.z); transform.SetPosition(position); // create the object TiXmlElement* overloads = nullptr; StrongGameObjectPtr pObject = g_pApp->m_pGame->CreateGameObject(objectArchetype, overloads, &transform); // fire the new object created event if (pObject) { shared_ptr<Event_NewGameObject> pNewObjectEvent(CB_NEW Event_NewGameObject(pObject->GetId())); IEventManager::Get()->QueueEvent(pNewObjectEvent); return pObject->GetId(); } return INVALID_GAMEOBJECT_ID; }
bool TransformComponent::VInit(TiXmlElement* pData) { AC_ASSERT(pData); // [mrmike] - this was changed post-press - because changes to the TransformComponents can come in partial definitions, // such as from the editor, its better to grab the current values rather than clear them out. Vec3 yawPitchRoll = m_transform.GetYawPitchRoll(); yawPitchRoll.x = RADIANS_TO_DEGREES(yawPitchRoll.x); yawPitchRoll.y = RADIANS_TO_DEGREES(yawPitchRoll.y); yawPitchRoll.z = RADIANS_TO_DEGREES(yawPitchRoll.z); Vec3 position = m_transform.GetPosition(); TiXmlElement* pPositionElement = pData->FirstChildElement("Position"); if (pPositionElement) { double x = 0; double y = 0; double z = 0; pPositionElement->Attribute("x", &x); pPositionElement->Attribute("y", &y); pPositionElement->Attribute("z", &z); position = Vec3(x, y, z); } TiXmlElement* pOrientationElement = pData->FirstChildElement("YawPitchRoll"); if (pOrientationElement) { double yaw = 0; double pitch = 0; double roll = 0; pOrientationElement->Attribute("x", &yaw); pOrientationElement->Attribute("y", &pitch); pOrientationElement->Attribute("z", &roll); yawPitchRoll = Vec3(yaw, pitch, roll); } Mat4x4 translation; translation.BuildTranslation(position); Mat4x4 rotation; rotation.BuildYawPitchRoll((float)DEGREES_TO_RADIANS(yawPitchRoll.x), (float)DEGREES_TO_RADIANS(yawPitchRoll.y), (float)DEGREES_TO_RADIANS(yawPitchRoll.z)); /** // This is not supported yet. TiXmlElement* pLookAtElement = pData->FirstChildElement("LookAt"); if (pLookAtElement) { double x = 0; double y = 0; double z = 0; pLookAtElement->Attribute("x", &x); pLookAtElement->Attribute("y", &y); pLookAtElement->Attribute("z", &z); Vec3 lookAt((float)x, (float)y, (float)z); rotation.BuildRotationLookAt(translation.GetPosition(), lookAt, g_Up); } TiXmlElement* pScaleElement = pData->FirstChildElement("Scale"); if (pScaleElement) { double x = 0; double y = 0; double z = 0; pScaleElement->Attribute("x", &x); pScaleElement->Attribute("y", &y); pScaleElement->Attribute("z", &z); m_scale = Vec3((float)x, (float)y, (float)z); } **/ m_transform = rotation * translation; return true; }
void MovementController::OnUpdate(DWORD const deltaMilliseconds) { //if (m_bKey['Q']) //{ // // This code is a cheat to position the camera exactly in a given // // spot so I can take screen shots! // Mat4x4 camTranslate; // D3DXMatrixTranslation(&m_matPosition, 8.847f, 7.055f, 11.618f); // m_fTargetYaw = m_fYaw += -64.35f; // m_fTargetPitch = m_fPitch = 28.57f; // // Calculate the new rotation matrix from the camera // // yaw and pitch. // Mat4x4 matRot; // D3DXMatrixRotationYawPitchRoll(&matRot, DEGREES_TO_RADIANS(m_fYaw), DEGREES_TO_RADIANS(m_fPitch), 0); // // Create the new object-to-world matrix, and the // // new world-to-object matrix. // D3DXMatrixMultiply(&m_matToWorld, &matRot, &m_matPosition); // D3DXMatrixInverse(&m_matFromWorld, NULL, &m_matToWorld); // m_object->VSetTransform(&m_matToWorld, &m_matFromWorld); // return; //} bool bTranslating = false; Vec4 atWorld(0,0,0,0); Vec4 rightWorld(0,0,0,0); Vec4 upWorld(0,0,0,0); if (m_bKey['W'] || m_bKey['S']) { // In D3D, the "look at" default is always // the positive Z axis. Vec4 at = g_Forward4; if (m_bKey['S']) at *= -1; // This will give us the "look at" vector // in world space - we'll use that to move // the camera. atWorld = m_matToWorld.Xform(at); bTranslating = true; } if (m_bKey['A'] || m_bKey['D']) { // In D3D, the "right" default is always // the positive X axis. Vec4 right = g_Right4; if (m_bKey['A']) right *= -1; // This will give us the "right" vector // in world space - we'll use that to move // the camera rightWorld = m_matToWorld.Xform(right); bTranslating = true; } if (m_bKey[' '] || m_bKey['C'] || m_bKey['X']) { // In D3D, the "up" default is always // the positive Y axis. Vec4 up = g_Right4; if (!m_bKey[' ']) up *= -1; //Unlike strafing, Up is always up no matter //which way you are looking upWorld = up; bTranslating = true; } //Handling rotation as a result of mouse position { // The secret formula!!! Don't give it away! //If you are seeing this now, then you must be some kind of elite hacker! m_fYaw += (m_fTargetYaw - m_fYaw) * ( .35f ); m_fTargetPitch = MAX(-90, MIN(90, m_fTargetPitch)); m_fPitch += (m_fTargetPitch - m_fPitch) * ( .35f ); // Calculate the new rotation matrix from the camera // yaw and pitch. Mat4x4 matRot; matRot.BuildYawPitchRoll(DEGREES_TO_RADIANS(-m_fYaw), DEGREES_TO_RADIANS(m_fPitch), 0); // Create the new object-to-world matrix, and the // new world-to-object matrix. m_matToWorld = matRot * m_matPosition; m_matFromWorld = m_matToWorld.Inverse(); m_object->VSetTransform(&m_matToWorld, &m_matFromWorld); } if (bTranslating) { float elapsedTime = (float)deltaMilliseconds / 1000.0f; Vec3 direction = atWorld + rightWorld + upWorld; direction.Normalize(); // Ramp the acceleration by the elapsed time. float numberOfSeconds = 5.f; m_currentSpeed += m_maxSpeed * ( (elapsedTime*elapsedTime) / numberOfSeconds); if (m_currentSpeed > m_maxSpeed) m_currentSpeed = m_maxSpeed; direction *= m_currentSpeed; Vec3 pos = m_matPosition.GetPosition() + direction; m_matPosition.SetPosition(pos); m_matToWorld.SetPosition(pos); m_matFromWorld = m_matToWorld.Inverse(); m_object->VSetTransform(&m_matToWorld, &m_matFromWorld); } else { m_currentSpeed = 0.0f; } }
int InternalScriptExports::CreateActor(const char* actorArchetype, LuaPlus::LuaObject luaPosition, LuaPlus::LuaObject luaYawPitchRoll) { if (!luaPosition.IsTable()) { GCC_ERROR("Invalid object passed to CreateActor(); type = " + std::string(luaPosition.TypeName())); return INVALID_ACTOR_ID; } if (!luaYawPitchRoll.IsTable()) { GCC_ERROR("Invalid object passed to CreateActor(); type = " + std::string(luaYawPitchRoll.TypeName())); return INVALID_ACTOR_ID; } Vec3 pos(luaPosition["x"].GetFloat(), luaPosition["y"].GetFloat(), luaPosition["z"].GetFloat()); Vec3 ypr(luaYawPitchRoll["x"].GetFloat(), luaYawPitchRoll["y"].GetFloat(), luaYawPitchRoll["z"].GetFloat()); Mat4x4 initialTransform; initialTransform.BuildYawPitchRoll(ypr.x, ypr.y, ypr.z); initialTransform.SetPosition(pos); TiXmlElement *overloads = NULL; StrongActorPtr pActor = g_pApp->m_pGame->VCreateActor(actorArchetype, overloads, &initialTransform); if (pActor) { shared_ptr<EvtData_New_Actor> pNewActorEvent(GCC_NEW EvtData_New_Actor(pActor->GetId())); IEventManager::Get()->VQueueEvent(pNewActorEvent); return pActor->GetId(); } return INVALID_ACTOR_ID; }