Basic joystick thrust/view drive behavior

This commit is contained in:
Philip Rosedale 2013-12-10 16:52:08 -08:00
parent 0f0ee07f8c
commit c9f96d1adf
7 changed files with 69 additions and 62 deletions

View file

@ -2477,8 +2477,10 @@ void Application::updateAvatar(float deltaTime) {
_yawFromTouch = 0.f;
// apply pitch from touch
_myAvatar.getHead().setMousePitch(_myAvatar.getHead().getMousePitch() + _pitchFromTouch);
_myAvatar.getHead().setMousePitch(_myAvatar.getHead().getMousePitch() +
_myAvatar.getHand().getPitchUpdate() +
_pitchFromTouch);
_myAvatar.getHand().setPitchUpdate(0.f);
_pitchFromTouch = 0.0f;
// Update my avatar's state from gyros and/or webcam

View file

@ -19,6 +19,8 @@
using namespace std;
const int TOY_BALL_HAND = 1;
Hand::Hand(Avatar* owningAvatar) :
HandData((AvatarData*)owningAvatar),
@ -30,7 +32,8 @@ Hand::Hand(Avatar* owningAvatar) :
_collisionDuration(0),
_toyBallPosition(0),
_toyBallVelocity(0),
_toyBallInHand(false)
_toyBallInHand(false),
_pitchUpdate(0)
{
}
@ -77,7 +80,7 @@ void Hand::simulate(float deltaTime, bool isMine) {
FingerData& finger = palm.getFingers()[0]; // Sixense has only one finger
glm::vec3 fingerTipPosition = finger.getTipPosition();
// Toy ball game
if (palm.getSixenseID() == 0) {
if (palm.getSixenseID() == TOY_BALL_HAND) {
if (palm.getControllerButtons() & BUTTON_FWD) {
// If grabbing toy ball, add forces to it
if (!_toyBallInHand) {
@ -97,9 +100,11 @@ void Hand::simulate(float deltaTime, bool isMine) {
// If toy ball just released, add velocity to it!
if (_toyBallInHand) {
_toyBallInHand = false;
glm::vec3 handVelocity = palm.getVelocity();
printVector(handVelocity);
_toyBallVelocity += handVelocity;
glm::vec3 handVelocity = palm.getRawVelocity();
glm::vec3 fingerTipVelocity = palm.getTipVelocity();
glm::quat avatarRotation = _owningAvatar->getOrientation();
//printVector(avatarRotation * handVelocity);
_toyBallVelocity += avatarRotation * fingerTipVelocity;
}
}
// Simulate toy ball
@ -211,7 +216,7 @@ void Hand::handleVoxelCollision(PalmData* palm, const glm::vec3& fingerTipPositi
const float DECAY_PER_SAMPLE = 0.0005f;
const float DURATION_MAX = 2.0f;
const float MIN_VOLUME = 0.1f;
float volume = MIN_VOLUME + glm::clamp(glm::length(palm->getVelocity()), 0.f, (1.f - MIN_VOLUME));
float volume = MIN_VOLUME + glm::clamp(glm::length(palm->getRawVelocity()), 0.f, (1.f - MIN_VOLUME));
float duration = volume;
_collisionCenter = fingerTipPosition;
_collisionAge = deltaTime;
@ -296,9 +301,10 @@ void Hand::render( bool isMine) {
// Render toy ball
if (isMine) {
glPushMatrix();
glColor4f(1, 0, 0, 0.5);
const float TOY_BALL_RADIUS = 0.05f;
glColor3f(1, 0, 0);
glTranslatef(_toyBallPosition.x, _toyBallPosition.y, _toyBallPosition.z);
glutSolidSphere(0.1f, 10, 10);
glutSolidSphere(TOY_BALL_RADIUS, 10, 10);
glPopMatrix();
}
@ -315,47 +321,8 @@ void Hand::render( bool isMine) {
_collisionAge = 0.f;
}
}
// If hand controller buttons pressed, render stuff as needed
if (getPalms().size() > 0) {
for (size_t i = 0; i < getPalms().size(); ++i) {
PalmData& palm = getPalms()[i];
// If trigger pulled, thrust in that direction and draw beam
const float MAX_THRUSTER_BEAM_LENGTH = 5.f;
const float THRUSTER_MARKER_SIZE = 0.0125f;
if (palm.getJoystickY() != 0.f) {
FingerData& finger = palm.getFingers()[0];
if (finger.isActive()) {
if (palm.getJoystickY() > 0.f) {
glColor3f(0, 1, 0);
} else {
glColor3f(1, 0, 0);
}
glm::vec3 palmPosition = palm.getPosition();
glm::vec3 pointerPosition = palmPosition +
glm::normalize(finger.getTipPosition() - palmPosition) *
MAX_THRUSTER_BEAM_LENGTH;
glPushMatrix();
glm::vec3 markerPosition = palmPosition +
glm::normalize(finger.getTipPosition() - palmPosition) *
MAX_THRUSTER_BEAM_LENGTH *
(0.5f + palm.getJoystickY() / 2.f);
glTranslatef(markerPosition.x, markerPosition.y, markerPosition.z);
glutSolidSphere(THRUSTER_MARKER_SIZE, 10, 10);
glPopMatrix();
glLineWidth(2.0);
glBegin(GL_LINES);
glVertex3f(palmPosition.x, palmPosition.y, palmPosition.z);
glVertex3f(pointerPosition.x, pointerPosition.y, pointerPosition.z);
glEnd();
}
}
}
}
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_RESCALE_NORMAL);

View file

@ -54,6 +54,10 @@ public:
const glm::vec3& getLeapFingerTipBallPosition (int ball) const { return _leapFingerTipBalls [ball].position;}
const glm::vec3& getLeapFingerRootBallPosition(int ball) const { return _leapFingerRootBalls[ball].position;}
// Pitch from controller input to view
const float getPitchUpdate() const { return _pitchUpdate; }
void setPitchUpdate(float pitchUpdate) { _pitchUpdate = pitchUpdate; }
private:
// disallow copies of the Hand, copy of owning Avatar is disallowed too
Hand(const Hand&);
@ -90,6 +94,8 @@ private:
glm::vec3 _toyBallPosition;
glm::vec3 _toyBallVelocity;
bool _toyBallInHand;
float _pitchUpdate;
};

View file

@ -667,20 +667,32 @@ void MyAvatar::updateThrust(float deltaTime, Transmitter * transmitter) {
// Add thrust and rotation from hand controllers
const float THRUST_MAG_HAND_JETS = THRUST_MAG_FWD;
const float JOYSTICK_YAW_MAG = YAW_MAG;
const float JOYSTICK_PITCH_MAG = PITCH_MAG;
const int THRUST_CONTROLLER = 0;
const int VIEW_CONTROLLER = 1;
for (size_t i = 0; i < getHand().getPalms().size(); ++i) {
PalmData& palm = getHand().getPalms()[i];
if (palm.isActive()) {
if (palm.isActive() && (palm.getSixenseID() == THRUST_CONTROLLER)) {
if (palm.getJoystickY() != 0.f) {
FingerData& finger = palm.getFingers()[0];
if (finger.isActive()) {
}
glm::vec3 thrustDirection = glm::normalize(finger.getTipPosition() - palm.getPosition());
_thrust += thrustDirection * _scale * THRUST_MAG_HAND_JETS * palm.getJoystickY() * _thrustMultiplier * deltaTime;
//glm::vec3 thrustDirection = glm::normalize(finger.getTipPosition() - palm.getPosition());
_thrust += front * _scale * THRUST_MAG_HAND_JETS * palm.getJoystickY() * _thrustMultiplier * deltaTime;
}
if (palm.getJoystickX() != 0.f) {
_thrust += right * _scale * THRUST_MAG_HAND_JETS * palm.getJoystickX() * _thrustMultiplier * deltaTime;
}
} else if (palm.isActive() && (palm.getSixenseID() == VIEW_CONTROLLER)) {
if (palm.getJoystickX() != 0.f) {
_bodyYawDelta -= palm.getJoystickX() * JOYSTICK_YAW_MAG * deltaTime;
}
if (palm.getJoystickY() != 0.f) {
getHand().setPitchUpdate(getHand().getPitchUpdate() +
(palm.getJoystickY() * JOYSTICK_PITCH_MAG * deltaTime));
}
}
}
// Update speed brake status

View file

@ -16,7 +16,7 @@
SixenseManager::SixenseManager() {
#ifdef HAVE_SIXENSE
sixenseInit();
sixenseSetFilterEnabled(0);
sixenseSetFilterEnabled(1);
#endif
}
@ -86,20 +86,27 @@ void SixenseManager::update(float deltaTime) {
const glm::vec3 PALM_VECTOR(0.0f, -1.0f, 0.0f);
glm::vec3 newNormal = rotation * PALM_VECTOR;
palm->setRawNormal(newNormal);
palm->setRawRotation(rotation);
// Compute current velocity from position change
glm::vec3 rawVelocity = (position - palm->getRawPosition()) / deltaTime / 1000.f;
palm->setVelocity(rotation * rawVelocity); // meters/sec
palm->setRawVelocity(rawVelocity); // meters/sec
palm->setRawPosition(position);
// initialize the "finger" based on the direction
FingerData finger(palm, &hand);
finger.setActive(true);
finger.setRawRootPosition(position);
const float FINGER_LENGTH = 300.0f; // Millimeters
const glm::vec3 FINGER_VECTOR(0.0f, 0.0f, FINGER_LENGTH);
const glm::vec3 newTipPosition = position + rotation * FINGER_VECTOR;
finger.setRawTipPosition(position + rotation * FINGER_VECTOR);
// temporary for toy ball - store first finger tip velocity
glm::vec3 oldTipPosition = palm->getTipPosition();
palm->setTipVelocity((newTipPosition - oldTipPosition) / deltaTime / 1000.f);
palm->setTipPosition(newTipPosition);
// three fingers indicates to the skeleton that we have enough data to determine direction
palm->getFingers().clear();
palm->getFingers().push_back(finger);

View file

@ -59,9 +59,10 @@ void HandData::getLeftRightPalmIndices(int& leftPalmIndex, int& rightPalmIndex)
}
PalmData::PalmData(HandData* owningHandData) :
_rawRotation(0, 0, 0, 1),
_rawPosition(0, 0, 0),
_rawNormal(0, 1, 0),
_velocity(0, 0, 0),
_rawVelocity(0, 0, 0),
_rotationalVelocity(0, 0, 0),
_controllerButtons(0),
_isActive(false),

View file

@ -94,6 +94,7 @@ public:
void setLeapID(int id) { _leapID = id; }
void setRawTipPosition(const glm::vec3& pos) { _tipRawPosition = pos; }
void setRawRootPosition(const glm::vec3& pos) { _rootRawPosition = pos; }
void setTrailLength(unsigned int length);
void updateTrail();
@ -122,6 +123,7 @@ public:
PalmData(HandData* owningHandData);
glm::vec3 getPosition() const { return _owningHandData->leapPositionToWorldPosition(_rawPosition); }
glm::vec3 getNormal() const { return _owningHandData->leapDirectionToWorldDirection(_rawNormal); }
const glm::vec3& getRawPosition() const { return _rawPosition; }
const glm::vec3& getRawNormal() const { return _rawNormal; }
bool isActive() const { return _isActive; }
@ -136,13 +138,19 @@ public:
void setLeapID(int id) { _leapID = id; }
void setSixenseID(int id) { _sixenseID = id; }
void setRawRotation(const glm::quat rawRotation) { _rawRotation = rawRotation; };
const glm::quat getRawRotation() const { return _rawRotation; }
void setRawPosition(const glm::vec3& pos) { _rawPosition = pos; }
void setRawNormal(const glm::vec3& normal) { _rawNormal = normal; }
void setVelocity(const glm::vec3& velocity) { _velocity = velocity; }
const glm::vec3& getVelocity() const { return _velocity; }
const glm::vec3& getRotationalVelocity() const { return _rotationalVelocity; }
void setRawVelocity(const glm::vec3& velocity) { _rawVelocity = velocity; }
const glm::vec3& getRawVelocity() const { return _rawVelocity; }
void addToPosition(const glm::vec3& delta);
void setTipPosition(const glm::vec3& position) { _tipPosition = position; }
const glm::vec3 getTipPosition() const { return _tipPosition; }
const glm::vec3& getTipVelocity() const { return _tipVelocity; }
void setTipVelocity(const glm::vec3& velocity) { _tipVelocity = velocity; }
void incrementFramesWithoutData() { _numFramesWithoutData++; }
void resetFramesWithoutData() { _numFramesWithoutData = 0; }
int getFramesWithoutData() const { return _numFramesWithoutData; }
@ -162,11 +170,15 @@ public:
private:
std::vector<FingerData> _fingers;
glm::quat _rawRotation;
glm::vec3 _rawPosition;
glm::vec3 _rawNormal;
glm::vec3 _velocity;
glm::vec3 _rawVelocity;
glm::vec3 _rotationalVelocity;
glm::quat _lastRotation;
glm::vec3 _tipPosition;
glm::vec3 _tipVelocity;
int _controllerButtons;
float _trigger;
float _joystickX, _joystickY;