Fixed jerking on gravity, added mouse pitch, yaw behavior.

This commit is contained in:
Philip Rosedale 2013-05-16 17:05:45 -06:00
parent 7866c803fb
commit 9f57787ece
5 changed files with 46 additions and 30 deletions

View file

@ -306,13 +306,13 @@ void Application::paintGL() {
if (_myCamera.getMode() == CAMERA_MODE_FIRST_PERSON) { if (_myCamera.getMode() == CAMERA_MODE_FIRST_PERSON) {
_myCamera.setTargetPosition(_myAvatar.getSpringyHeadPosition()); _myCamera.setTargetPosition(_myAvatar.getSpringyHeadPosition());
_myCamera.setTargetRotation(_myAvatar.getAbsoluteHeadYaw(), _myCamera.setTargetRotation(_myAvatar.getAbsoluteHeadYaw(),
_myAvatar.getAbsoluteHeadPitch(), -_myAvatar.getAbsoluteHeadPitch(),
0.0f); 0.0f);
} else if (_myCamera.getMode() == CAMERA_MODE_THIRD_PERSON) { } else if (_myCamera.getMode() == CAMERA_MODE_THIRD_PERSON) {
_myCamera.setTargetPosition(_myAvatar.getHeadPosition()); _myCamera.setTargetPosition(_myAvatar.getHeadPosition());
_myCamera.setTargetRotation(_myAvatar.getBodyYaw(), _myCamera.setTargetRotation(_myAvatar.getBodyYaw(),
0.0f, -_myAvatar.getAbsoluteHeadPitch(),
0.0f); 0.0f);
} }
} }
@ -864,7 +864,11 @@ void Application::idle() {
} }
// Update from Mouse // Update from Mouse
_myAvatar.updateFromMouse(_mouseX, _mouseY, _glWidget->width(), _glWidget->height()); QPoint mouse = QCursor::pos();
_myAvatar.updateFromMouse(_glWidget->mapFromGlobal(mouse).x(),
_glWidget->mapFromGlobal(mouse).y(),
_glWidget->width(),
_glWidget->height());
// Read serial port interface devices // Read serial port interface devices
if (_serialPort.active) { if (_serialPort.active) {

View file

@ -214,7 +214,6 @@ void Avatar::reset() {
_head.leanForward = _head.leanSideways = 0; _head.leanForward = _head.leanSideways = 0;
} }
// Update avatar head rotation with sensor data // Update avatar head rotation with sensor data
void Avatar::updateHeadFromGyros(float deltaTime, SerialInterface* serialInterface, glm::vec3* gravity) { void Avatar::updateHeadFromGyros(float deltaTime, SerialInterface* serialInterface, glm::vec3* gravity) {
float measuredPitchRate = 0.0f; float measuredPitchRate = 0.0f;
@ -226,8 +225,6 @@ void Avatar::updateHeadFromGyros(float deltaTime, SerialInterface* serialInterfa
measuredRollRate = serialInterface->getLastRollRate(); measuredRollRate = serialInterface->getLastRollRate();
// Update avatar head position based on measured gyro rates // Update avatar head position based on measured gyro rates
const float MAX_PITCH = 45;
const float MIN_PITCH = -45;
const float MAX_YAW = 85; const float MAX_YAW = 85;
const float MIN_YAW = -85; const float MIN_YAW = -85;
const float MAX_ROLL = 50; const float MAX_ROLL = 50;
@ -237,7 +234,6 @@ void Avatar::updateHeadFromGyros(float deltaTime, SerialInterface* serialInterfa
addHeadYaw(measuredYawRate * deltaTime); addHeadYaw(measuredYawRate * deltaTime);
addHeadRoll(measuredRollRate * deltaTime); addHeadRoll(measuredRollRate * deltaTime);
setHeadPitch(glm::clamp(getHeadPitch(), MIN_PITCH, MAX_PITCH));
setHeadYaw(glm::clamp(getHeadYaw(), MIN_YAW, MAX_YAW)); setHeadYaw(glm::clamp(getHeadYaw(), MIN_YAW, MAX_YAW));
setHeadRoll(glm::clamp(getHeadRoll(), MIN_ROLL, MAX_ROLL)); setHeadRoll(glm::clamp(getHeadRoll(), MIN_ROLL, MAX_ROLL));
@ -296,31 +292,41 @@ bool Avatar::getIsNearInteractingOther() {
void Avatar::updateFromMouse(int mouseX, int mouseY, int screenWidth, int screenHeight) { void Avatar::updateFromMouse(int mouseX, int mouseY, int screenWidth, int screenHeight) {
// Update yaw based on mouse behavior // Update yaw based on mouse behavior
const float MOUSE_MOVE_RADIUS = 0.25f; const float MOUSE_MOVE_RADIUS = 0.25f;
const float MOUSE_ROTATE_SPEED = 7.5f; const float MOUSE_ROTATE_SPEED = 5.0f;
const float MOUSE_PITCH_SPEED = 3.0f;
const float MAX_YAW_TO_ADD = 180.f; const float MAX_YAW_TO_ADD = 180.f;
const int TITLE_BAR_HEIGHT = 46;
float mouseLocationX = (float)mouseX / (float)screenWidth - 0.5f; float mouseLocationX = (float)mouseX / (float)screenWidth - 0.5f;
float mouseLocationY = (float)mouseY / (float)screenHeight - 0.5f;
printLog("mouse %d, %d\n", mouseX, mouseY); if ((mouseX > 1) && (mouseX < screenWidth) && (mouseY > TITLE_BAR_HEIGHT) && (mouseY < screenHeight)) {
//
if ((fabs(mouseLocationX) > MOUSE_MOVE_RADIUS) && // Mouse must be inside screen (not at edge) and not on title bar for movement to happen
(mouseX > 1) && //
(mouseX < screenWidth) && if (fabs(mouseLocationX) > MOUSE_MOVE_RADIUS) {
(mouseY > 1) && // Add Yaw
(mouseY < screenHeight)) { float mouseYawAdd = (fabs(mouseLocationX) - MOUSE_MOVE_RADIUS) / (0.5f - MOUSE_MOVE_RADIUS) * MOUSE_ROTATE_SPEED;
float mouseYawAdd = (fabs(mouseLocationX) - MOUSE_MOVE_RADIUS) / (0.5f - MOUSE_MOVE_RADIUS) * MOUSE_ROTATE_SPEED; bool rightTurning = (mouseLocationX > 0.f);
bool rightTurning = (mouseLocationX > 0.f); if (_isMouseTurningRight == rightTurning) {
if (_isMouseTurningRight == rightTurning) { _cumulativeMouseYaw += mouseYawAdd;
_cumulativeMouseYaw += mouseYawAdd; } else {
_cumulativeMouseYaw = 0;
_isMouseTurningRight = rightTurning;
}
if (_cumulativeMouseYaw < MAX_YAW_TO_ADD) {
setBodyYaw(getBodyYaw() - (rightTurning ? mouseYawAdd : -mouseYawAdd));
}
} else { } else {
_cumulativeMouseYaw = 0; _cumulativeMouseYaw = 0;
_isMouseTurningRight = rightTurning;
} }
if (_cumulativeMouseYaw < MAX_YAW_TO_ADD) { if (fabs(mouseLocationY) > MOUSE_MOVE_RADIUS) {
setBodyYaw(getBodyYaw() - (rightTurning ? mouseYawAdd : -mouseYawAdd)); float mousePitchAdd = (fabs(mouseLocationY) - MOUSE_MOVE_RADIUS) / (0.5f - MOUSE_MOVE_RADIUS) * MOUSE_PITCH_SPEED;
bool downPitching = (mouseLocationY > 0.f);
setHeadPitch(getHeadPitch() + (downPitching ? mousePitchAdd : -mousePitchAdd));
} }
} else {
_cumulativeMouseYaw = 0;
} }
return; return;
} }
@ -348,7 +354,7 @@ void Avatar::simulate(float deltaTime) {
_avatarTouch.simulate(deltaTime); _avatarTouch.simulate(deltaTime);
// apply gravity and collision with the ground/floor // apply gravity and collision with the ground/floor
if (USING_AVATAR_GRAVITY) { if (_isMine && USING_AVATAR_GRAVITY) {
if (_position.y > _pelvisStandingHeight + 0.01f) { if (_position.y > _pelvisStandingHeight + 0.01f) {
_velocity += _gravity * (GRAVITY_SCALE * deltaTime); _velocity += _gravity * (GRAVITY_SCALE * deltaTime);
} else if (_position.y < _pelvisStandingHeight) { } else if (_position.y < _pelvisStandingHeight) {

View file

@ -221,9 +221,6 @@ void Head::simulate(float deltaTime, bool isMine) {
(deltaTime / AUDIO_AVERAGING_SECS) * audioLoudness; (deltaTime / AUDIO_AVERAGING_SECS) * audioLoudness;
} }
void Head::render(bool lookingInMirror, float bodyYaw) { void Head::render(bool lookingInMirror, float bodyYaw) {
int side = 0; int side = 0;

View file

@ -236,4 +236,13 @@ void AvatarData::setBodyRoll(float bodyRoll) {
_bodyRoll = bodyRoll; _bodyRoll = bodyRoll;
} }
void AvatarData::setHeadPitch(float p) {
// Set head pitch and apply limits
const float MAX_PITCH = 60;
const float MIN_PITCH = -60;
_headPitch = glm::clamp(p, MIN_PITCH, MAX_PITCH);
}

View file

@ -73,13 +73,13 @@ public:
void setBodyRoll(float bodyRoll); void setBodyRoll(float bodyRoll);
// Head Rotation // Head Rotation
void setHeadPitch(float p) {_headPitch = p; } void setHeadPitch(float p);
void setHeadYaw(float y) {_headYaw = y; } void setHeadYaw(float y) {_headYaw = y; }
void setHeadRoll(float r) {_headRoll = r; }; void setHeadRoll(float r) {_headRoll = r; };
float getHeadPitch() const { return _headPitch; }; float getHeadPitch() const { return _headPitch; };
float getHeadYaw() const { return _headYaw; }; float getHeadYaw() const { return _headYaw; };
float getHeadRoll() const { return _headRoll; }; float getHeadRoll() const { return _headRoll; };
void addHeadPitch(float p) {_headPitch -= p; } void addHeadPitch(float p) { setHeadPitch(_headPitch - p); }
void addHeadYaw(float y){_headYaw -= y; } void addHeadYaw(float y){_headYaw -= y; }
void addHeadRoll(float r){_headRoll += r; } void addHeadRoll(float r){_headRoll += r; }