void MyCamera::Update(){ auto CntlVec = App::GetApp()->GetInputDevice().GetControlerVec(); //前回のターンからの時間 float ElapsedTime = App::GetApp()->GetElapsedTime(); if (CntlVec[0].bConnected){ Vector3 NewAt(0, 0, 0); auto TargetPtr = GetTargetObject(); if (TargetPtr){ //目指したい場所 Vector3 ToAt = TargetPtr->GetComponent<Transform>()->GetPosition(); NewAt = Lerp::CalculateLerp(GetAt(), ToAt, 0, 1.0f, m_ToTargetLerp, Lerp::Linear); } //ステップ1、注視点と位置の変更 Vector3 Span = GetAt() - GetEye(); Vector3 NewEye = NewAt - Span; SetAt(NewAt); SetEye(NewEye); //ステップ2、ズームの変更 //カメラ位置と注視点の間のベクトルを算出 Span = GetAt() - GetEye(); //正規化 Span.Normalize(); //変化値の決定 Span = Span * ElapsedTime * 10.0f; Vector3 NewArm = GetAt() - GetEye(); //Dパッド下 //カメラを引く if (CntlVec[0].wButtons & XINPUT_GAMEPAD_DPAD_DOWN){ //カメラ位置を引く NewEye = NewEye - Span; NewArm = NewAt - NewEye; if (NewArm.Length() > (GetFar() * 0.1f)){ NewEye = NewEye + Span; NewArm = NewAt - NewEye; } } //Dパッド上 //カメラを寄る if (CntlVec[0].wButtons & XINPUT_GAMEPAD_DPAD_UP){ //カメラ位置を寄る NewEye = NewEye + Span; NewArm = NewAt - NewEye; if (NewArm.Length() < GetNear() * 2.0f){ NewEye = NewEye - Span; NewArm = NewAt - NewEye; } } SetAt(NewAt); SetEye(NewEye); //ステップ3角度の変更 //現在のAtとEyeの角度を得る Vector3 ArmInv = GetEye() - GetAt(); //右スティックX方向 FLOAT AngleY = 0; //右スティックY方向 FLOAT AngleX = 0; FLOAT AngleZ = 0; if (CntlVec[0].fThumbRX != 0){ //右スティックを聞かないようにする // AngleY = -CntlVec[0].fThumbRX * ElapsedTime; } if (CntlVec[0].fThumbRY != 0){ //右スティックを聞かないようにする // AngleX = CntlVec[0].fThumbRY * ElapsedTime; // AngleZ = CntlVec[0].fThumbRY * ElapsedTime; } if (ArmInv.z > 0){ AngleX *= -1.0f; } if (ArmInv.x < 0){ AngleZ *= -1.0f; } Quaternion QtSpan(AngleX, AngleY, AngleZ); QtSpan.Normalize(); //回転先計算の行列を作成 Matrix4X4 Mat, Mat2; Mat.STRTransformation( Vector3(1.0f, 1.0f, 1.0f), ArmInv, QtSpan); Mat2.TranslationFromVector(GetAt()); Mat *= Mat2; NewEye = Mat.PosInMatrix(); if (NewEye.y < 0.5f){ NewEye.y = 0.5f; } //カメラが一定以上、上から視線にならなように調整 ArmInv = NewEye - GetAt(); ArmInv.Normalize(); float y2 = ArmInv.y * ArmInv.y; float x2 = ArmInv.x * ArmInv.x; float z2 = ArmInv.z * ArmInv.z; if (y2 <= (x2 + z2)){ SetEye(NewEye); } } Camera::Update(); }
void FPSCamera::Update( float _DeltaTime, float _TranslationSpeed, float _RotationSpeed, float _SpeedBoostWithShift ) { ////////////////////////////////////////////////////////////////////////// // Handle mouse manipulation // if ( gs_WindowInfos.Events.Mouse.dbuttons[0] > 0 ) { // Button down SetCapture( gs_WindowInfos.hWnd ); m_ButtonDownMouseX = gs_WindowInfos.Events.Mouse.x; m_ButtonDownMouseY = gs_WindowInfos.Events.Mouse.y; m_ButtonDownPosition = m_Position; m_ButtonDownTarget = m_Target; } else if ( gs_WindowInfos.Events.Mouse.dbuttons[0] < 0 ) { // Button up ReleaseCapture(); } if ( gs_WindowInfos.Events.Mouse.buttons[0] != 0 ) { int MouseDx = gs_WindowInfos.Events.Mouse.x - m_ButtonDownMouseX; int MouseDy = gs_WindowInfos.Events.Mouse.y - m_ButtonDownMouseY; float DAngleX = (_RotationSpeed * TWOPI) * MouseDx / RESX; float DAngleY = (_RotationSpeed * PI) * MouseDy / RESY; NjFloat3 At = m_ButtonDownTarget - m_ButtonDownPosition; float Distance2Target = At.Length(); At = At / Distance2Target; float Theta = asinf( At.y ); float Phi = atan2f( At.x, At.z ); Theta = CLAMP( Theta - DAngleY, -0.99f * HALFPI, +0.99f * HALFPI ); // Never completly up or down to avoid gimbal lock Phi -= DAngleX; NjFloat3 NewAt( sinf(Phi)*cosf(Theta), sinf(Theta), cosf(Phi)*cos(Theta) ); m_Target = m_Position + Distance2Target * NewAt; // Vector3 Euler = GetEuler( m_ButtonDownTransform ); // Matrix CamRotYMatrix = Matrix.RotationY( fAngleY + Euler.Y ); // Matrix CamRotXMatrix = Matrix.RotationX( fAngleX + Euler.X ); // Matrix CamRotZMatrix = Matrix.RotationZ( Euler.Z ); // // Matrix RotateMatrix = CamRotXMatrix * CamRotYMatrix * CamRotZMatrix; } NjFloat3 At = (m_Target - m_Position).Normalize(); NjFloat3 Right = (At ^ m_Up).Normalize(); NjFloat3 Up = Right ^ At; ////////////////////////////////////////////////////////////////////////// // Handle keyboard manipulation // float Speed = _DeltaTime * _TranslationSpeed; if ( gs_WindowInfos.Events.Keyboard.State[KEY_LSHIFT] ) Speed *= _SpeedBoostWithShift; NjFloat3 Delta = NjFloat3::Zero; if ( gs_WindowInfos.pKeys['Q'] ) { // Strafe left Delta = Delta - Speed * Right; } if ( gs_WindowInfos.pKeys['D'] ) { // Strafe right Delta = Delta + Speed * Right; } if ( gs_WindowInfos.pKeys['Z'] ) { // Forward Delta = Delta + Speed * At; } if ( gs_WindowInfos.pKeys['S'] ) { // Backward Delta = Delta - Speed * At; } if ( gs_WindowInfos.pKeys[' '] ) { // Up Delta = Delta + Speed * Up; } if ( gs_WindowInfos.Events.Keyboard.State[KEY_LCONTROL] ) { // Down Delta = Delta - Speed * Up; } m_Position = m_Position + Delta; m_Target = m_Target + Delta; ////////////////////////////////////////////////////////////////////////// // Rebuild camera matrix m_Camera.LookAt( m_Position, m_Target, m_Up ); }