add local gravity option parameter for MyAvatar

This commit is contained in:
Andrew Meadows 2014-04-28 16:40:22 -07:00
parent ea2e96ac6f
commit c7dbc5984f
8 changed files with 56 additions and 33 deletions

View file

@ -188,17 +188,18 @@ function update(deltaTime) {
ray = { origin: MyAvatar.position, direction: DOWN }; ray = { origin: MyAvatar.position, direction: DOWN };
var intersection = Voxels.findRayIntersection(ray); var intersection = Voxels.findRayIntersection(ray);
if (intersection.intersects) { if (intersection.intersects) {
if (!(MyAvatar.motionBehaviors & AVATAR_MOTION_OBEY_GRAVITY)) { if (!(MyAvatar.motionBehaviors & AVATAR_MOTION_OBEY_LOCAL_GRAVITY)) {
var v = intersection.voxel; var v = intersection.voxel;
var maxCorner = Vec3.sum({ x: v.x, y: v.y, z: v.z }, {x: v.s, y: v.s, z: v.s }); var maxCorner = Vec3.sum({ x: v.x, y: v.y, z: v.z }, {x: v.s, y: v.s, z: v.s });
var distance = lastPosition.y - maxCorner.y; var distance = lastPosition.y - maxCorner.y;
if ((gravityOnExpiry < now) && (distance < MAX_VOXEL_SCAN_DISTANCE)) { if ((gravityOnExpiry < now) && (distance < MAX_VOXEL_SCAN_DISTANCE)) {
MyAvatar.motionBehaviors = MyAvatar.motionBehaviors | AVATAR_MOTION_OBEY_GRAVITY; // NOTE: setting the gravity automatically sets the AVATAR_MOTION_OBEY_LOCAL_GRAVITY behavior bit.
MyAvatar.gravity = DOWN;
} }
} }
} else { } else {
if (MyAvatar.motionBehaviors & AVATAR_MOTION_OBEY_GRAVITY) { if (MyAvatar.motionBehaviors & AVATAR_MOTION_OBEY_LOCAL_GRAVITY) {
MyAvatar.motionBehaviors = MyAvatar.motionBehaviors & ~AVATAR_MOTION_OBEY_GRAVITY; MyAvatar.motionBehaviors = MyAvatar.motionBehaviors & ~AVATAR_MOTION_OBEY_LOCAL_GRAVITY;
} }
gravityOnExpiry = now + EXPIRY_PERIOD; gravityOnExpiry = now + EXPIRY_PERIOD;
} }
@ -216,9 +217,9 @@ function update(deltaTime) {
collisionOnExpiry = now + EXPIRY_PERIOD; collisionOnExpiry = now + EXPIRY_PERIOD;
} }
if (speed > MAX_WALKING_SPEED) { if (speed > MAX_WALKING_SPEED) {
if (MyAvatar.motionBehaviors & AVATAR_MOTION_OBEY_GRAVITY) { if (MyAvatar.motionBehaviors & AVATAR_MOTION_OBEY_LOCAL_GRAVITY) {
// turn off gravity // turn off gravity
MyAvatar.motionBehaviors = MyAvatar.motionBehaviors & ~AVATAR_MOTION_OBEY_GRAVITY; MyAvatar.motionBehaviors = MyAvatar.motionBehaviors & ~AVATAR_MOTION_OBEY_LOCAL_GRAVITY;
} }
} }
if (speed > MAX_COLLIDABLE_SPEED) { if (speed > MAX_COLLIDABLE_SPEED) {

View file

@ -857,7 +857,7 @@ void Application::keyPressEvent(QKeyEvent* event) {
case Qt::Key_G: case Qt::Key_G:
if (isShifted) { if (isShifted) {
Menu::getInstance()->triggerOption(MenuOption::ObeyGravity); Menu::getInstance()->triggerOption(MenuOption::ObeyEnvironmentalGravity);
} }
break; break;

View file

@ -188,7 +188,7 @@ Menu::Menu() :
addDisabledActionAndSeparator(editMenu, "Physics"); addDisabledActionAndSeparator(editMenu, "Physics");
QObject* avatar = appInstance->getAvatar(); QObject* avatar = appInstance->getAvatar();
addCheckableActionToQMenuAndActionHash(editMenu, MenuOption::ObeyGravity, Qt::SHIFT | Qt::Key_G, true, addCheckableActionToQMenuAndActionHash(editMenu, MenuOption::ObeyEnvironmentalGravity, Qt::SHIFT | Qt::Key_G, true,
avatar, SLOT(updateMotionBehaviors())); avatar, SLOT(updateMotionBehaviors()));

View file

@ -315,7 +315,7 @@ namespace MenuOption {
const QString GoTo = "Go To..."; const QString GoTo = "Go To...";
const QString GoToDomain = "Go To Domain..."; const QString GoToDomain = "Go To Domain...";
const QString GoToLocation = "Go To Location..."; const QString GoToLocation = "Go To Location...";
const QString ObeyGravity = "Obey Gravity"; const QString ObeyEnvironmentalGravity = "Obey Environmental Gravity";
const QString HandsCollideWithSelf = "Collide With Self"; const QString HandsCollideWithSelf = "Collide With Self";
const QString HeadMouse = "Head Mouse"; const QString HeadMouse = "Head Mouse";
const QString IncreaseAvatarSize = "Increase Avatar Size"; const QString IncreaseAvatarSize = "Increase Avatar Size";

View file

@ -125,10 +125,8 @@ void MyAvatar::update(float deltaTime) {
head->setAudioLoudness(audio->getLastInputLoudness()); head->setAudioLoudness(audio->getLastInputLoudness());
head->setAudioAverageLoudness(audio->getAudioAverageInputLoudness()); head->setAudioAverageLoudness(audio->getAudioAverageInputLoudness());
if (_motionBehaviors & AVATAR_MOTION_OBEY_GRAVITY) { if (_motionBehaviors & AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY) {
setGravity(Application::getInstance()->getEnvironment()->getGravity(getPosition())); _gravity = Application::getInstance()->getEnvironment()->getGravity(getPosition());
} else {
setGravity(glm::vec3(0.0f, 0.0f, 0.0f));
} }
simulate(deltaTime); simulate(deltaTime);
@ -463,6 +461,27 @@ void MyAvatar::renderHeadMouse() const {
*/ */
} }
void MyAvatar::setLocalGravity(glm::vec3 gravity) {
_motionBehaviors |= AVATAR_MOTION_OBEY_LOCAL_GRAVITY;
// Environmental and Local gravities are incompatible. Since Local is being set here
// the environmental setting must be removed.
_motionBehaviors &= ~AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY;
setGravity(gravity);
}
void MyAvatar::setGravity(const glm::vec3& gravity) {
_gravity = gravity;
getHead()->setGravity(_gravity);
// use the gravity to determine the new world up direction, if possible
float gravityLength = glm::length(gravity);
if (gravityLength > EPSILON) {
_worldUpDirection = _gravity / -gravityLength;
} else {
_worldUpDirection = DEFAULT_UP_DIRECTION;
}
}
void MyAvatar::saveData(QSettings* settings) { void MyAvatar::saveData(QSettings* settings) {
settings->beginGroup("Avatar"); settings->beginGroup("Avatar");
@ -1046,19 +1065,6 @@ void MyAvatar::maybeUpdateBillboard() {
sendBillboardPacket(); sendBillboardPacket();
} }
void MyAvatar::setGravity(glm::vec3 gravity) {
_gravity = gravity;
getHead()->setGravity(_gravity);
// use the gravity to determine the new world up direction, if possible
float gravityLength = glm::length(gravity);
if (gravityLength > EPSILON) {
_worldUpDirection = _gravity / -gravityLength;
} else {
_worldUpDirection = DEFAULT_UP_DIRECTION;
}
}
void MyAvatar::goHome() { void MyAvatar::goHome() {
qDebug("Going Home!"); qDebug("Going Home!");
setPosition(START_LOCATION); setPosition(START_LOCATION);
@ -1147,8 +1153,13 @@ void MyAvatar::goToLocationFromResponse(const QJsonObject& jsonObject) {
void MyAvatar::updateMotionBehaviors() { void MyAvatar::updateMotionBehaviors() {
_motionBehaviors = 0; _motionBehaviors = 0;
if (Menu::getInstance()->isOptionChecked(MenuOption::ObeyGravity)) { if (Menu::getInstance()->isOptionChecked(MenuOption::ObeyEnvironmentalGravity)) {
_motionBehaviors |= AVATAR_MOTION_OBEY_GRAVITY; _motionBehaviors |= AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY;
// Environmental and Local gravities are incompatible. Environmental setting trumps local.
_motionBehaviors &= ~AVATAR_MOTION_OBEY_LOCAL_GRAVITY;
}
if (! (_motionBehaviors & (AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY | AVATAR_MOTION_OBEY_LOCAL_GRAVITY))) {
_gravity = glm::vec3(0.0f);
} }
} }
@ -1164,7 +1175,14 @@ void MyAvatar::setCollisionGroups(quint32 collisionGroups) {
void MyAvatar::setMotionBehaviors(quint32 flags) { void MyAvatar::setMotionBehaviors(quint32 flags) {
_motionBehaviors = flags; _motionBehaviors = flags;
Menu* menu = Menu::getInstance(); Menu* menu = Menu::getInstance();
menu->setIsOptionChecked(MenuOption::ObeyGravity, (bool)(_motionBehaviors & AVATAR_MOTION_OBEY_GRAVITY)); menu->setIsOptionChecked(MenuOption::ObeyEnvironmentalGravity, (bool)(_motionBehaviors & AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY));
// Environmental and Local gravities are incompatible. Environmental setting trumps local.
if (_motionBehaviors & AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY) {
_motionBehaviors &= ~AVATAR_MOTION_OBEY_LOCAL_GRAVITY;
setGravity(Application::getInstance()->getEnvironment()->getGravity(getPosition()));
} else if (! (_motionBehaviors & (AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY | AVATAR_MOTION_OBEY_LOCAL_GRAVITY))) {
_gravity = glm::vec3(0.0f);
}
} }
void MyAvatar::applyCollision(const glm::vec3& contactPoint, const glm::vec3& penetration) { void MyAvatar::applyCollision(const glm::vec3& contactPoint, const glm::vec3& penetration) {

View file

@ -29,6 +29,7 @@ class MyAvatar : public Avatar {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool shouldRenderLocally READ getShouldRenderLocally WRITE setShouldRenderLocally) Q_PROPERTY(bool shouldRenderLocally READ getShouldRenderLocally WRITE setShouldRenderLocally)
Q_PROPERTY(quint32 motionBehaviors READ getMotionBehaviors WRITE setMotionBehaviors) Q_PROPERTY(quint32 motionBehaviors READ getMotionBehaviors WRITE setMotionBehaviors)
Q_PROPERTY(glm::vec3 gravity READ getGravity WRITE setLocalGravity)
public: public:
MyAvatar(); MyAvatar();
@ -50,8 +51,7 @@ public:
void setMousePressed(bool mousePressed) { _mousePressed = mousePressed; } void setMousePressed(bool mousePressed) { _mousePressed = mousePressed; }
void setVelocity(const glm::vec3 velocity) { _velocity = velocity; } void setVelocity(const glm::vec3 velocity) { _velocity = velocity; }
void setLeanScale(float scale) { _leanScale = scale; } void setLeanScale(float scale) { _leanScale = scale; }
void setGravity(glm::vec3 gravity); void setLocalGravity(glm::vec3 gravity);
void setMoveTarget(const glm::vec3 moveTarget);
void setShouldRenderLocally(bool shouldRender) { _shouldRender = shouldRender; } void setShouldRenderLocally(bool shouldRender) { _shouldRender = shouldRender; }
// getters // getters
@ -121,6 +121,7 @@ private:
bool _shouldJump; bool _shouldJump;
float _driveKeys[MAX_DRIVE_KEYS]; float _driveKeys[MAX_DRIVE_KEYS];
glm::vec3 _gravity; glm::vec3 _gravity;
glm::vec3 _environmentGravity;
float _distanceToNearestAvatar; // How close is the nearest avatar? float _distanceToNearestAvatar; // How close is the nearest avatar?
// motion stuff // motion stuff
@ -149,6 +150,7 @@ private:
void updateCollisionSound(const glm::vec3& penetration, float deltaTime, float frequency); void updateCollisionSound(const glm::vec3& penetration, float deltaTime, float frequency);
void updateChatCircle(float deltaTime); void updateChatCircle(float deltaTime);
void maybeUpdateBillboard(); void maybeUpdateBillboard();
void setGravity(const glm::vec3& gravity);
}; };
#endif // hifi_MyAvatar_h #endif // hifi_MyAvatar_h

View file

@ -51,7 +51,8 @@ typedef unsigned long long quint64;
#include "HandData.h" #include "HandData.h"
// avatar motion behaviors // avatar motion behaviors
const quint32 AVATAR_MOTION_OBEY_GRAVITY = 1U << 0; const quint32 AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY = 1U << 0;
const quint32 AVATAR_MOTION_OBEY_LOCAL_GRAVITY = 1U << 1;
// First bitset // First bitset
const int KEY_STATE_START_BIT = 0; // 1st and 2nd bits const int KEY_STATE_START_BIT = 0; // 1st and 2nd bits

View file

@ -239,7 +239,8 @@ void ScriptEngine::init() {
globalObject.setProperty("COLLISION_GROUP_VOXELS", _engine.newVariant(QVariant(COLLISION_GROUP_VOXELS))); globalObject.setProperty("COLLISION_GROUP_VOXELS", _engine.newVariant(QVariant(COLLISION_GROUP_VOXELS)));
globalObject.setProperty("COLLISION_GROUP_PARTICLES", _engine.newVariant(QVariant(COLLISION_GROUP_PARTICLES))); globalObject.setProperty("COLLISION_GROUP_PARTICLES", _engine.newVariant(QVariant(COLLISION_GROUP_PARTICLES)));
globalObject.setProperty("AVATAR_MOTION_OBEY_GRAVITY", _engine.newVariant(QVariant(AVATAR_MOTION_OBEY_GRAVITY))); globalObject.setProperty("AVATAR_MOTION_OBEY_LOCAL_GRAVITY", _engine.newVariant(QVariant(AVATAR_MOTION_OBEY_LOCAL_GRAVITY)));
globalObject.setProperty("AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY", _engine.newVariant(QVariant(AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY)));
// let the VoxelPacketSender know how frequently we plan to call it // let the VoxelPacketSender know how frequently we plan to call it
_voxelsScriptingInterface.getVoxelPacketSender()->setProcessCallIntervalHint(SCRIPT_DATA_CALLBACK_USECS); _voxelsScriptingInterface.getVoxelPacketSender()->setProcessCallIntervalHint(SCRIPT_DATA_CALLBACK_USECS);