bool LightLx::Initialize(ID3D11Device* device) { HRESULT hr; hr = GenerateViewMatrix(); if (FAILED(hr)) { return false; } hr = GenerateProjectionMatrix(); if (FAILED(hr)) { return false; } return true; }
void Camera::Update(float ElapsedTime){ //--------------------------------------------- //TODO: Camera shouldn't move beneath the floor // so we need to check with a heightmap or // for now probably just the y = -20 plane. //--------------------------------------------- bool MatrixChanged = false; Vector3 TargetPos = (mFollowingObject ? mObject->position : mFollowPosition); Vector3 ToMove = TargetPos - position; float dist = ToMove.length(); //1. Camera position //We only need to move if the camera is not at the player if( dist > 0 ){ //If the camera is really far away OR really close to the object and the object is not moving //then just teleport the camera to the exact object position if( dist > 100.0f || (mFollowingObject && !mObject->movement.isZeroLength() && dist < 0.1f) ){ position = TargetPos; }else{ //We are at a normal distance from the object. Let camera fly with normal speed //ToMove already has a length, so if the camera is further away from the player, //then the camera will go faster. //When the object is moving in a single direction then the camera will be (PlayerSpeed/2.0f) units behind the object position += ToMove * 2.0f * ElapsedTime; } MatrixChanged = true; } //2. Camera rotation if( mFreeRotation ){ if( mCamYawSpeed ){ if( mCamYawSpeed > 1.5f*PI ) mCamYawSpeed = 1.5f*PI; if( mCamYawSpeed < -1.5f*PI ) mCamYawSpeed = -1.5f*PI; yaw += mCamYawSpeed * ElapsedTime; MatrixChanged = true; DeAccelerate(mCamYawSpeed, 3.5f * ElapsedTime); //Deacceleration of 3.5 radians per second per second } if( mCamPitchSpeed ){ if( mCamPitchSpeed > PI ) mCamPitchSpeed = PI; else if( mCamPitchSpeed < -PI ) mCamPitchSpeed = -PI; pitch += mCamPitchSpeed * ElapsedTime; MatrixChanged = true; //Decrease the speed (which causes a natural 'slow down') DeAccelerate(mCamPitchSpeed, 3.5f * ElapsedTime); //Deacceleration of 3.5 radians per second per second } }else{ mCamYawSpeed = 0; mCamPitchSpeed = 0; if( mFollowingObject ){ if( yaw != mObject->yaw || pitch != mObject->pitch ) MatrixChanged = true; yaw = mObject->yaw; pitch = mObject->pitch; } } //3. Camera zoom if( mCamZoomSpeed ){ if( mCamZoomSpeed > 60 ) mCamZoomSpeed = 60; else if( mCamZoomSpeed < -60 ) mCamZoomSpeed = -60; //Adjust the zoom with the speed mCamDist += mCamZoomSpeed * ElapsedTime; MatrixChanged = true; //Make sure it does not zoom in or out too far if( mCamDist < MinCamDist ){ mCamDist = MinCamDist; mCamZoomSpeed = 0; } else if( mCamDist > MaxCamDist ){ mCamDist = MaxCamDist; mCamZoomSpeed = 0; } //Decrease the speed (which causes a natural 'slow down') DeAccelerate(mCamZoomSpeed, 700 * ElapsedTime); //Deacceleration of 700 units per second per second } if( MatrixChanged ) GenerateViewMatrix(); }