// Returns a direction vector going through the center of the requested cell (x, y) in the face nFace. CVector CPlanetaryMap::GetDirectionVector(int nFace, int x, int y) { int nHalf = m_nResolution >> 1; float fx = (0.5f + (x - nHalf)) / (float)nHalf; float fy = (0.5f + (y - nHalf)) / (float)nHalf; const float fArray[6][3] = {{1, -fy, -fx}, {-1, -fy, fx}, {fx, 1, fy}, {fx, -1, -fy}, {fx, -fy, 1}, {-fx, -fy, -1}}; CVector vDirection(fArray[nFace]); vDirection.Normalize(); return vDirection; }
void cParticlesFire::Setup(){ for (int i = 0; i < 100; ++i) { ST_PC_VERTEX v; v.c = D3DCOLOR_XRGB(128, 80, 13); /*ST_PARTICLE stP; stP._vertex.c = D3DCOLOR_XRGB(30, 80, 128);*/ //v.p = D3DXVECTOR3(0, 0, 100 - 10 * i); //v.p = D3DXVECTOR3(0, 0, 10 * i); D3DXVECTOR3 vDirection(0, 1, 0); D3DXMATRIXA16 rot, rotX, rotY, rotZ; D3DXMatrixRotationX(&rotX, D3DXToRadian(rand() % 361)); D3DXMatrixRotationY(&rotY, D3DXToRadian(rand() % 361)); D3DXMatrixRotationZ(&rotZ, D3DXToRadian(rand() % 361)); rot = rotX*rotY*rotZ; D3DXMATRIXA16 localMat; localMat = rot; D3DXVECTOR3 localPos; D3DXVec3TransformCoord(&localPos, &vDirection, &localMat); localPos = localPos * ((rand() % 200) / 2000.0f); v.p = localPos; ST_PARTICLE stP; stP._vInitialPos = v.p; stP._lifeSpan = (rand() % 400) / 200.0f; stP._currentTime = 0.0f; stP._speed = (rand() % 200) / 200.0f; /*D3DXMatrixRotationX(&rotX, D3DXToRadian(rand() % 361)); D3DXMatrixRotationY(&rotY, D3DXToRadian(rand() % 361)); D3DXMatrixRotationZ(&rotZ, D3DXToRadian(rand() % 361));*/ D3DXMatrixRotationX(&rotX, D3DXToRadian(0)); D3DXMatrixRotationY(&rotY, D3DXToRadian(0)); D3DXMatrixRotationZ(&rotZ, D3DXToRadian(0)); rot = rotX*rotY*rotZ; localMat = rot; D3DXVECTOR3 vDir; D3DXVec3TransformNormal(&vDir, &vDirection, &localMat); stP._direction = vDir; m_vecVertex.push_back(v); m_vecMoveInfo.push_back(stP); } }
quaternion(vector3 vDir) { vector3 vUp (vDir); vector3 vDirection (1, 0, 0); vector3 vRight = vUp.crossProduct(vDirection); // Step 2. Put the three vectors into the matrix to bulid a basis rotation matrix // This step isnt necessary, but im adding it because often you would want to convert from matricies to quaternions instead of vectors to quaternions // If you want to skip this step, you can use the vector values directly in the quaternion setup below matrix4x4 mBasis= matrix4x4(vRight.x, vRight.y, vRight.z, 0.0f, vUp.x, vUp.y, vUp.z, 0.0f, vDirection.x, vDirection.y, vDirection.z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); // Step 3. Build a quaternion from the matrix w = (float)Ogre::Math::Sqrt(1.0f + mBasis[0][0] + mBasis[1][1] + mBasis[2][2]) / 2.0f; double dfWScale = w * 4.0; x = (float)((mBasis[2][1] - mBasis[1][2]) / dfWScale); y = (float)((mBasis[0][2] - mBasis[2][0]) / dfWScale); z = (float)((mBasis[1][0] - mBasis[0][1]) / dfWScale); }
void MeshGeodesic::Transport( const Wml::Vector2f & vDirectionIn, const rms::Frame3f * pPrevFrame ) { Wml::Vector2f vDirection(vDirectionIn); // align frames rms::Frame3f vStartFrame = m_vOptPath[0]; if ( pPrevFrame ) { vStartFrame = *pPrevFrame; vStartFrame.Origin() = m_vOptPath[0].Origin(); vStartFrame.AlignZAxis(m_vOptPath[0].Z()); } size_t nCount = m_vOptPath.size(); for ( unsigned int i = 0; i < nCount; ++i ) { rms::Frame3f vCurFrame( m_vOptPath[i] ); rms::Frame3f vNewFrame(vStartFrame); vNewFrame.AlignZAxis(vCurFrame.Z()); vNewFrame.Origin() = vCurFrame.Origin(); m_vOptPath[i] = vNewFrame; } float fLength = vDirection.Length() * 1.5f; for ( unsigned int i = 0; i < nCount; ++i ) { rms::Frame3f & vCenter = m_vOptPath[i]; m_pExpGen->SetSurfaceDistances(vCenter.Origin(), 0.0f, fLength, &vCenter ); // generate mesh caches m_pExpGen->MeshCurrentUVs(); Wml::Vector3f vNew3D, vNewNormal; vNew3D = m_pExpGen->Find3D( vDirection, &vNewNormal ); vCenter.Origin() = vNew3D; vCenter.AlignZAxis( vNewNormal ); } }
void cParticlesFire::Update(float fDelta){ for (size_t i = 0; i < m_vecVertex.size(); i++){ m_vecVertex[i].p = m_vecVertex[i].p + (m_vecMoveInfo[i]._direction * m_vecMoveInfo[i]._speed * fDelta); float alpha = 1.0f - (m_vecMoveInfo[i]._currentTime / m_vecMoveInfo[i]._lifeSpan); if (alpha < 0.0f){ alpha = 0.0f; } m_vecVertex[i].c = D3DXCOLOR(128 / 255.0f, 80 / 255.0f, 13 / 255.0f, alpha); m_vecMoveInfo[i]._currentTime += fDelta; if (m_vecMoveInfo[i]._currentTime > m_vecMoveInfo[i]._lifeSpan){ m_vecMoveInfo[i]._currentTime = 0.0f; m_vecVertex[i].p = m_vecMoveInfo[i]._vInitialPos; m_vecMoveInfo[i]._lifeSpan = (rand() % 400) / 200.0f; m_vecMoveInfo[i]._currentTime = 0.0f; m_vecMoveInfo[i]._speed = (rand() % 200) / 200.0f; D3DXMATRIXA16 rot, rotX, rotY, rotZ; D3DXMatrixRotationX(&rotX, D3DXToRadian(0)); D3DXMatrixRotationY(&rotY, D3DXToRadian(0)); D3DXMatrixRotationZ(&rotZ, D3DXToRadian(0)); rot = rotX*rotY*rotZ; D3DXVECTOR3 vDirection(0, 1, 0); D3DXMATRIXA16 localMat; localMat = rot; D3DXVECTOR3 vDir; D3DXVec3TransformNormal(&vDir, &vDirection, &localMat); m_vecMoveInfo[i]._direction = vDir; } } }
void VTimeOfDayComponent::UpdateParent() { IVTimeOfDay *pTimeOfDayInterface = Vision::Renderer.GetTimeOfDayHandler(); if (pTimeOfDayInterface == NULL) return; VisObject3D_cl *pOwnerObject = (VisObject3D_cl *)m_pOwner; VASSERT(pOwnerObject); hkvVec3 vDirection(hkvNoInitialization); pTimeOfDayInterface->GetSunDirection(vDirection); vDirection.normalizeIfNotZero(); if (AttachmentType == TIMEOFDAY_ATTACHMENT_MOONLIGHTSOURCE) { vDirection = -vDirection; } else if(AttachmentType == TIMEOFDAY_ATTACHMENT_SUNBACKLIGHTSOURCE) { vDirection.x = -vDirection.x; vDirection.y = -vDirection.y; } if (AttachmentType != TIMEOFDAY_ATTACHMENT_ENABLEDATNIGHTLIGHTSOURCE) { pOwnerObject->SetDirection(vDirection); } if (AttachmentType == TIMEOFDAY_ATTACHMENT_CORONALIGHTSOURCE) { // TODO (multiple renderer nodes) IVRendererNode *pRenderer = Vision::Renderer.GetRendererNode(0); float fNear, fFar; pRenderer->GetReferenceContext()->GetClipPlanes(fNear, fFar); hkvVec3 vCamPos = pRenderer->GetReferenceContext()->GetCamera()->GetPosition(); hkvVec3 vCoronaPos = -vDirection; vCoronaPos *= 0.95f * fFar; vCoronaPos += vCamPos; pOwnerObject->SetPosition(vCoronaPos); } if (m_bIsLightClass) { VisLightSource_cl *pLight = (VisLightSource_cl*)m_pOwner; VColorRef sunColor = pTimeOfDayInterface->GetSunColor(); bool bSwitchable = (AttachmentType == TIMEOFDAY_ATTACHMENT_ENABLEDATNIGHTLIGHTSOURCE); float fBelowHorizonMultiplier = hkvMath::pow (hkvMath::Max(-vDirection.z+0.1f, 0.0f), bSwitchable ? 1.0f : 0.1f); fBelowHorizonMultiplier = hkvMath::Min(1.0f, fBelowHorizonMultiplier); if (bSwitchable && fBelowHorizonMultiplier < 1.0f && fBelowHorizonMultiplier > 0.f) { pLight->SetColor(m_iColor); pLight->SetMultiplier(Intensity * (1.0f - fBelowHorizonMultiplier)); } else if (AttachmentType == TIMEOFDAY_ATTACHMENT_SUNLIGHTSOURCE) { pLight->SetColor(sunColor); pLight->SetMultiplier(Intensity * fBelowHorizonMultiplier); } else if ((AttachmentType == TIMEOFDAY_ATTACHMENT_MOONLIGHTSOURCE) || (AttachmentType == TIMEOFDAY_ATTACHMENT_SUNBACKLIGHTSOURCE)) { // TODO VColorRef negativeColor = V_RGBA_WHITE - sunColor; pLight->SetColor(negativeColor); pLight->SetMultiplier(Intensity * fBelowHorizonMultiplier * 0.333f); } else if (AttachmentType == TIMEOFDAY_ATTACHMENT_CORONALIGHTSOURCE) { hkvVec3 vSunColorFloat = sunColor.ToFloat(); float fLargestComponent = hkvMath::Max(hkvMath::Max(vSunColorFloat.x, vSunColorFloat.y), vSunColorFloat.z); if (fLargestComponent <= 0.0f) fLargestComponent = 1.0f; sunColor.FromFloat(vSunColorFloat * (1.0f / fLargestComponent)); pLight->SetColor(sunColor * fBelowHorizonMultiplier); pLight->SetMultiplier(0.0f); } } }
void CMole::ComputeRepulsorForce(CFVec2& _vrRepelForce) { // Get wall repulsors CWall* pWalls = 0; int iNumWalls = 0; // Get mole repulsors CMole* pMoles = 0; int iNumMoles = 0; CMMMContext& rMMMContext = CMMMContext::GetInstance(); rMMMContext.GetWalls(pWalls, iNumWalls); rMMMContext.GetMoles(pMoles, iNumMoles); // Get repulsors std::vector<CRepulsor*> m_aRepulsors; for (int i = 0; i < iNumWalls; ++ i) { m_aRepulsors.push_back( &pWalls[i].GetRepulsor(0) ); m_aRepulsors.push_back( &pWalls[i].GetRepulsor(1) ); } for (int i = 0; i < iNumMoles; ++ i) { if (&pMoles[i] != this) m_aRepulsors.push_back( pMoles[i].GetRepulsor() ); } for (unsigned int j = 0; j < m_aRepulsors.size(); ++ j) { CFVec2 vRepulseForce(0.0f, 0.0f); CFVec2 vRepulsorPosition = m_aRepulsors[j]->GetPosition(); float fRepulsorRadius = m_aRepulsors[j]->GetRadius(); float fRepulsionRadius = m_aRepulsors[j]->GetRepulsionRadius(); // Displacement to repulsor CFVec2 vDisplacement(m_vPosition); vDisplacement -= vRepulsorPosition; // Direction to repulsor CFVec2 vDirection(vDisplacement); vDirection.Normalise(); // Distance to repulsor float fDistance(vDisplacement.Magnitude()); fDistance -= fRepulsorRadius; fDistance -= m_fRadius; // Within repulsion radius if (fDistance < fRepulsionRadius) { // How far into repulsion radius float fRepulsionRatio = 1.0f - (fDistance / fRepulsionRadius); // Opposite direction CFVec2 vOppositeDirection(vDirection); vOppositeDirection.Normalise(); // Apply force opposite direction to repulsor vRepulseForce = vOppositeDirection; vRepulseForce *= m_aRepulsors[j]->GetMultiplier(); vRepulseForce *= fRepulsionRatio; _vrRepelForce += vRepulseForce; } } }
Facing NounShip::getFacing( const Vector3 & vWorld ) { Vector3 vDirection( worldFrame() * (vWorld - worldPosition()) ); vDirection.normalize(); return getFacing( atan2( vDirection.x, vDirection.z ), false ); }
void CAtlas::handleMessage(const CMessage &message) { if (message.getType() == MSG_SWITCH_MAP) { if (m_bSwitchingMaps) {return;} const CMessageSwitchMap &switch_map_message(dynamic_cast<const CMessageSwitchMap&>(message)); if (switch_map_message.getStatus() == CMessageSwitchMap::INJECT) { LOGI("Atlas: changing map to '%s'", switch_map_message.getMap().c_str()); // get new switch type m_eSwitchMapType = switch_map_message.getSwitchMapType(); m_sNextMap = switch_map_message.getMap(); m_sNextMapEntrance = switch_map_message.getTargetEntrance(); m_bSwitchingMaps = true; if (m_eSwitchMapType == SMT_MOVE_CAMERA) { // player position in new map Ogre::Vector3 vPlayerPos, vPlayerMoveToPos; // create next map m_pNextMap = new CMap(this, CMapPackPtr(new CMapPack(CFileManager::getResourcePath("maps/Atlases/LightWorld/"), switch_map_message.getMap())), m_pSceneNode, m_pPlayer); mFirstFrame = true; CMapPackPtr nextPack = m_pNextMap->getMapPack(); CMapPackPtr currPack = m_pCurrentMap->getMapPack(); Ogre::Vector3 vMapPositionOffset = currPack->getGlobalPosition() - nextPack->getGlobalPosition(); m_pCurrentMap->moveMap(vMapPositionOffset); // determine direction Ogre::Vector3 vDirection(Ogre::Vector3::ZERO); if (abs(nextPack->getGlobalPosition().x + nextPack->getGlobalSize().x - currPack->getGlobalPosition().x) < 0.01) { vDirection.x = -1; } else if (abs(nextPack->getGlobalPosition().z + nextPack->getGlobalSize().y - currPack->getGlobalPosition().z) < 0.01) { vDirection.z = -1; } else if (abs(currPack->getGlobalPosition().x + currPack->getGlobalSize().x - nextPack->getGlobalPosition().x) < 0.01) { vDirection.x = 1; } else if (abs(currPack->getGlobalPosition().z + currPack->getGlobalSize().y - nextPack->getGlobalPosition().z) < 0.01) { vDirection.z = 1; } vPlayerPos = m_pPlayer->getPosition() + vMapPositionOffset; vPlayerMoveToPos = vPlayerPos + vDirection * 0.2; // change players map m_pPlayer->enterMap(m_pNextMap, vPlayerMoveToPos); m_pPlayer->setPosition(vPlayerPos); m_bPlayerTargetReached = false; CMessageHandler::getSingleton().addMessage(new CMessageSwitchMap(m_pNextMap->getMapPack()->getName(), CMessageSwitchMap::SWITCHING, m_eSwitchMapType, m_pCurrentMap, m_pNextMap, m_sNextMapEntrance)); } else if (m_eSwitchMapType == SMT_FADE_ALPHA) { pause(PAUSE_PLAYER_UPDATE | PAUSE_MAP_UPDATE); mAlphaFader.startFadeOut(1); } else if (m_eSwitchMapType == SMT_FADE_ELLIPTIC) { mEllipticFader.startFadeOut(1); pause(PAUSE_PLAYER_UPDATE | PAUSE_MAP_UPDATE); } } } else if (message.getType() == MSG_TARGET_REACHED) { const CMessageTargetReached &message_target_reached(dynamic_cast<const CMessageTargetReached &>(message)); if (message_target_reached.getEntity() == m_pPlayer) { if (m_bSwitchingMaps) { CMap *pMap; if (m_eSwitchMapType != SMT_MOVE_CAMERA) { m_bSwitchingMaps = false; pMap = m_pCurrentMap; } else { pMap = m_pNextMap; } m_bPlayerTargetReached = true; unpause(PAUSE_ALL); pMap->start(); CMessageHandler::getSingleton().addMessage(new CMessageSwitchMap(m_pCurrentMap->getMapPack()->getName(), CMessageSwitchMap::FINISHED, m_eSwitchMapType, pMap, nullptr, m_sNextMapEntrance)); } } } }
void VTimeOfDayComponent::UpdateParent() { VTimeOfDay *pTimeOfDayInterface = (VTimeOfDay*)Vision::Renderer.GetTimeOfDayHandler(); if (pTimeOfDayInterface == NULL) return; VisObject3D_cl *pOwnerObject = (VisObject3D_cl *)m_pOwner; VASSERT(pOwnerObject); hkvVec3 vDirection(hkvNoInitialization); pTimeOfDayInterface->GetSunDirection(vDirection); // The Moon and back light direction is calculated from the Sun direction if (AttachmentType == TIMEOFDAY_ATTACHMENT_MOONLIGHTSOURCE) { vDirection = -vDirection; } else if(AttachmentType == TIMEOFDAY_ATTACHMENT_SUNBACKLIGHTSOURCE) { vDirection.x = -vDirection.x; vDirection.y = -vDirection.y; } if (AttachmentType != TIMEOFDAY_ATTACHMENT_ENABLEDATNIGHTLIGHTSOURCE) { pOwnerObject->SetDirection(vDirection); } if (AttachmentType == TIMEOFDAY_ATTACHMENT_CORONALIGHTSOURCE) { // TODO (multiple renderer nodes) IVRendererNode *pRenderer = Vision::Renderer.GetRendererNode(0); float fNear, fFar; pRenderer->GetReferenceContext()->GetClipPlanes(fNear, fFar); hkvVec3 vCamPos = pRenderer->GetReferenceContext()->GetCamera()->GetPosition(); hkvVec3 vCoronaPos = -vDirection; vCoronaPos *= 0.95f * fFar; vCoronaPos += vCamPos; pOwnerObject->SetPosition(vCoronaPos); } // Set the color and intensity of the light if (m_bIsLightClass) { VisLightSource_cl *pLight = (VisLightSource_cl*)m_pOwner; VColorRef color; float intensity = 0.0f; switch (AttachmentType) { case TIMEOFDAY_ATTACHMENT_ENABLEDATNIGHTLIGHTSOURCE: { color = m_iColor; const float fMarginNearHorizon = 0.1f; //10% - Margin above the horizon to switch lights ON/OFF const float fBelowHorizonMultiplier = hkvMath::Max(-vDirection.z + fMarginNearHorizon, 0.0f); intensity = 1.0f - hkvMath::Min(1.0f, fBelowHorizonMultiplier); break; } case TIMEOFDAY_ATTACHMENT_CORONALIGHTSOURCE: { color = pTimeOfDayInterface->GetSunColor(); hkvVec3 vSunColorFloat = color.ToFloat(); float fLargestComponent = hkvMath::Max(hkvMath::Max(vSunColorFloat.x, vSunColorFloat.y), vSunColorFloat.z); if (fLargestComponent > 0.0f) { color.FromFloat(vSunColorFloat / fLargestComponent); } intensity = 0.0f; break; } case TIMEOFDAY_ATTACHMENT_SUNLIGHTSOURCE: { color = pTimeOfDayInterface->GetSunColor(); intensity = pTimeOfDayInterface->GetSunIntensity(); break; } case TIMEOFDAY_ATTACHMENT_SUNBACKLIGHTSOURCE: { color = pTimeOfDayInterface->GetBackLightColor(); intensity = pTimeOfDayInterface->GetBackLightIntensity(); break; } case TIMEOFDAY_ATTACHMENT_MOONLIGHTSOURCE: { color = pTimeOfDayInterface->GetMoonColor(); intensity = pTimeOfDayInterface->GetMoonIntensity(); break; } default: VASSERT_MSG(0,"Unknown time of day attachment type"); } pLight->SetColor(color); pLight->SetMultiplier(intensity * Intensity); } }