Example #1
0
void Player::PollControls(const float timeStep)
{
	static bool stickySpeedKey = false;

	if (Pi::game->GetTimeAccel() == Game::TIMEACCEL_PAUSED || Pi::player->IsDead() || GetFlightState() != FLYING)
		return;

	// if flying 
	{
		ClearThrusterState();
		SetGunState(0,0);
		SetGunState(1,0);

		vector3d wantAngVel(0.0);
		double angThrustSoftness = 50.0;

		// have to use this function. SDL mouse position event is bugged in windows
		int mouseMotion[2];
		SDL_GetRelativeMouseState (mouseMotion+0, mouseMotion+1);	// call to flush
		if (Pi::MouseButtonState(SDL_BUTTON_RIGHT))
		{
			matrix4x4d rot; GetRotMatrix(rot);
			if (!m_mouseActive) {
				m_mouseDir = vector3d(-rot[8],-rot[9],-rot[10]);	// in world space
				m_mouseX = m_mouseY = 0;
				m_mouseActive = true;
			}
			vector3d objDir = m_mouseDir * rot;

			const double radiansPerPixel = 0.002;

			m_mouseX += mouseMotion[0] * radiansPerPixel;
			double modx = clipmouse(objDir.x, m_mouseX);			
			m_mouseX -= modx;

			const bool invertY = (Pi::IsMouseYInvert() ? !m_invertMouse : m_invertMouse);

			m_mouseY += mouseMotion[1] * radiansPerPixel * (invertY ? -1 : 1);
			double mody = clipmouse(objDir.y, m_mouseY);
			m_mouseY -= mody;

			if(!float_is_zero_general(modx) || !float_is_zero_general(mody)) {
				matrix4x4d mrot = matrix4x4d::RotateYMatrix(modx); mrot.RotateX(mody);
				m_mouseDir = (rot * (mrot * objDir)).Normalized();
			}
		}
		else m_mouseActive = false;

		// disable all keyboard controls while the console is active
		if (!Pi::IsConsoleActive()) {
			if (m_flightControlState == CONTROL_FIXSPEED) {
				double oldSpeed = m_setSpeed;
				if (stickySpeedKey) {
					if (!(KeyBindings::increaseSpeed.IsActive() || KeyBindings::decreaseSpeed.IsActive())) {
						stickySpeedKey = false;
					}
				}
				
				if (!stickySpeedKey) {
					if (KeyBindings::increaseSpeed.IsActive())
						m_setSpeed += std::max(fabs(m_setSpeed)*0.05, 1.0);
					if (KeyBindings::decreaseSpeed.IsActive())
						m_setSpeed -= std::max(fabs(m_setSpeed)*0.05, 1.0);
					if ( ((oldSpeed < 0.0) && (m_setSpeed >= 0.0)) ||
						 ((oldSpeed > 0.0) && (m_setSpeed <= 0.0)) ) {
						// flipped from going forward to backwards. make the speed 'stick' at zero
						// until the player lets go of the key and presses it again
						stickySpeedKey = true;
						m_setSpeed = 0;
					}
				}
			}

			if (KeyBindings::thrustForward.IsActive()) SetThrusterState(2, -1.0);
			if (KeyBindings::thrustBackwards.IsActive()) SetThrusterState(2, 1.0);
			if (KeyBindings::thrustUp.IsActive()) SetThrusterState(1, 1.0);
			if (KeyBindings::thrustDown.IsActive()) SetThrusterState(1, -1.0);
			if (KeyBindings::thrustLeft.IsActive()) SetThrusterState(0, -1.0);
			if (KeyBindings::thrustRight.IsActive()) SetThrusterState(0, 1.0);

			if (KeyBindings::fireLaser.IsActive() || (Pi::MouseButtonState(SDL_BUTTON_LEFT) && Pi::MouseButtonState(SDL_BUTTON_RIGHT))) {
					SetGunState(Pi::worldView->GetActiveWeapon(), 1);
			}

			if (KeyBindings::yawLeft.IsActive()) wantAngVel.y += 1.0;
			if (KeyBindings::yawRight.IsActive()) wantAngVel.y += -1.0;
			if (KeyBindings::pitchDown.IsActive()) wantAngVel.x += -1.0;
			if (KeyBindings::pitchUp.IsActive()) wantAngVel.x += 1.0;
			if (KeyBindings::rollLeft.IsActive()) wantAngVel.z += 1.0;
			if (KeyBindings::rollRight.IsActive()) wantAngVel.z -= 1.0;

			if (KeyBindings::fastRotate.IsActive())
				angThrustSoftness = 10.0;
		}

		vector3d changeVec;
		changeVec.x = KeyBindings::pitchAxis.GetValue();
		changeVec.y = KeyBindings::yawAxis.GetValue();
		changeVec.z = KeyBindings::rollAxis.GetValue();

		// Deadzone
		if(changeVec.LengthSqr() < m_joystickDeadzone)
			changeVec = vector3d(0.0);

		changeVec *= 2.0;
		wantAngVel += changeVec;

		double invTimeAccelRate = 1.0 / Pi::game->GetTimeAccelRate();
		for (int axis=0; axis<3; axis++)
			wantAngVel[axis] = Clamp(wantAngVel[axis], -invTimeAccelRate, invTimeAccelRate);
		
		if (m_mouseActive) AIFaceDirection(m_mouseDir);
		else AIModelCoordsMatchAngVel(wantAngVel, angThrustSoftness);
	}
}
Example #2
0
void PlayerShipController::PollControls(const float timeStep, const bool force_rotation_damping, int *mouseMotion)
{
	static bool stickySpeedKey = false;
	CheckControlsLock();
	if (m_controlsLocked) return;

	// if flying
	{
		m_ship->ClearThrusterState();
		m_ship->SetGunState(0,0);
		m_ship->SetGunState(1,0);

		vector3d wantAngVel(0.0);
		double angThrustSoftness = 10.0;

		const float linearThrustPower = (KeyBindings::thrustLowPower.IsActive() ? m_lowThrustPower : 1.0f);

		// have to use this function. SDL mouse position event is bugged in windows
		if (Pi::MouseButtonState(SDL_BUTTON_RIGHT))
		{
			const matrix3x3d &rot = m_ship->GetOrient();
			if (!m_mouseActive) {
				m_mouseDir = -rot.VectorZ();	// in world space
				m_mouseX = m_mouseY = 0;
				m_mouseActive = true;
			}
			vector3d objDir = m_mouseDir * rot;

			const double radiansPerPixel = 0.00002 * m_fovY;
			const int maxMotion = std::max(abs(mouseMotion[0]), abs(mouseMotion[1]));
			const double accel = Clamp(maxMotion / 4.0, 0.0, 90.0 / m_fovY);

			m_mouseX += mouseMotion[0] * accel * radiansPerPixel;
			double modx = clipmouse(objDir.x, m_mouseX);
			m_mouseX -= modx;

			const bool invertY = (Pi::IsMouseYInvert() ? !m_invertMouse : m_invertMouse);

			m_mouseY += mouseMotion[1] * accel * radiansPerPixel * (invertY ? -1 : 1);
			double mody = clipmouse(objDir.y, m_mouseY);
			m_mouseY -= mody;

			if(!is_zero_general(modx) || !is_zero_general(mody)) {
				matrix3x3d mrot = matrix3x3d::RotateY(modx) * matrix3x3d::RotateX(mody);
				m_mouseDir = (rot * (mrot * objDir)).Normalized();
			}
		}
		else m_mouseActive = false;

		if (m_flightControlState == CONTROL_FIXSPEED) {
			double oldSpeed = m_setSpeed;
			if (stickySpeedKey) {
				if (!(KeyBindings::increaseSpeed.IsActive() || KeyBindings::decreaseSpeed.IsActive())) {
					stickySpeedKey = false;
				}
			}

			if (!stickySpeedKey) {
				if (KeyBindings::increaseSpeed.IsActive())
					m_setSpeed += std::max(fabs(m_setSpeed)*0.05, 1.0);
				if (KeyBindings::decreaseSpeed.IsActive())
					m_setSpeed -= std::max(fabs(m_setSpeed)*0.05, 1.0);
				if ( ((oldSpeed < 0.0) && (m_setSpeed >= 0.0)) ||
						((oldSpeed > 0.0) && (m_setSpeed <= 0.0)) ) {
					// flipped from going forward to backwards. make the speed 'stick' at zero
					// until the player lets go of the key and presses it again
					stickySpeedKey = true;
					m_setSpeed = 0;
				}
			}
		}

		if (KeyBindings::thrustForward.IsActive()) m_ship->SetThrusterState(2, -linearThrustPower);
		if (KeyBindings::thrustBackwards.IsActive()) m_ship->SetThrusterState(2, linearThrustPower);
		if (KeyBindings::thrustUp.IsActive()) m_ship->SetThrusterState(1, linearThrustPower);
		if (KeyBindings::thrustDown.IsActive()) m_ship->SetThrusterState(1, -linearThrustPower);
		if (KeyBindings::thrustLeft.IsActive()) m_ship->SetThrusterState(0, -linearThrustPower);
		if (KeyBindings::thrustRight.IsActive()) m_ship->SetThrusterState(0, linearThrustPower);

		if (KeyBindings::fireLaser.IsActive() || (Pi::MouseButtonState(SDL_BUTTON_LEFT) && Pi::MouseButtonState(SDL_BUTTON_RIGHT))) {
				//XXX worldview? madness, ask from ship instead
				m_ship->SetGunState(Pi::worldView->GetActiveWeapon(), 1);
		}

		if (KeyBindings::yawLeft.IsActive()) wantAngVel.y += 1.0;
		if (KeyBindings::yawRight.IsActive()) wantAngVel.y += -1.0;
		if (KeyBindings::pitchDown.IsActive()) wantAngVel.x += -1.0;
		if (KeyBindings::pitchUp.IsActive()) wantAngVel.x += 1.0;
		if (KeyBindings::rollLeft.IsActive()) wantAngVel.z += 1.0;
		if (KeyBindings::rollRight.IsActive()) wantAngVel.z -= 1.0;
		if (KeyBindings::killRot.IsActive()) SetFlightControlState(CONTROL_FIXHEADING_KILLROT);

		if (KeyBindings::thrustLowPower.IsActive())
			angThrustSoftness = 50.0;

		vector3d changeVec;
		changeVec.x = KeyBindings::pitchAxis.GetValue();
		changeVec.y = KeyBindings::yawAxis.GetValue();
		changeVec.z = KeyBindings::rollAxis.GetValue();

		// Deadzone more accurate
		for (int axis=0; axis<3; axis++) {
				if (fabs(changeVec[axis]) < m_joystickDeadzone)
					changeVec[axis]=0.0;
				else
					changeVec[axis] = changeVec[axis] * 2.0;
		}
		
		wantAngVel += changeVec;

		if (wantAngVel.Length() >= 0.001 || force_rotation_damping || m_rotationDamping) {
			if (Pi::game->GetTimeAccel()!=Game::TIMEACCEL_1X) {
				for (int axis=0; axis<3; axis++)
					wantAngVel[axis] = wantAngVel[axis] * Pi::game->GetInvTimeAccelRate();
			}

			m_ship->AIModelCoordsMatchAngVel(wantAngVel, angThrustSoftness);
		}

		if (m_mouseActive) m_ship->AIFaceDirection(m_mouseDir);
	}
}
Example #3
0
void Player::PollControls(const float timeStep)
{
	double time_accel = Pi::GetTimeAccel();
	double invTimeAccel = 1.0 / time_accel;
	static bool stickySpeedKey = false;

	if ((time_accel == 0) || GetDockedWith() || Pi::player->IsDead() ||
	    (GetFlightState() != FLYING)) {
		return;
	}

	// if flying 
	{
		ClearThrusterState();
		
		vector3d wantAngVel(0.0);

		// have to use this function. SDL mouse position event is bugged in windows
		int mouseMotion[2];
		SDL_GetRelativeMouseState (mouseMotion+0, mouseMotion+1);	// call to flush
		if (Pi::MouseButtonState(3))
		{
			matrix4x4d rot; GetRotMatrix(rot);
			if (!m_mouseActive) {
				m_mouseDir = vector3d(-rot[8],-rot[9],-rot[10]);	// in world space
				m_mouseX = m_mouseY = 0;
				m_mouseActive = true;
			}
			vector3d objDir = m_mouseDir * rot;

			m_mouseX += mouseMotion[0] * 0.002;
			double modx = clipmouse(objDir.x, m_mouseX);			
			m_mouseX -= modx;

			m_mouseY += mouseMotion[1] * 0.002;		// factor pixels => radians
			double mody = clipmouse(objDir.y, m_mouseY);
			m_mouseY -= mody;

			if(modx != 0.0 || mody != 0.0) {
				matrix4x4d mrot = matrix4x4d::RotateYMatrix(modx); mrot.RotateX(mody);
				m_mouseDir = (rot * (mrot * objDir)).Normalized();
			}
		}
		else m_mouseActive = false;
		
	
		if (m_flightControlState == CONTROL_FIXSPEED) {
			double oldSpeed = m_setSpeed;
			if (stickySpeedKey) {
				if (!(KeyBindings::increaseSpeed.IsActive() || KeyBindings::decreaseSpeed.IsActive())) {
					stickySpeedKey = false;
				}
			}
			
			if (!stickySpeedKey) {
				if (KeyBindings::increaseSpeed.IsActive()) m_setSpeed += std::max(m_setSpeed*0.05, 1.0);
				if (KeyBindings::decreaseSpeed.IsActive()) m_setSpeed -= std::max(m_setSpeed*0.05, 1.0);
				if ( ((oldSpeed < 0.0) && (m_setSpeed >= 0.0)) ||
				     ((oldSpeed > 0.0) && (m_setSpeed <= 0.0)) ) {
					// flipped from going forward to backwards. make the speed 'stick' at zero
					// until the player lets go of the key and presses it again
					stickySpeedKey = true;
					m_setSpeed = 0;
				}
			}
		}

		if (KeyBindings::thrustForward.IsActive()) SetThrusterState(2, -1.0);
		if (KeyBindings::thrustBackwards.IsActive()) SetThrusterState(2, 1.0);
		if (KeyBindings::thrustUp.IsActive()) SetThrusterState(1, 1.0);
		if (KeyBindings::thrustDown.IsActive()) SetThrusterState(1, -1.0);
		if (KeyBindings::thrustLeft.IsActive()) SetThrusterState(0, -1.0);
		if (KeyBindings::thrustRight.IsActive()) SetThrusterState(0, 1.0);
		
		SetGunState(0,0);
		SetGunState(1,0);
		if (KeyBindings::fireLaser.IsActive() || (Pi::MouseButtonState(1) && Pi::MouseButtonState(3))) {
				SetGunState(Pi::worldView->GetActiveWeapon(), 1);
		}

		if (KeyBindings::yawLeft.IsActive()) wantAngVel.y += 1.0;
		if (KeyBindings::yawRight.IsActive()) wantAngVel.y += -1.0;
		if (KeyBindings::pitchDown.IsActive()) wantAngVel.x += -1.0;
		if (KeyBindings::pitchUp.IsActive()) wantAngVel.x += 1.0;
		if (KeyBindings::rollLeft.IsActive()) wantAngVel.z += 1.0;
		if (KeyBindings::rollRight.IsActive()) wantAngVel.z -= 1.0;

		wantAngVel.x += 2 * KeyBindings::pitchAxis.GetValue();
		wantAngVel.y += 2 * KeyBindings::yawAxis.GetValue();
		wantAngVel.z += 2 * KeyBindings::rollAxis.GetValue();

		for (int axis=0; axis<3; axis++)
			wantAngVel[axis] = Clamp(wantAngVel[axis], -invTimeAccel, invTimeAccel);

//		matrix4x4d rot; GetRotMatrix(rot);
		const double angThrustSoftness = KeyBindings::fastRotate.IsActive() ? 10.0 : 50.0;
		
		if (m_mouseActive) AIFaceDirection(m_mouseDir);
		else AIModelCoordsMatchAngVel(wantAngVel, angThrustSoftness);
	}
}