From 83a1654d9cc19dbba073ac03268edf78e0cbce54 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Mon, 9 Dec 2013 15:12:28 -0800 Subject: [PATCH] making toy ball --- interface/src/Application.cpp | 1 + interface/src/avatar/Hand.cpp | 52 +++++++++++++++++++++++- interface/src/avatar/Hand.h | 5 +++ interface/src/devices/SixenseManager.cpp | 14 +++---- libraries/avatars/src/HandData.cpp | 1 + libraries/avatars/src/HandData.h | 4 +- 6 files changed, 67 insertions(+), 10 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 7d10928d87..30a1dfa3c9 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2475,6 +2475,7 @@ void Application::updateAvatar(float deltaTime) { // apply pitch from touch _myAvatar.getHead().setMousePitch(_myAvatar.getHead().getMousePitch() + _pitchFromTouch); + _pitchFromTouch = 0.0f; // Update my avatar's state from gyros and/or webcam diff --git a/interface/src/avatar/Hand.cpp b/interface/src/avatar/Hand.cpp index 7d735a5ced..c5d0fe371d 100755 --- a/interface/src/avatar/Hand.cpp +++ b/interface/src/avatar/Hand.cpp @@ -27,7 +27,10 @@ Hand::Hand(Avatar* owningAvatar) : _ballColor(0.0, 0.0, 0.4), _collisionCenter(0,0,0), _collisionAge(0), - _collisionDuration(0) + _collisionDuration(0), + _toyBallPosition(0), + _toyBallVelocity(0), + _toyBallInHand(false) { } @@ -64,13 +67,50 @@ void Hand::simulate(float deltaTime, bool isMine) { calculateGeometry(); if (isMine) { - // Create a voxel at fingertip if controller button is pressed const float FINGERTIP_VOXEL_SIZE = 0.0125; + + // Iterate hand controllers, take actions as needed + for (size_t i = 0; i < getNumPalms(); ++i) { PalmData& palm = getPalms()[i]; if (palm.isActive()) { FingerData& finger = palm.getFingers()[0]; // Sixense has only one finger glm::vec3 fingerTipPosition = finger.getTipPosition(); + // Toy ball game + if (palm.getSixenseID() == 0) { + if (palm.getControllerButtons() & BUTTON_FWD) { + // If grabbing toy ball, add forces to it + if (!_toyBallInHand) { + // Test for whether close enough to catch and catch + bool isCaught = true; + if (isCaught) { + _toyBallInHand = true; + } + } + if (_toyBallInHand) { + // Ball is in hand + _toyBallPosition = fingerTipPosition; + _toyBallVelocity = glm::vec3(0); + + } + } else { + // If toy ball just released, add velocity to it! + if (_toyBallInHand) { + _toyBallInHand = false; + glm::vec3 handVelocity = palm.getVelocity(); + printVector(handVelocity); + _toyBallVelocity += handVelocity; + } + } + // Simulate toy ball + const glm::vec3 TOYBALL_GRAVITY (0, -1, 0); + _toyBallPosition += _toyBallVelocity * deltaTime; + if (!_toyBallInHand) { + _toyBallVelocity += TOYBALL_GRAVITY * deltaTime; + } + _toyBallVelocity *= 0.99f; + } + if (palm.getControllerButtons() & BUTTON_1) { if (glm::length(fingerTipPosition - _lastFingerAddVoxel) > (FINGERTIP_VOXEL_SIZE / 2.f)) { QColor paintColor = Menu::getInstance()->getActionForOption(MenuOption::VoxelPaintColor)->data().value(); @@ -253,6 +293,14 @@ void Hand::render( bool isMine) { renderLeapHands(); } + // Render toy ball + if (isMine) { + glPushMatrix(); + glColor4f(1, 0, 0, 0.5); + glTranslatef(_toyBallPosition.x, _toyBallPosition.y, _toyBallPosition.z); + glutSolidSphere(0.1f, 10, 10); + glPopMatrix(); + } if (isMine) { // If hand/voxel collision has happened, render a little expanding sphere diff --git a/interface/src/avatar/Hand.h b/interface/src/avatar/Hand.h index 12d6d0d8da..6900e40815 100755 --- a/interface/src/avatar/Hand.h +++ b/interface/src/avatar/Hand.h @@ -86,6 +86,11 @@ private: void calculateGeometry(); void handleVoxelCollision(PalmData* palm, const glm::vec3& fingerTipPosition, VoxelTreeElement* voxel, float deltaTime); + + glm::vec3 _toyBallPosition; + glm::vec3 _toyBallVelocity; + bool _toyBallInHand; + }; #endif diff --git a/interface/src/devices/SixenseManager.cpp b/interface/src/devices/SixenseManager.cpp index e7414895e3..da7f710c11 100644 --- a/interface/src/devices/SixenseManager.cpp +++ b/interface/src/devices/SixenseManager.cpp @@ -42,8 +42,6 @@ void SixenseManager::update(float deltaTime) { sixenseControllerData data; sixenseGetNewestData(i, &data); - //printf("si: %i\n", data.controller_index); - // Set palm position and normal based on Hydra position/orientation // Either find a palm matching the sixense controller, or make a new one @@ -82,16 +80,18 @@ void SixenseManager::update(float deltaTime) { const glm::vec3 SPHERE_TO_TORSO(-250.f, -300.f, -300.f); position = SPHERE_TO_TORSO + position; - // Compute current velocity from position change - palm->setVelocity((position - palm->getRawPosition()) / deltaTime / 1000.f); // meters/sec - palm->setRawPosition(position); - // Rotation of Palm glm::quat rotation(data.rot_quat[3], -data.rot_quat[0], data.rot_quat[1], -data.rot_quat[2]); rotation = glm::angleAxis(180.0f, 0.f, 1.f, 0.f) * rotation; const glm::vec3 PALM_VECTOR(0.0f, -1.0f, 0.0f); - palm->setRawNormal(rotation * PALM_VECTOR); + glm::vec3 newNormal = rotation * PALM_VECTOR; + palm->setRawNormal(newNormal); + // Compute current velocity from position change + glm::vec3 rawVelocity = (position - palm->getRawPosition()) / deltaTime / 1000.f; + palm->setVelocity(rotation * rawVelocity); // meters/sec + palm->setRawPosition(position); + // initialize the "finger" based on the direction FingerData finger(palm, &hand); finger.setActive(true); diff --git a/libraries/avatars/src/HandData.cpp b/libraries/avatars/src/HandData.cpp index d525bbd7df..1705673b43 100644 --- a/libraries/avatars/src/HandData.cpp +++ b/libraries/avatars/src/HandData.cpp @@ -62,6 +62,7 @@ PalmData::PalmData(HandData* owningHandData) : _rawPosition(0, 0, 0), _rawNormal(0, 1, 0), _velocity(0, 0, 0), +_rotationalVelocity(0, 0, 0), _controllerButtons(0), _isActive(false), _leapID(LEAPID_INVALID), diff --git a/libraries/avatars/src/HandData.h b/libraries/avatars/src/HandData.h index fb5cbff763..461820b0b3 100755 --- a/libraries/avatars/src/HandData.h +++ b/libraries/avatars/src/HandData.h @@ -140,7 +140,7 @@ public: 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 addToPosition(const glm::vec3& delta); void incrementFramesWithoutData() { _numFramesWithoutData++; } @@ -165,6 +165,8 @@ private: glm::vec3 _rawPosition; glm::vec3 _rawNormal; glm::vec3 _velocity; + glm::vec3 _rotationalVelocity; + glm::quat _lastRotation; int _controllerButtons; float _trigger; float _joystickX, _joystickY;