void CustomCam1::rotateCamVertical(float degrees) { if (degrees + f_currentPitch > f_pitchLimit && degrees > 0) { degrees = f_pitchLimit - f_currentPitch; if (f_currentPitch >= f_pitchLimit) { degrees = 0.f; } } else if (degrees + f_currentPitch < -f_pitchLimit && degrees < 0) { degrees = -f_pitchLimit - f_currentPitch; if (f_currentPitch <= -f_pitchLimit) { degrees = 0.f; } } Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotation; target -= position; f_currentPitch += degrees; rotation.SetToRotation(static_cast<float>(degrees), right.x, right.y, right.z); target = rotation * target; target += position; }
void CTransform::SetRotate( const float angle, const float rx, const float ry, const float rz, bool self) { Mtx44 MtxBackToPosition; if (self) { Mtx44 MtxBackToOrigin; MtxBackToOrigin.a[12] = -Mtx.a[12]; MtxBackToOrigin.a[13] = -Mtx.a[13]; MtxBackToOrigin.a[14] = -Mtx.a[14]; MtxBackToPosition.a[12] = Mtx.a[12]; MtxBackToPosition.a[13] = Mtx.a[13]; MtxBackToPosition.a[14] = Mtx.a[14]; Mtx = Mtx + MtxBackToOrigin; } Mtx44 TempMtx; TempMtx.SetToRotation(angle, rx, ry, rz); Mtx = Mtx * TempMtx ; if (self) { Mtx = Mtx + MtxBackToPosition; } }
void AI::movementFB(double &dt, bool forward) { Mtx44 rotation; if (forward) { if (e_State == ATTACK) { f_movementSpeed = f_move_run; } else { f_movementSpeed = f_move_walk; } vel += (getDirection(true).Normalize() * (f_movementSpeed)) * static_cast<float>(dt); } else { Lookat = Lookat - pos; rotation.SetToRotation(720 * static_cast<float>(dt), 0, 1, 0); Lookat = rotation * Lookat; Lookat = Lookat + pos; //Velocity += (getDirection(true).Normalize() * f_movementSpeed) * static_cast<float>(dt); } }
void FPcamera::SpinCounterClockwise(const double dt) { float angle = (float)(-TURN_SPEED * (float)dt); view = (target - position).Normalized(); Mtx44 rotation; rotation.SetToRotation(angle, 0, 0, 1); view = rotation * view; target = position + view; }
void CameraComponent::LookAround(double dt) { auto infoC = this->getParent()->getComponent<InformationComponent>(); if (infoC) { Mtx44 rotation; rotation.SetToRotation(m_Camera->getCameraYaw(),0, 1, 0); Vector3 curDirection = infoC->getDirection().Normalized(); curDirection = rotation * curDirection; infoC->setDirection(curDirection); } }
void CPlayInfo3PV::UpdateDir(float yaw, float pitch) { // Yaw Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); curDirection = rotation * curDirection; Vector3 right = curDirection.Cross(curUp); right.y = 0; right.Normalize(); curUp = right.Cross(curDirection).Normalized(); // Pitch //rotation.SetToRotation(pitch, 1, 0, 0); right = curDirection.Cross(curUp); right.y = 0; right.Normalize(); curUp = right.Cross(curDirection).Normalized(); rotation.SetToRotation(pitch, right.x, right.y, right.z); curDirection = rotation * curDirection; }
void AI::movementLR(double &dt, bool left, float rotation_speed) { Mtx44 rotation; if (left == true) { Lookat = Lookat - pos; rotation.SetToRotation(-rotation_speed * static_cast<float>(dt), 0, 1, 0); Lookat = rotation * Lookat; Lookat = Lookat + pos; //Velocity += (getDirection(true).Normalize() * f_movementSpeed) * static_cast<float>(dt); } else { Lookat = Lookat - pos; rotation.SetToRotation(rotation_speed * static_cast<float>(dt), 0, 1, 0); Lookat = rotation * Lookat; Lookat = Lookat + pos; //Velocity += (getDirection(true).Normalize() * f_movementSpeed) * static_cast<float>(dt); } }
/******************************************************************************** LookDown ********************************************************************************/ void TPCamera::LookDown(const double dt) { //float pitch = (float)(-CAMERA_SPEED * Application::camera_pitch * (float)dt); float pitch = (float)(CAMERA_SPEED * (float)dt); Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); view = rotation * view; target = position + view; }
void FPcamera::lookUp(const double dt) { float pitch = (float)(-TURN_SPEED * Application::camera_pitch * (float)dt); rotationX -= pitch; view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); view = rotation * view; target = position + view; }
void FPcamera::lookRight(const double dt) { view = (target - position).Normalized(); float yaw = (float)(-TURN_SPEED * Application::camera_yaw * (float)dt); rotationY += yaw; Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); view = rotation * view; target = position + view; Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); }
/******************************************************************************** Turn right ********************************************************************************/ void TPCamera::TurnRight(const double dt) { Vector3 view = (target - position).Normalized(); // float yaw = (float)(-CAMERA_SPEED * Application::camera_yaw * (float)dt); float yaw = (float)(-CAMERA_SPEED * (float)dt); Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); view = rotation * view; target = position + view; Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); }
/******************************************************************************** LookRight ********************************************************************************/ void Camera3::LookRight(const double dt) { float yaw = yawVelocity; Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); view = rotation * view; target = position + view; up = rotation * up; }
void FPcamera::lookDown(const double dt, float downValue) { float pitch = (float)(-WALK_SPEED * Application::camera_pitch * (float)dt - downValue * (float)dt); recoil -= 5.f * (float)dt;; rotationX -= pitch; view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); view = rotation * view; target = position + view; }
void FPS_Cam::LookPitch(float pitchValue, double dt) { float mspeed = mouse_speed * pitchValue * dt; FaceDirection = (target - position).Normalized(); Vector3 right = FaceDirection.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(FaceDirection).Normalized(); rotation22.SetToRotation(mspeed, right.x, right.y, right.z); FaceDirection = rotation22 * FaceDirection; target = position + FaceDirection; up = rotation22 * up; }
void FPS_Cam::LookYaw(float yawValue, double dt) { float mspeed = mouse_speed * yawValue; FaceDirection = (target - position).Normalized(); Vector3 right = FaceDirection.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(FaceDirection).Normalized(); rotation22.SetToRotation(mspeed * dt, 0, 1, 0); FaceDirection = rotation22 * FaceDirection; target = position + FaceDirection; up = rotation22 * up; }
void FPcamera::lookLeft(const double dt) { //Update the camera direction based on mouse move // left-right rotate view = (target - position).Normalized(); float yaw = (float)(-TURN_SPEED * Application::camera_yaw * (float)dt); rotationY += yaw; Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); view = rotation * view; target = position + view; Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); }
/******************************************************************************** LookDown ********************************************************************************/ void Camera3::LookDown(const double dt) { float pitch = pitchVelocity; //Controller::incrementPitchAngle(pitch); Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); view = rotation * view; target = position + view; up = rotation * up; }
void AI::ai_ScanArea(double &dt) { b_updateAI = false; b_aiScanning = true; static double rotationSpeed = 50.f; if(b_rotateClockwiseFirst == NULL) { b_rotateClockwiseFirst = static_cast<bool>(Math::RandIntMinMax(0, 1)); } d_totalRotation += rotationSpeed * dt; //Scan if(d_totalRotation < 90) { if(b_rotateClockwiseFirst) d_enemyRotation = -rotationSpeed * dt; else d_enemyRotation = rotationSpeed * dt; } else if (d_totalRotation >= 90 && d_totalRotation < 270) { if(b_rotateClockwiseFirst) d_enemyRotation = rotationSpeed * dt; else d_enemyRotation = -rotationSpeed * dt; } else { e_State = WALKING; d_totalRotation = 0.f; b_updateAI = true; b_rotateClockwiseFirst = NULL; b_aiScanning = false; } Mtx44 rotation; Lookat = Lookat - pos; rotation.SetToRotation(static_cast<float>(d_enemyRotation), 0, 1, 0); Lookat = rotation * Lookat; Lookat = Lookat + pos; }
/******************************************************************************** Hero Move Left Right ********************************************************************************/ void CPlayInfo3PV::MoveLeftRight(const bool mode, const float timeDiff) { Mtx44 rotation; rotation.SetToRotation(90, 0, 1, 0); // Dir without moving y axis Vector3 tempTarget = curPosition + curDirection; tempTarget.y = curPosition.y; Vector3 tempDir = (tempTarget - curPosition).Normalized(); Vector3 right = rotation * tempDir; if (mode) { curPosition += right * (CAMERA_SPEED * timeDiff); //curPosition.x = curPosition.x + (int) (200.0f * timeDiff); } else { curPosition -= right * (CAMERA_SPEED * timeDiff); //curPosition.x = curPosition.x - (int) (200.0f * timeDiff); } }
void Camera3::Update(double dt) { static const float CAMERA_SPEED = 80.f; static const float MOVEMENT_SPEED = 30.f; if (Application::IsKeyPressed('W')) { Vector3 camDir = (target - position).Normalized(); position += (camDir * MOVEMENT_SPEED * dt); target += (camDir * MOVEMENT_SPEED * dt); } if (Application::IsKeyPressed('S')) { Vector3 camDir = (target - position).Normalized(); position -= (camDir * MOVEMENT_SPEED * dt); target -= (camDir * MOVEMENT_SPEED * dt); } if (Application::IsKeyPressed('A')) { Vector3 camDir = (target - position).Normalized(); Vector3 leftDir = (up.Cross(camDir)).Normalized(); position += (leftDir * MOVEMENT_SPEED * dt); target += (leftDir * MOVEMENT_SPEED * dt); } if (Application::IsKeyPressed('D')) { Vector3 camDir = (target - position).Normalized(); Vector3 rightDir = (camDir.Cross(up)).Normalized(); position += (rightDir * MOVEMENT_SPEED * dt); target += (rightDir * MOVEMENT_SPEED * dt); } if (Application::IsKeyPressed(VK_LEFT)) { float yaw = (float)(CAMERA_SPEED * dt); Mtx44 rotate; rotate.SetToRotation(yaw, 0, 1, 0); target.x -= position.x; target.y -= position.y; target.z -= position.z; target = rotate * target; target.x += position.x; target.y += position.y; target.z += position.z; up = rotate * up; } if (Application::IsKeyPressed(VK_RIGHT)) { float yaw = (float)(-CAMERA_SPEED * dt); Mtx44 rotate; rotate.SetToRotation(yaw, 0, 1, 0); target.x -= position.x; target.y -= position.y; target.z -= position.z; target = rotate * target; target.x += position.x; target.y += position.y; target.z += position.z; up = rotate * up; } if (Application::IsKeyPressed(VK_UP)) { float pitch = (float)(CAMERA_SPEED * dt); Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotate; rotate.SetToRotation(pitch, right.x, right.y, right.z); target.x -= position.x; target.y -= position.y; target.z -= position.z; target = rotate * target; target.x += position.x; target.y += position.y; target.z += position.z; } if (Application::IsKeyPressed(VK_DOWN)) { float pitch = (float)(-CAMERA_SPEED * dt); Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotate; rotate.SetToRotation(pitch, right.x, right.y, right.z); target.x -= position.x; target.y -= position.y; target.z -= position.z; target = rotate * target; target.x += position.x; target.y += position.y; target.z += position.z; } if (Application::IsKeyPressed('R')) { Reset(); } }
void AI::AiLookatRotation(double &dt) { if(currentLookat != 0) { static float rotationSpeed = 4; if(e_State == ATTACK) { rotationSpeed = 10; } else { rotationSpeed = 5; } if (b_aiRotating == false) { float rotationdiff = (CalAnglefromPosition(currentLookat, pos, true) - CalAnglefromPosition(Lookat, pos, true)); if (rotationdiff >= 180) { Mtx44 rotation; Lookat = Lookat - pos; rotation.SetToRotation(-360, 0, 1, 0); Lookat = rotation * Lookat; Lookat = Lookat + pos; rotationdiff -= 360; } else if (rotationdiff <= -180) { Mtx44 rotation; Lookat = Lookat - pos; rotation.SetToRotation(360, 0, 1, 0); Lookat = rotation * Lookat; Lookat = Lookat + pos; rotationdiff += 360; } Mtx44 rotation; Lookat = Lookat - pos; rotation.SetToRotation(rotationdiff * rotationSpeed * static_cast<float>(dt), 0, 1, 0); Lookat = rotation * Lookat; Lookat = Lookat + pos; if(rotationdiff < 0.1 && rotationdiff > -0.1) { b_aiRotating = true; } } else { if (e_State == ATTACK) { prevPosition = pos; } else if (e_State == ALERT) { destination = currentLookat; prevPosition = pos; } currentLookat = 0; b_aiRotating = false; } } }
void AI::aiStateHandling(double &dt, Vector3 &playerPos, std::vector<GameObject*> &m_GOList) { Mtx44 rotation; rotation.SetToRotation(CalAnglefromPosition(Lookat, pos, true), 0.f, 1.f, 0.f); Vector3 L, R, C; C = rotation * Vector3(0.f, ModelPos.y, 50.f); L = rotation * Vector3(-15.f, ModelPos.y, 15.f); R = rotation * Vector3(15.f, ModelPos.y, 15.f); switch (e_State) { case WALKING: { //Have the AI partol a certain area //Need Pathfinding i think destination = playerPos; if (b_isDestinationWithinFOV && b_isDestinationVisible) { //If player is infront and near player, then ai will switch to attack state if ((playerPos - pos).LengthSquared() < d_detectionRange) { currentLookat.x = playerPos.x; currentLookat.z = playerPos.z; destination = playerPos; prevPosition = pos; e_State = ATTACK; break; } //if ai saw player but is too far way, the ai will investigate else if ((playerPos - pos).LengthSquared() >= d_detectionRange && (playerPos - pos).LengthSquared() <= d_detectionRangeMax) { destination.x = playerPos.x; destination.z = playerPos.z; destination = playerPos; prevPosition = pos; e_State = ALERT; break; } } SensorUpdate(dt, collisionChecking(pos + L, m_GOList), collisionChecking(pos + C, m_GOList), collisionChecking(pos + R, m_GOList)); } break; case ALERT: { //If destination is not visible if (!b_isDestinationVisible) { //use sensor update 2 to get to the destination SensorUpdate2(dt, collisionChecking(pos + L, m_GOList), collisionChecking(pos + C, m_GOList), collisionChecking(pos + R, m_GOList)); } //If player is infront and near player, then ai will switch to attack state if (isVisible(pos, Lookat, getDetectionAngle(), playerPos) && (playerPos - pos).LengthSquared() < d_detectionRange) { currentLookat.x = playerPos.x; currentLookat.z = playerPos.z; destination = playerPos; } if (b_isDestinationVisible && b_isDestinationWithinFOV && destination == playerPos) { e_State = ATTACK; } if(b_isDestinationVisible && b_isDestinationWithinFOV) { moveToDestination(dt); } if((pos - destination).LengthSquared() < 300) { e_State = WALKING; } } break; case ATTACK: { destination = playerPos; //if enemy is holding a weapon if (holding != NULL) { if (holding->isWeapon) { WeaponsObject *WO = dynamic_cast<WeaponsObject*>(holding); //Enemy is holding a gun if (WO->isGun) { //Make enemy stay at the same pos if (b_isDestinationVisible && b_isDestinationWithinFOV && (playerPos - pos).LengthSquared() < d_detectionRange) { currentLookat.x = playerPos.x; currentLookat.z = playerPos.z; b_SHOOTLA = true; } else { b_SHOOTLA = false; if (!b_isDestinationVisible) { SensorUpdate2(dt, collisionChecking(pos + L, m_GOList), collisionChecking(pos + C, m_GOList), collisionChecking(pos + R, m_GOList)); } else { moveToDestination(dt); } } } else { if (!b_isDestinationVisible) { SensorUpdate2(dt, collisionChecking(pos + L, m_GOList), collisionChecking(pos + C, m_GOList), collisionChecking(pos + R, m_GOList)); } else { moveToDestination(dt); } } } else { if (!b_isDestinationVisible) { SensorUpdate2(dt, collisionChecking(pos + L, m_GOList), collisionChecking(pos + C, m_GOList), collisionChecking(pos + R, m_GOList)); } else { moveToDestination(dt); } } } //Enemy is not holding a weapon else { if (!b_isDestinationVisible) { SensorUpdate2(dt, collisionChecking(pos + L, m_GOList), collisionChecking(pos + C, m_GOList), collisionChecking(pos + R, m_GOList)); } else { moveToDestination(dt); } } break; } default: break; } }
void FPCamera::Update(double dt){ tempPos = position; static const float MOVE_SPEED = 100.0f; static const float CAMERA_SPEED = MOVE_SPEED * 2; if (Application::IsKeyPressed('A')) { Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); view = target - position; view.Normalize(); position -= right * dt * MOVE_SPEED; target -= right * dt * MOVE_SPEED; } if (Application::IsKeyPressed('D')) { Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); view = target - position; view.Normalize(); position += right * dt * MOVE_SPEED; target += right * dt * MOVE_SPEED; } if (Application::IsKeyPressed('W')) { Vector3 view = (target - position).Normalized(); /*position.x += view.x * dt * MOVE_SPEED; position.z += view.z * dt * MOVE_SPEED; target.x += view.x * dt * MOVE_SPEED; target.z += view.z * dt * MOVE_SPEED;*/ //for free y move position += view * dt * MOVE_SPEED; target += view * dt * MOVE_SPEED; } if (Application::IsKeyPressed('S')) { Vector3 view = (target - position).Normalized(); /*position.x -= view.x * dt * MOVE_SPEED; position.z -= view.z * dt * MOVE_SPEED; target.x -= view.x * dt * MOVE_SPEED; target.z -= view.z * dt * MOVE_SPEED;*/ //for free y move position -= view * dt * MOVE_SPEED; target -= view * dt * MOVE_SPEED; } if (Application::IsKeyPressed(VK_UP)) { float pitch = (float)(-CAMERA_SPEED * dt); Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); Mtx44 rotation; rotation.SetToRotation(-pitch, right.x, right.y, right.z); up = rotation * up; view = rotation * view; target = position + view; } if (Application::IsKeyPressed(VK_DOWN)) { float pitch = (float)(-CAMERA_SPEED * dt); Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); up = rotation * up; view = rotation * view; target = position + view; } if (Application::IsKeyPressed(VK_LEFT)) { float yaw = (float)(-CAMERA_SPEED * dt); Mtx44 rotation; rotation.SetToRotation(-yaw, 0, 1, 0); Vector3 view = position - target; //view.Normalize(); view = rotation * view; position = target + view; up = rotation * up; } if (Application::IsKeyPressed(VK_RIGHT)) { float yaw = (float)(-CAMERA_SPEED * dt); Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); Vector3 view = position - target; //view.Normalize(); view = rotation * view; position = target + view; up = rotation * up; } if (Application::IsKeyPressed('N')) { Vector3 direction = target - position; if (direction.Length() > 5) { Vector3 view = (target - position).Normalized(); position += view * (float)(100.f * dt); } } if (Application::IsKeyPressed('M')) { Vector3 view = (target - position).Normalized(); position -= view * (float)(100.f * dt); } if (Application::IsKeyPressed('R')) { Reset(); } if (position.x > 499 || position.z > 499 || position.x < -499 || position.z < -499 || position.y > 499 || position.y < -499){ Reset(); } }
void Camera2::Update(double dt) { static const float CAMERA_SPEED = 50.f; TestPosition = position; PlayerPosition = position; if (Application::IsKeyPressed('W')) { Vector3 view = (target - position).Normalized(); // Normalize view vector TestPosition.x += view.x * dt * 100; TestPosition.z += view.z * dt * 100; if (BoundaryCheck(TestPosition) == true) { position.x += view.x * dt * 100; position.z += view.z * dt * 100; target.x += view.x * dt * 100; target.z += view.z * dt * 100; } } if (Application::IsKeyPressed('A')) { Vector3 view = (target - position).Normalized(); // Normalize view vector Vector3 right = view.Cross(up); TestPosition.x -= right.x * dt * 100; TestPosition.z -= right.z * dt * 100; if ((BoundaryCheck(TestPosition) == true)) { position.x -= right.x * dt * 100; position.z -= right.z * dt * 100; target.x -= right.x * dt * 100; target.z -= right.z * dt * 100; } } if (Application::IsKeyPressed('S')) { Vector3 view = (target - position).Normalized(); // Normalize view vector TestPosition.x -= view.x * dt * 100; TestPosition.z -= view.z * dt * 100; if ((BoundaryCheck(TestPosition) == true)) { position.x -= view.x * dt * 100; position.z -= view.z * dt * 100; target.x -= view.x * dt * 100; target.z -= view.z * dt * 100; } } if (Application::IsKeyPressed('D')) { Vector3 view = (target - position).Normalized(); // Normalize view vector Vector3 right = view.Cross(up); TestPosition.x += right.x * dt * 100; TestPosition.z += right.z * dt * 100; if ((BoundaryCheck(TestPosition) == true)) { position.x += right.x * dt * 100; position.z += right.z * dt * 100; target.x += right.x * dt * 100; target.z += right.z * dt * 100; } } Application::MouseMovement(mouseXPos, mouseYPos); float horizontalMouseMovement = 10 * dt * static_cast<float>((800 / 2) - mouseXPos); float verticalMouseMovement = 10 * dt * static_cast<float>((600 / 2) - mouseYPos); if (verticalMouseMovement) { Vector3 TESTview = ((target - position).Normalized()) * 10; Vector3 view = ((target - position).Normalized()) * 10; // normalize view vector Vector3 right = view.Cross(up); Mtx44 rotation; rotation.SetToRotation(verticalMouseMovement, right.x, right.y, right.z); TESTview = rotation * view; if (TESTview.y < 8.f && TESTview.y > -9.f) { view = rotation * view; up = rotation * up; target = position + view; } } if (horizontalMouseMovement) { Vector3 view = ((target - position).Normalized()) * 10; // normalize view vector float yaw = (float)(CAMERA_SPEED * dt); Mtx44 rotation; rotation.SetToRotation(horizontalMouseMovement, 0, 1, 0); view = rotation * view; target = position + view; up = rotation * up; } if (Application::IsKeyPressed('R')) { Reset(); } }
void Camera2::Update(double dt) { //Mouse shit POINT currentposition_; GetCursorPos(¤tposition_); static const float CAMERA_SPEED = 40.f; //Move front if (Application::IsKeyPressed('W')) { Vector3 view = (target - position).Normalized(); view.y = 0; position += (view * dt * 10.f); target += (view * dt * 10.f); } //Move Back if (Application::IsKeyPressed('S')) { Vector3 view = (target - position).Normalized(); view.y = 0; position -= (view * dt * 10.f); target -= (view * dt * 10.f); } //Move Right if (Application::IsKeyPressed('D')) { Vector3 view = (target - position).Normalized(); Vector3 right=view.Cross(up); right.y = 0; position += (right * dt * 10.f); target += (right * dt * 10.f); } //Move Left if (Application::IsKeyPressed('A')) { Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; position -= (right * dt * 10.f); target -= (right * dt * 10.f); } //Rotate up if (Application::IsKeyPressed(VK_UP)) //Pitch { //Original float pitch = (float)(CAMERA_SPEED * dt); /*Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); position = rotation * position;*/ // FOr FP view Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); view = rotation * view; target = position + view; } //Rotate Down if (Application::IsKeyPressed(VK_DOWN)) //Pitch, down { float pitch = (float)(-CAMERA_SPEED * dt); //Original /*Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); position = rotation * position;*/ //Fp Camera Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); view = rotation * view; target = position + view; } //Rotate Right if (Application::IsKeyPressed(VK_RIGHT)) { float yaw = (float)(-CAMERA_SPEED * dt); //Original /* Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); position = rotation * position; up = rotation * up;*/ //FP camera Vector3 view = (target - position).Normalized(); Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); view = rotation * view; target = position + view; up = rotation*up; } //Rotate Left if (Application::IsKeyPressed(VK_LEFT)) { float yaw = (float)(CAMERA_SPEED * dt); //Original /*Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); position = rotation * position; up = rotation * up;*/ //FP camera Vector3 view = (target - position).Normalized(); Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); view = rotation * view; target = position + view; up = rotation*up; } if (currentposition_.x > (1600/2)) { float yaw = (float)(-CAMERA_SPEED * dt); Vector3 view = (target - position).Normalized(); Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); view = rotation * view; target = position + view; up = rotation*up; } if (currentposition_.x < (1600 / 2)) { float yaw = (float)(CAMERA_SPEED * dt); Vector3 view = (target - position).Normalized(); Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); view = rotation * view; target = position + view; up = rotation*up; } if (currentposition_.y > (900 / 2)) { float pitch = (float)(-CAMERA_SPEED * dt); Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); view = rotation * view; target = position + view; } if (currentposition_.y < (900 / 2)) { float pitch = (float)(CAMERA_SPEED * dt); Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); view = rotation * view; target = position + view; } if (Application::IsKeyPressed('R')) { Reset(); } SetCursorPos(1600 / 2, 900 / 2); }
void Camera2::Update(double dt) { static const float CAMERA_SPEED = 50.f; if(Application::IsKeyPressed(VK_LEFT) || Application::IsKeyPressed('A')) { float yaw = (float)(-CAMERA_SPEED * dt); Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); position = rotation * position; up = rotation * up; } if(Application::IsKeyPressed(VK_RIGHT) || Application::IsKeyPressed('D')) { float yaw = (float)(CAMERA_SPEED * dt); Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); position = rotation * position; up = rotation * up; } if(Application::IsKeyPressed(VK_UP) || Application::IsKeyPressed('W')) { float pitch = (float)(-CAMERA_SPEED * dt); Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); position = rotation * position; } if(Application::IsKeyPressed(VK_DOWN) || Application::IsKeyPressed('S')) { float pitch = (float)(CAMERA_SPEED * dt); Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); position = rotation * position; } if(Application::IsKeyPressed('N')) { Vector3 direction = target - position; if(direction.Length() > 5) { Vector3 view = (target - position).Normalized(); position += view * (float)(100.f * dt); } } if(Application::IsKeyPressed('M')) { Vector3 view = (target - position).Normalized(); position -= view * (float)(100.f * dt); } if(Application::IsKeyPressed('R')) { Reset(); } }
void MS::Rotate(float degrees, float axisX, float axisY, float axisZ) { Mtx44 mat; mat.SetToRotation(degrees, axisX, axisY, axisZ); ms.top() = ms.top() * mat; }
void Helicopter::Update(double dt, bool controlling) { static const float THRUST_SPEED = 10.f; static const float THRUST_LIMIT = 5.f; static const float TURN_SPEED = 100.f; bool piloting = false; if (controlling) { if (Application::IsKeyPressed('W')) { if (thrustSpeed < THRUST_LIMIT) thrustSpeed += (float)(THRUST_SPEED * dt); piloting = true; } if (Application::IsKeyPressed('S')) { if (thrustSpeed > -THRUST_LIMIT) thrustSpeed -= (float)(THRUST_SPEED * dt); piloting = true; } if (Application::IsKeyPressed('A')) { float yaw = (float)(TURN_SPEED * dt); rotateYaw += yaw; camera.Rotate(yaw, 0, 1, 0); Mtx44 rotate; rotate.SetToRotation(yaw, 0, 1, 0); thrustDir = rotate * thrustDir; frontDir = rotate * frontDir; right = rotate * right; } if (Application::IsKeyPressed('D')) { float yaw = (float)(-TURN_SPEED * dt); rotateYaw += yaw; camera.Rotate(yaw, 0, 1, 0); Mtx44 rotate; rotate.SetToRotation(yaw, 0, 1, 0); thrustDir = rotate * thrustDir; frontDir = rotate * frontDir; right = rotate * right; } if (Application::IsKeyPressed(VK_NUMPAD8)) { float pitch = (float)(-TURN_SPEED * dt); rotatePitch += pitch; //camera.Rotate(pitch, right.x, right.y, right.z); Mtx44 rotate; rotate.SetToRotation(pitch, right.x, right.y, right.z); thrustDir = rotate * thrustDir; frontDir = rotate * frontDir; std::cout << frontDir << std::endl; } if (Application::IsKeyPressed(VK_NUMPAD5)) { float pitch = (float)(TURN_SPEED * dt); rotatePitch += pitch; //camera.Rotate(pitch, right.x, right.y, right.z); Mtx44 rotate; rotate.SetToRotation(pitch, right.x, right.y, right.z); thrustDir = rotate * thrustDir; frontDir = rotate * frontDir; } if (Application::IsKeyPressed(VK_NUMPAD4)) { float roll = (float)(-TURN_SPEED * dt); rotateRoll += roll; Mtx44 rotate; rotate.SetToRotation(roll, frontDir.x, frontDir.y, frontDir.z); thrustDir = rotate * thrustDir; right = rotate * right; } if (Application::IsKeyPressed(VK_NUMPAD6)) { float roll = (float)(TURN_SPEED * dt); rotateRoll += roll; Mtx44 rotate; rotate.SetToRotation(roll, frontDir.x, frontDir.y, frontDir.z); thrustDir = rotate * thrustDir; right = rotate * right; } } if (!piloting) { if (thrustSpeed > 0.f) { thrustSpeed -= (float)(THRUST_SPEED * dt); } else if (thrustSpeed < 0.f) { thrustSpeed += (float)(THRUST_SPEED * dt); } } frontDir.Normalize(); right.Normalize(); thrustDir.Normalize(); Vector3 oldPos = position; vSpeed += ((thrustDir.y * thrustSpeed) - WV_GRAVITY) * dt; xSpeed += ((thrustDir.x * thrustSpeed)) * dt; zSpeed += ((thrustDir.z * thrustSpeed)) * dt; position.x += xSpeed * dt; position.z += zSpeed * dt; position.y += vSpeed * dt; hitbox.SetPosition(position); if (Hitbox::CheckHitBox(hitbox, position, oldPos, &hitbox)) { vSpeed = 0.f; xSpeed = 0.f; zSpeed = 0.f; } camera.target = position; camera.Update(dt); }
void Camera3::Update(double dt) { static const float CAMERA_LOOK_SPEED = 200.f; static const float CAMERA_MOVE_SPEED = 100.f; if(Application::IsKeyPressed('A')) { Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); position -= right * CAMERA_MOVE_SPEED * (float)dt; target -= right * CAMERA_MOVE_SPEED * (float)dt; } if(Application::IsKeyPressed('D')) { Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); position += right * CAMERA_MOVE_SPEED * (float)dt; target += right * CAMERA_MOVE_SPEED * (float)dt; } if(Application::IsKeyPressed('W')) { Vector3 view = (target - position).Normalized(); position += view * CAMERA_MOVE_SPEED * (float)dt; target += view * CAMERA_MOVE_SPEED * (float)dt; } if(Application::IsKeyPressed('S')) { Vector3 view = (target - position).Normalized(); position -= view * CAMERA_MOVE_SPEED * (float)dt; target -= view * CAMERA_MOVE_SPEED * (float)dt; } if(Application::IsKeyPressed(VK_LEFT)) { Vector3 view = (target - position).Normalized(); float yaw = (float)(CAMERA_MOVE_SPEED * (float)dt); Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); view = rotation * view; target = position + view; Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); } if(Application::IsKeyPressed(VK_RIGHT)) { Vector3 view = (target - position).Normalized(); float yaw = (float)(-CAMERA_MOVE_SPEED * (float)dt); Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); view = rotation * view; target = position + view; Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); } if(Application::IsKeyPressed(VK_UP)) { float pitch = (float)(CAMERA_MOVE_SPEED * (float)dt); Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); view = rotation * view; target = position + view; } if(Application::IsKeyPressed(VK_DOWN)) { float pitch = (float)(-CAMERA_MOVE_SPEED * (float)dt); Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); view = rotation * view; target = position + view; } //Update the camera direction based on mouse move // left-right rotate { Vector3 view = (target - position).Normalized(); float yaw = (float)(-CAMERA_LOOK_SPEED * Application::camera_yaw * (float)dt); Mtx44 rotation; rotation.SetToRotation(yaw, 0, 1, 0); view = rotation * view; target = position + view; Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); } { float pitch = (float)(-CAMERA_LOOK_SPEED * Application::camera_pitch * (float)dt); Vector3 view = (target - position).Normalized(); Vector3 right = view.Cross(up); right.y = 0; right.Normalize(); up = right.Cross(view).Normalized(); Mtx44 rotation; rotation.SetToRotation(pitch, right.x, right.y, right.z); view = rotation * view; target = position + view; } if(Application::IsKeyPressed('R')) { Reset(); } }
void SecurityCam::update(const double &dt, Vector3 &playerPos, std::vector<GameObject*> m_goList) { Vector3 SCPos = pos; Vector3 SCLookat = Lookat; SCPos.y = SCLookat.y = 0; switch(c_State) { case SPOTTED: { float rotationdiff = (CalAnglefromPosition(playerPos, pos, true) - CalAnglefromPosition(Lookat, pos, true)) * 2.f; Mtx44 rotation; Lookat = Lookat - pos; f_rotationLimiter += rotationdiff * static_cast<float>(dt); rotation.SetToRotation(rotationdiff * static_cast<float>(dt), 0, 1, 0); Lookat = rotation * Lookat; Lookat = Lookat + pos; //Check whether player is still in the fov of security camera if (((isVisible(SCPos, SCLookat, static_cast<float>(f_cameraFOV), playerPos)) && (SCPos - playerPos).LengthSquared() < f_cameraRange)) { //Give player 1 sec to prevent detection by the security camera if (alerttimer < 1) { alerttimer += static_cast<float>(dt); } else { c_State = FOUND; alerttimer = 0; } } else { //if player is no longer in the fov of the security camera c_State = NOTFOUND; alerttimer = 0; } } break; case FOUND: { float rotationdiff = (CalAnglefromPosition(playerPos, pos, true) - CalAnglefromPosition(Lookat, pos, true)); if (rotationdiff >= 180) { Mtx44 rotation; Lookat = Lookat - pos; rotation.SetToRotation(-360, 0, 1, 0); Lookat = rotation * Lookat; Lookat = Lookat + pos; rotationdiff -= 360; } else if (rotationdiff <= -180) { Mtx44 rotation; Lookat = Lookat - pos; rotation.SetToRotation(360, 0, 1, 0); Lookat = rotation * Lookat; Lookat = Lookat + pos; rotationdiff += 360; } Mtx44 rotation; Lookat = Lookat - pos; f_rotationLimiter += rotationdiff * static_cast<float>(dt); rotation.SetToRotation(rotationdiff * 2 * static_cast<float>(dt), 0, 1, 0); Lookat = rotation * Lookat; Lookat = Lookat + pos; //Alert all the ai to the camera's position if (b_alertAI) { for (std::vector<GameObject*>::iterator it = m_goList.begin(); it != m_goList.end(); it++) { GameObject *go = (GameObject *)*it; AI *ai = dynamic_cast<AI*>(go); if (ai != NULL) { if (ai->getState() == AI::WALKING) { ai->setState(AI::ALERT); ai->setcurrentLookat(pos); ai->setDestination(pos); } } } b_alertAI = false; } else { if (!isVisible(pos, Lookat, f_cameraFOV, playerPos) || (playerPos - pos).LengthSquared() > f_cameraRange) { c_State = NOTFOUND; } } } break; case NOTFOUND: { //Check whether enemy is within security camera's FOV b_alertAI = true; if (((isVisible(SCPos, SCLookat, static_cast<float>(f_cameraFOV), playerPos)) && (SCPos - playerPos).LengthSquared() < f_cameraRange)) { c_State = SPOTTED; } } break; } if (c_State == NOTFOUND) { float f_currentRotation = 0; if (rotationState) { f_currentRotation = 20; if (f_rotationLimiter > f_rotationAngle) { rotationState = false; } } else { f_currentRotation = -20; if (f_rotationLimiter < -f_rotationAngle) { rotationState = true; } } if (f_currentRotation != 0) { Mtx44 rotation; Lookat = Lookat - pos; f_rotationLimiter += f_currentRotation * static_cast<float>(dt); rotation.SetToRotation(f_currentRotation * static_cast<float>(dt), 0, 1, 0); Lookat = rotation * Lookat; Lookat = Lookat + pos; } } //CollisionChecking(m_goList); }