From 31d92fd90ab37a482de3db966d77f419855df172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Brisset?= Date: Fri, 30 Oct 2015 12:51:30 -0700 Subject: [PATCH 01/10] Baseball test code --- interface/src/Application.cpp | 10 +++++-- interface/src/avatar/AvatarActionHold.cpp | 22 +++++++++++++-- .../input-plugins/ViveControllerManager.cpp | 28 +++++++++++++++---- .../src/input-plugins/ViveControllerManager.h | 8 ++++-- libraries/shared/src/PhysicsHelpers.h | 2 +- 5 files changed, 56 insertions(+), 14 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index eebd26c3c3..8f9fc9b16e 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -950,8 +950,8 @@ void Application::initializeGL() { checkFPStimer.start(1000); // call our idle function whenever we can - connect(&idleTimer, &QTimer::timeout, this, &Application::idle); - idleTimer.start(TARGET_SIM_FRAME_PERIOD_MS); + // connect(&idleTimer, &QTimer::timeout, this, &Application::idle); + // idleTimer.start(TARGET_SIM_FRAME_PERIOD_MS); _idleLoopStdev.reset(); // update before the first render @@ -1023,6 +1023,10 @@ void Application::paintGL() { if (_inPaint) { return; } + + // this is a good idea + idle(); + _inPaint = true; Finally clearFlagLambda([this] { _inPaint = false; }); @@ -2070,7 +2074,7 @@ void Application::idle() { float secondsSinceLastUpdate = timeSinceLastUpdateUs / USECS_PER_SECOND; if (isThrottled && (timeSinceLastUpdateUs / USECS_PER_MSEC) < THROTTLED_SIM_FRAME_PERIOD_MS) { - return; // bail early, we're throttled and not enough time has elapsed + //return; // bail early, we're throttled and not enough time has elapsed } _lastTimeUpdated.start(); diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 59bfd88be6..941e876075 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -35,7 +35,8 @@ AvatarActionHold::~AvatarActionHold() { qDebug() << "AvatarActionHold::~AvatarActionHold"; #endif } - +#include +#include void AvatarActionHold::updateActionWorker(float deltaTimeStep) { bool gotLock = false; glm::quat rotation; @@ -51,7 +52,24 @@ void AvatarActionHold::updateActionWorker(float deltaTimeStep) { glm::vec3 offset; glm::vec3 palmPosition; glm::quat palmRotation; - if (_hand == "right") { + + const auto& plugins = PluginManager::getInstance()->getInputPlugins(); + auto it = std::find_if(std::begin(plugins), std::end(plugins), [](const InputPluginPointer& plugin) { + return plugin->getName() == ViveControllerManager::NAME; + }); + + if (it != std::end(plugins)) { + const auto& vive = it->dynamicCast(); + auto index = (_hand == "right") ? 0 : 1; auto userInputMapper = DependencyManager::get(); + auto translation = extractTranslation(userInputMapper->getSensorToWorldMat()); + auto rotation = glm::quat_cast(userInputMapper->getSensorToWorldMat()); + + + const glm::quat quarterX = glm::angleAxis(PI / 2.0f, glm::vec3(1.0f, 0.0f, 0.0f)); + const glm::quat yFlip = glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f)); + palmPosition = translation + rotation * vive->getPosition(index); + palmRotation = rotation * vive->getRotation(index) * yFlip * quarterX; + } else if (_hand == "right") { palmPosition = holdingAvatar->getRightPalmPosition(); palmRotation = holdingAvatar->getRightPalmRotation(); } else { diff --git a/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp b/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp index bb8267b616..28e001208c 100644 --- a/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp +++ b/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp @@ -217,6 +217,15 @@ void ViveControllerManager::renderHand(UserInputMapper::PoseValue pose, gpu::Bat batch.drawIndexed(gpu::TRIANGLES, mesh->getNumIndices(), 0); } +glm::vec3 ViveControllerManager::getPosition(int hand) const { + const mat4& mat = _trackedDevicePoseMat4[hand ? 3 : 4]; + return extractTranslation(mat); +} +glm::quat ViveControllerManager::getRotation(int hand) const { + const mat4& mat = _trackedDevicePoseMat4[hand ? 3 : 4]; + return glm::quat_cast(mat); +} + void ViveControllerManager::update(float deltaTime, bool jointsCaptured) { #ifdef Q_OS_WIN _poseStateMap.clear(); @@ -250,7 +259,7 @@ void ViveControllerManager::update(float deltaTime, bool jointsCaptured) { numTrackedControllers++; const mat4& mat = _trackedDevicePoseMat4[device]; - + if (!jointsCaptured) { handlePoseEvent(mat, numTrackedControllers - 1); } @@ -372,16 +381,23 @@ void ViveControllerManager::handlePoseEvent(const mat4& mat, int index) { // Q = (deltaQ * QOffset) * (yFlip * quarterTurnAboutX) // // Q = (deltaQ * inverse(deltaQForAlignedHand)) * (yFlip * quarterTurnAboutX) - + + float sign = (index == LEFT_HAND) ? -1.0f : 1.0f; + const glm::quat quarterX = glm::angleAxis(PI / 2.0f, glm::vec3(1.0f, 0.0f, 0.0f)); const glm::quat yFlip = glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f)); - float sign = (index == LEFT_HAND) ? -1.0f : 1.0f; const glm::quat signedQuaterZ = glm::angleAxis(sign * PI / 2.0f, glm::vec3(0.0f, 0.0f, 1.0f)); const glm::quat eighthX = glm::angleAxis(PI / 4.0f, glm::vec3(1.0f, 0.0f, 0.0f)); - const glm::quat offset = glm::inverse(signedQuaterZ * eighthX); - rotation = rotation * offset * yFlip * quarterX; + + + const glm::quat rotationOffset = glm::inverse(signedQuaterZ * eighthX) * yFlip * quarterX; + const glm::vec3 translationOffset = glm::vec3(sign * CONTROLLER_LENGTH_OFFSET / 2.0f, + CONTROLLER_LENGTH_OFFSET / 2.0f, + 2.0f * CONTROLLER_LENGTH_OFFSET); - position += rotation * glm::vec3(0, 0, -CONTROLLER_LENGTH_OFFSET); + position += rotation * translationOffset; + rotation = rotation * rotationOffset; + //{quat, x = 0.653281, y = -0.270598, z = 0.653281, w = 0.270598}{vec3, x = 0.0381, y = -0.0381, z = -0.1524} _poseStateMap[makeInput(JointChannel(index)).getChannel()] = UserInputMapper::PoseValue(position, rotation); } diff --git a/libraries/input-plugins/src/input-plugins/ViveControllerManager.h b/libraries/input-plugins/src/input-plugins/ViveControllerManager.h index 5cae8daaf4..f6220ca97a 100644 --- a/libraries/input-plugins/src/input-plugins/ViveControllerManager.h +++ b/libraries/input-plugins/src/input-plugins/ViveControllerManager.h @@ -27,6 +27,8 @@ class ViveControllerManager : public InputPlugin, public InputDevice { Q_OBJECT public: + static const QString NAME; + enum JoystickAxisChannel { AXIS_Y_POS = 1U << 1, AXIS_Y_NEG = 1U << 2, @@ -74,6 +76,10 @@ public: UserInputMapper::Input makeInput(unsigned int button, int index); UserInputMapper::Input makeInput(JoystickAxisChannel axis, int index); UserInputMapper::Input makeInput(JointChannel joint); + + int getNumDevices() const; + glm::vec3 getPosition(int device) const; + glm::quat getRotation(int device) const; private: void renderHand(UserInputMapper::PoseValue pose, gpu::Batch& batch, int index); @@ -92,8 +98,6 @@ private: int _rightHandRenderID; bool _renderControllers; - - static const QString NAME; }; #endif // hifi__ViveControllerManager diff --git a/libraries/shared/src/PhysicsHelpers.h b/libraries/shared/src/PhysicsHelpers.h index 7ceafea915..3b6fccdc99 100644 --- a/libraries/shared/src/PhysicsHelpers.h +++ b/libraries/shared/src/PhysicsHelpers.h @@ -16,7 +16,7 @@ #include const int PHYSICS_ENGINE_MAX_NUM_SUBSTEPS = 6; // Bullet will start to "lose time" at 10 FPS. -const float PHYSICS_ENGINE_FIXED_SUBSTEP = 1.0f / 60.0f; +const float PHYSICS_ENGINE_FIXED_SUBSTEP = 1.0f / 90.0f; // return incremental rotation (Bullet-style) caused by angularVelocity over timeStep glm::quat computeBulletRotationStep(const glm::vec3& angularVelocity, float timeStep); From 01bbd3665e2cdcb78a4770b0e7ca5da88e80915f Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 30 Oct 2015 14:37:06 -0700 Subject: [PATCH 02/10] Remove accidentally commited file --- examples/map.js~ | 323 ----------------------------------------------- 1 file changed, 323 deletions(-) delete mode 100644 examples/map.js~ diff --git a/examples/map.js~ b/examples/map.js~ deleted file mode 100644 index 5a4e0f0f8c..0000000000 --- a/examples/map.js~ +++ /dev/null @@ -1,323 +0,0 @@ -Script.include("entityManager.js"); -Script.include("overlayManager.js"); - - -// Poll for nearby map data - -var entityManager = new EntityManager(); - -// From http://evanw.github.io/lightgl.js/docs/raytracer.html -function raySphereIntersection(origin, ray, center, radius) { - var offset = Vec3.subtract(origin, center); - var a = Vec3.dot(ray, ray); - // var a = ray.dot(ray); - var b = 2 * Vec3.dot(ray, offset); - // var b = 2 * ray.dot(offset); - var c = Vec3.dot(offset, offset) - radius * radius; - // var c = offset.dot(offset) - radius * radius; - var discriminant = b * b - 4 * a * c; - - if (discriminant > 0) { - return true; - } - - return null; -}; - - -Map = function(data) { - var visible = false; - - var ROOT_OFFSET = Vec3.multiply(0.3, Quat.getFront(MyAvatar.orientation)); - var ROOT_POSITION = Vec3.sum(MyAvatar.position, ROOT_OFFSET); - - var ROOT_SCALE = 0.0005; - - // Create object in objectManager - var rootObject = entityManager.addBare(); - var position = ROOT_POSITION; - rootObject.position = ROOT_POSITION; - rootObject.scale = ROOT_SCALE - Vec3.print("Position:", position); - - // Search for all nearby objects that have the userData "mapped" - // TODO Update to use the zone's bounds - var entities = Entities.findEntities(MyAvatar.position, 32000); - var mappedEntities = []; - var minCorner = { - x: 4294967295, - y: 4294967295, - z: 4294967295, - }; - var maxCorner = { - x: -4294967295, - y: -4294967295, - z: -4294967295, - }; - - for (var i = 0; i < entities.length; ++i) { - var entityID = entities[i]; - var properties = Entities.getEntityProperties(entityID); - if (properties.userData == "mapped" || properties.userData == "tracked") { - - print("Found: ", properties.name); - - minCorner.x = Math.min(minCorner.x, properties.position.x - (properties.dimensions.x / 2)); - minCorner.y = Math.min(minCorner.y, properties.position.y - (properties.dimensions.y / 2)); - minCorner.z = Math.min(minCorner.z, properties.position.z - (properties.dimensions.z / 2)); - - maxCorner.x = Math.max(maxCorner.x, properties.position.x - (properties.dimensions.x / 2)); - maxCorner.y = Math.max(maxCorner.y, properties.position.y - (properties.dimensions.y / 2)); - maxCorner.z = Math.max(maxCorner.z, properties.position.z - (properties.dimensions.z / 2)); - - } - // if (properties.userData == "mapped") { - // properties.visible = false; - // var entity = entityManager.add(properties.type, properties); - // mappedEntities.push(entity); - // } else if (properties.userData == "tracked") { - // // TODO implement tracking of objects - // } - } - - var dimensions = { - x: maxCorner.x - minCorner.x, - y: maxCorner.y - minCorner.y, - z: maxCorner.z - minCorner.z, - }; - Vec3.print("dims", dimensions); - - var center = { - x: minCorner.x + (dimensions.x / 2), - y: minCorner.y + (dimensions.y / 2), - z: minCorner.z + (dimensions.z / 2), - }; - Vec3.print("center", center); - - var trackedEntities = []; - var waypointEntities = []; - for (var i = 0; i < entities.length; ++i) { - var entityID = entities[i]; - var properties = Entities.getEntityProperties(entityID); - var mapData = null; - try { - var data = JSON.parse(properties.userData.replace(/(\r\n|\n|\r)/gm,"")); - mapData = data.mapData; - } catch (e) { - print("Caught: ", properties.name); - } - - if (mapData) { - print("Creating copy of", properties.name); - properties.name += " (COPY)"; - properties.userData = ""; - properties.visible = true; - var position = properties.position; - properties.position = Vec3.subtract(properties.position, center); - properties.position = Vec3.multiply(properties.position, ROOT_SCALE); - var extra = { }; - - if (mapData.track) { - extra.trackingEntityID= entityID; - trackedEntities.push(entity); - rootObject.addChild(entity); - } - if (mapData.waypoint) { - print("Waypoint: ", mapData.waypoint.name); - // properties.type = "Model"; - // properties.modelURL = "atp:ca49a13938376b3eb68d7b2b9189afb3f580c07b6950ea9e65b5260787204145.fbx"; - extra.waypoint = mapData.waypoint; - extra.waypoint.position = position; - } - - var entity = entityManager.add(properties.type, properties); - entity.__extra__ = extra; - if (mapData.waypoint) { - waypointEntities.push(entity); - } - mappedEntities.push(entity); - - rootObject.addChild(entity); - - } else { - // print("Not creating copy of", properties.name); - } - } - - var avatarArrowEntity = entityManager.add("Model", { - name: "You Are Here", - modelURL: "atp:ce4f0c4e491e40b73d28f2646da4f676fe9ea364cf5f1bf5615522ef6acfd80e.fbx", - position: Vec3.multiply(Vec3.subtract(MyAvatar.position, center), ROOT_SCALE), - dimensions: { x: 30, y: 100, z: 100 }, - }); - rootObject.addChild(avatarArrowEntity); - - this.isVisible = function() { - return visible; - } - - Controller.mousePressEvent.connect(mousePressEvent); - function mousePressEvent(event) { - // Entities.setZonesArePickable(false); - - var pickRay = Camera.computePickRay(event.x, event.y); - for (var i = 0; i < waypointEntities.length; ++i) { - var entity = waypointEntities[i]; - print("Checkit for hit", entity.__extra__.waypoint.name); - var result = raySphereIntersection(pickRay.origin, pickRay.direction, entity.worldPosition, 0.1);//entity.worldScale); - if (result) { - print("Pressed entity: ", entity.id); - print("Pressed waypoint: ", entity.__extra__.waypoint.name); - print("Teleporting..."); - MyAvatar.position = entity.__extra__.waypoint.position; - break; - } - } - // var result = Entities.findRayIntersection(pickRay, false); - // if (result.intersects) { - // var entity = entityManager.get(result.entityID); - // if (entity) { - // print("Pressed entity: ", entity.id); - // } - // if (entity && entity.__extra__.waypoint) { - // print("Pressed waypoint: ", entity.__extra__.waypoint.name); - // print("Teleporting..."); - // MyAvatar.position = entity.__extra__.waypoint.position; - // } - // } - - // Entities.setZonesArePickable(true); - }; - - var time = 0; - Script.update.connect(function(dt) { - time += dt; - // Update tracked entities - for (var i = 0; i < trackedEntities.length; ++i) { - entity = trackedEntities[i]; - var entityID = entity.__extra__.trackingEntityID; - var properties = Entities.getEntityProperties(entityID); - properties.position = Vec3.subtract(properties.position, center); - properties.position = Vec3.multiply(properties.position, ROOT_SCALE); - entity.position = properties.position; - } - - - var position = Vec3.subtract(MyAvatar.position, center) - position.y += 60 + (Math.sin(time) * 10); - position = Vec3.multiply(position, ROOT_SCALE); - avatarArrowEntity.position = position; - // Vec3.print("Position:", avatarArrowEntity.position); - - // rootObject.position = Vec3.sum(position, { x: 0, y: Math.sin(time) / 30, z: 0 }); - //var ROOT_OFFSET = Vec3.multiply(0.3, Quat.getFront(MyAvatar.orientation)); - //var ROOT_POSITION = Vec3.sum(MyAvatar.position, ROOT_OFFSET); - // position = ROOT_POSITION; - rootObject.position = ROOT_POSITION; - entityManager.update(); - - // Update waypoint highlights - var pickRay = Camera.computePickRay(event.x, event.y); - for (var i = 0; i < waypointEntities.length; ++i) { - var entity = waypointEntities[i]; - print("Checkit for hit", entity.__extra__.waypoint.name); - var result = raySphereIntersection(pickRay.origin, pickRay.direction, entity.worldPosition, 0.1);//entity.worldScale); - if (result) { - print("Pressed entity: ", entity.id); - print("Pressed waypoint: ", entity.__extra__.waypoint.name); - print("Teleporting..."); - MyAvatar.position = entity.__extra__.waypoint.position; - break; - } - } - }); - - function setVisible(newValue) { - if (visible != newValue) { - visible = newValue; - - if (visible) { - } else { - } - } - } - - this.show = function() { - setVisible(true); - } - - this.hide = function() { - setVisible(false); - } -}; - -var map = null; -map = Map(mapData); - -// On press key -Controller.keyPressEvent.connect(function(event) { - if (event.text == "m") { - if (!map) { - map = Map(mapData); - } - - map.show(); - print("MAP!"); - } -}); - - - - - -var mapData = { - config: { - // World dimensions that the minimap maps to - worldDimensions: { - x: 10.0, - y: 10.0, - z: 10.0, - }, - // The center of the map should map to this location in the center of the area - worldCenter: { - x: 5.0, - y: 5.0, - z: 5.0, - }, - // Map dimensions - mapDimensions: { - x: 10.0, - y: 10.0, - z: 10.0, - }, - - // Can this be automated? Tag entities that should be included? Store in UserData? - objects: [ - { - type: "Model", - modelURL: "https://hifi-public.s3.amazonaws.com/ozan/sets/huffman_set/huffman_set.fbx", - }, - ], - }, - waypoints: [ - { - name: "Forest's Edge", - position: { - }, - }, - ], -}; - - -// entityManager = new OverlayManager(); -// entityManager = new EntityManager(); -// -// var rootEntity = entityManager.addBare(); -// -// var time = 0; -// -// -// rootEntity.scale = 0.1; -// Script.include("sfData.js"); -// rootEntity.addChild(entity); -entityManager.update(); From 57a68edaa3befacdc76e83108164e55f8495141e Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 2 Nov 2015 14:18:39 -0800 Subject: [PATCH 03/10] resolve conflicts merging with huffman/baseball --- libraries/script-engine/src/ScriptEngine.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/script-engine/src/ScriptEngine.h b/libraries/script-engine/src/ScriptEngine.h index b0595cf3c6..8ec06dd89d 100644 --- a/libraries/script-engine/src/ScriptEngine.h +++ b/libraries/script-engine/src/ScriptEngine.h @@ -141,10 +141,10 @@ public: // NOTE - this is used by the TypedArray implemetation. we need to review this for thread safety ArrayBufferClass* getArrayBufferClass() { return _arrayBufferClass; } - + public slots: void callAnimationStateHandler(QScriptValue callback, AnimVariantMap parameters, QStringList names, bool useNames, AnimVariantResultHandler resultHandler); - + signals: void scriptLoaded(const QString& scriptFilename); void errorLoadingScript(const QString& scriptFilename); @@ -184,7 +184,7 @@ private: QObject* setupTimerWithInterval(const QScriptValue& function, int intervalMS, bool isSingleShot); void stopTimer(QTimer* timer); - + QString _fileNameString; Quat _quatLibrary; Vec3 _vec3Library; From f18d8f5fbbc3285d3487acc368189b3638d7f705 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Mon, 2 Nov 2015 15:13:07 -0800 Subject: [PATCH 04/10] Add shouldRenderLocally to bat.js and fix findEntities --- examples/baseball/bat.js | 2 ++ examples/baseball/pitching.js | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/baseball/bat.js b/examples/baseball/bat.js index 76e9a0a588..cc4629c089 100644 --- a/examples/baseball/bat.js +++ b/examples/baseball/bat.js @@ -5,11 +5,13 @@ this.startNearGrab = function() { print("Started near grab!"); pitchingMachine.start(); + MyAvatar.shouldRenderLocally = false; }; this.releaseGrab = function() { print("Stopped near grab!"); if (pitchingMachine) { pitchingMachine.stop(); } + MyAvatar.shouldRenderLocally = true; }; }); diff --git a/examples/baseball/pitching.js b/examples/baseball/pitching.js index 9d84cf7a27..9c5654d652 100644 --- a/examples/baseball/pitching.js +++ b/examples/baseball/pitching.js @@ -85,10 +85,12 @@ var PITCH_RATE = 5000; function findEntities(properties, searchRadius) { var entities = Entities.findEntities(MyAvatar.position, searchRadius); var matchedEntities = []; + var keys = Object.keys(properties); for (var i = 0; i < entities.length; ++i) { var match = true; + var candidateProperties = Entities.getEntityProperties(entities[i], keys); for (var key in properties) { - if (entities[key] != properties[key]) { + if (candidateProperties[key] != properties[key]) { // This isn't a match, move to next entity match = false; break; From d535bb6ed8272fdb01e7112272621668ea756be3 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Mon, 2 Nov 2015 15:28:18 -0800 Subject: [PATCH 05/10] Fix pitching machine not starting when enabled --- examples/baseball/pitching.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/baseball/pitching.js b/examples/baseball/pitching.js index 9c5654d652..47876aafdf 100644 --- a/examples/baseball/pitching.js +++ b/examples/baseball/pitching.js @@ -106,7 +106,7 @@ function findEntities(properties, searchRadius) { getPitchingMachine = function() { // Search for pitching machine - var entities = findEntities({ name: PITCHING_MACHINE_PROPERTIES.name }, 100); + var entities = findEntities({ name: PITCHING_MACHINE_PROPERTIES.name }, 1000); var pitchingMachineID = null; // Create if it doesn't exist @@ -183,6 +183,8 @@ PitchingMachine.prototype = { var self = this; Script.setTimeout(function() { self.pitchBall() }, 3000); } + } else if (this.enabled) { + this.pitchBall(); } } }; From bb0ce67f6b103b671e1598f6cbaf1291c10e21ac Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Mon, 2 Nov 2015 15:34:42 -0800 Subject: [PATCH 06/10] Update bat.js to lazy load the pitching machine --- examples/baseball/bat.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/baseball/bat.js b/examples/baseball/bat.js index cc4629c089..c8857095d8 100644 --- a/examples/baseball/bat.js +++ b/examples/baseball/bat.js @@ -1,8 +1,11 @@ (function() { Script.include("pitching.js"); - var pitchingMachine = getPitchingMachine(); - Script.update.connect(function(dt) { pitchingMachine.update(dt); }); + var pitchingMachine = null; this.startNearGrab = function() { + if (!pitchingMachine) { + getPitchingMachine(); + Script.update.connect(function(dt) { pitchingMachine.update(dt); }); + } print("Started near grab!"); pitchingMachine.start(); MyAvatar.shouldRenderLocally = false; From 14cdc5801e9f76e57e641ec60a7faf6289f4d265 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Mon, 2 Nov 2015 15:59:55 -0800 Subject: [PATCH 07/10] Fix updating of distance/high score --- examples/baseball/pitching.js | 53 ++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/examples/baseball/pitching.js b/examples/baseball/pitching.js index 47876aafdf..8710a33cce 100644 --- a/examples/baseball/pitching.js +++ b/examples/baseball/pitching.js @@ -2,9 +2,35 @@ print("Loading pitching"); //Script.include("../libraries/line.js"); Script.include("https://raw.githubusercontent.com/huffman/hifi/line-js/examples/libraries/line.js"); +// Return all entities with properties `properties` within radius `searchRadius` +function findEntities(properties, searchRadius) { + var entities = Entities.findEntities(MyAvatar.position, searchRadius); + var matchedEntities = []; + var keys = Object.keys(properties); + for (var i = 0; i < entities.length; ++i) { + var match = true; + var candidateProperties = Entities.getEntityProperties(entities[i], keys); + for (var key in properties) { + if (candidateProperties[key] != properties[key]) { + // This isn't a match, move to next entity + match = false; + break; + } + } + if (match) { + matchedEntities.push(entities[i]); + } + } + + return matchedEntities; +} + // These are hard-coded to the relevant entity IDs on the sport server -var DISTANCE_BILLBOARD_ENTITY_ID = "{faa88b15-5b85-408c-ae07-a31e5a5ca791}"; -var HIGH_SCORE_BILLBOARD_ENTITY_ID = "{5fe0daf5-3fc5-43e3-a4eb-81a8e840a52b}"; +var DISTANCE_BILLBOARD_NAME = "CurrentScore"; +var HIGH_SCORE_BILLBOARD_NAME = "HighScore"; +var DISTANCE_BILLBOARD_ENTITY_ID = findEntities({name: DISTANCE_BILLBOARD_NAME }, 1000)[0]; +var HIGH_SCORE_BILLBOARD_ENTITY_ID = findEntities({name: HIGH_SCORE_BILLBOARD_NAME }, 1000)[0]; +print("Distance: ", DISTANCE_BILLBOARD_ENTITY_ID) var METERS_TO_FEET = 3.28084; @@ -81,29 +107,6 @@ var DISTANCE_FROM_PLATE = PITCHING_MACHINE_PROPERTIES.position.z; var PITCH_RATE = 5000; -// Return all entities with properties `properties` within radius `searchRadius` -function findEntities(properties, searchRadius) { - var entities = Entities.findEntities(MyAvatar.position, searchRadius); - var matchedEntities = []; - var keys = Object.keys(properties); - for (var i = 0; i < entities.length; ++i) { - var match = true; - var candidateProperties = Entities.getEntityProperties(entities[i], keys); - for (var key in properties) { - if (candidateProperties[key] != properties[key]) { - // This isn't a match, move to next entity - match = false; - break; - } - } - if (match) { - matchedEntities.push(entities[i]); - } - } - - return matchedEntities; -} - getPitchingMachine = function() { // Search for pitching machine var entities = findEntities({ name: PITCHING_MACHINE_PROPERTIES.name }, 1000); From e8b550be9fb3c84b160624a74ad29c4979368bde Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Mon, 2 Nov 2015 16:09:51 -0800 Subject: [PATCH 08/10] Clean up foul/strike in pitching.js --- examples/baseball/pitching.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/baseball/pitching.js b/examples/baseball/pitching.js index 8710a33cce..647ee834c2 100644 --- a/examples/baseball/pitching.js +++ b/examples/baseball/pitching.js @@ -133,7 +133,6 @@ function PitchingMachine(pitchingMachineID) { PitchingMachine.prototype = { pitchBall: function() { cleanupTrail(); - updateBillboard(""); if (!this.enabled) { return; @@ -171,9 +170,17 @@ PitchingMachine.prototype = { }, start: function() { print("Starting Pitching Machine"); + if (this.enabled) { + print("Already enabled") + return; + } this.enabled = true; + this.pitchBall(); }, stop: function() { + if (!this.enabled) { + return; + } print("Stopping Pitching Machine"); this.enabled = false; }, @@ -186,8 +193,6 @@ PitchingMachine.prototype = { var self = this; Script.setTimeout(function() { self.pitchBall() }, 3000); } - } else if (this.enabled) { - this.pitchBall(); } } }; @@ -312,7 +317,6 @@ function ObjectTrail(entityID, startPosition) { trailInterval = Script.setInterval(function() { var properties = Entities.getEntityProperties(entityID, ['position']); if (Vec3.distance(properties.position, lastPosition)) { - Vec3.print("Adding trail", properties.position); var strokeWidth = Math.log(1 + trail.size) * 0.05; trail.enqueuePoint(properties.position, strokeWidth); lastPosition = properties.position; @@ -505,7 +509,7 @@ Baseball.prototype = { }); }, 500); if (foul) { - updateBillboard("FOUL!"); + updateBillboard("FOUL"); print("FOUL ", yaw) this.state = BASEBALL_STATE.FOUL; playRandomSound(AUDIO.foul, { @@ -526,7 +530,7 @@ Baseball.prototype = { } else if (name == "backstop") { if (this.state == BASEBALL_STATE.PITCHING) { this.state = BASEBALL_STATE.STRIKE; - updateBillboard("STRIKE!"); + updateBillboard("STRIKE"); print("STRIKE"); playRandomSound(AUDIO.strike, { position: myPosition, From c63fc8557cda1fa992ca12ac9134ce355f6f790c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Brisset?= Date: Tue, 3 Nov 2015 10:11:25 -0800 Subject: [PATCH 09/10] More baseball test code --- interface/src/avatar/AvatarActionHold.cpp | 53 ++++++++++--------- .../input-plugins/ViveControllerManager.cpp | 32 ++++++----- 2 files changed, 46 insertions(+), 39 deletions(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 5bec513fd4..b6e3ea408f 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -52,32 +52,35 @@ void AvatarActionHold::updateActionWorker(float deltaTimeStep) { glm::vec3 offset; glm::vec3 palmPosition; glm::quat palmRotation; - - const auto& plugins = PluginManager::getInstance()->getInputPlugins(); - auto it = std::find_if(std::begin(plugins), std::end(plugins), [](const InputPluginPointer& plugin) { - return plugin->getName() == ViveControllerManager::NAME; - }); - - if (it != std::end(plugins)) { - const auto& vive = it->dynamicCast(); - auto index = (_hand == "right") ? 0 : 1; + +#ifdef Q_OS_WIN + const auto& plugins = PluginManager::getInstance()->getInputPlugins(); + auto it = std::find_if(std::begin(plugins), std::end(plugins), [](const InputPluginPointer& plugin) { + return plugin->getName() == ViveControllerManager::NAME; + }); + + if (it != std::end(plugins)) { + const auto& vive = it->dynamicCast(); + auto index = (_hand == "left") ? 0 : 1; auto userInputMapper = DependencyManager::get(); - auto translation = extractTranslation(userInputMapper->getSensorToWorldMat()); - auto rotation = glm::quat_cast(userInputMapper->getSensorToWorldMat()); - - - const glm::quat quarterX = glm::angleAxis(PI / 2.0f, glm::vec3(1.0f, 0.0f, 0.0f)); - const glm::quat yFlip = glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f)); - palmPosition = translation + rotation * vive->getPosition(index); - palmRotation = rotation * vive->getRotation(index) * yFlip * quarterX; - } else if (_hand == "right") { - palmPosition = holdingAvatar->getRightPalmPosition(); - palmRotation = holdingAvatar->getRightPalmRotation(); - } else { - palmPosition = holdingAvatar->getLeftPalmPosition(); - palmRotation = holdingAvatar->getLeftPalmRotation(); - } - + auto translation = extractTranslation(userInputMapper->getSensorToWorldMat()); + auto rotation = glm::quat_cast(userInputMapper->getSensorToWorldMat()); + + + const glm::quat quarterX = glm::angleAxis(PI / 2.0f, glm::vec3(1.0f, 0.0f, 0.0f)); + const glm::quat yFlip = glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f)); + palmPosition = translation + rotation * vive->getPosition(index); + palmRotation = rotation * vive->getRotation(index) * yFlip * quarterX * glm::angleAxis(PI, glm::vec3(1.0f, 0.0f, 0.0f)) * glm::angleAxis(PI_OVER_TWO, glm::vec3(0.0f, 0.0f, 1.0f)); + } else +#endif + if (_hand == "right") { + palmPosition = holdingAvatar->getRightPalmPosition(); + palmRotation = holdingAvatar->getRightPalmRotation(); + } else { + palmPosition = holdingAvatar->getLeftPalmPosition(); + palmRotation = holdingAvatar->getLeftPalmRotation(); + } + rotation = palmRotation * _relativeRotation; offset = rotation * _relativePosition; position = palmPosition + offset; diff --git a/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp b/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp index b8755ea2e3..f5863c6ed0 100644 --- a/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp +++ b/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp @@ -278,12 +278,18 @@ void ViveControllerManager::update(float deltaTime, bool jointsCaptured) { //qDebug() << "Trackpad: " << controllerState.rAxis[0].x << " " << controllerState.rAxis[0].y; //qDebug() << "Trigger: " << controllerState.rAxis[1].x << " " << controllerState.rAxis[1].y; for (uint32_t i = 0; i < vr::k_EButton_Max; ++i) { - auto mask = vr::ButtonMaskFromId((vr::EVRButtonId)i); - bool pressed = 0 != (controllerState.ulButtonPressed & mask); + auto mask = vr::ButtonMaskFromId((vr::EVRButtonId)i); + bool pressed = 0 != (controllerState.ulButtonPressed & mask); handleButtonEvent(i, pressed, left); } - for (uint32_t i = 0; i < vr::k_unControllerStateAxisCount; i++) { - handleAxisEvent(i, controllerState.rAxis[i].x, controllerState.rAxis[i].y, left); + for (uint32_t i = 0; i < vr::k_unControllerStateAxisCount; i++) { + auto mask = vr::ButtonMaskFromId((vr::EVRButtonId)(i + vr::k_EButton_Axis0)); + bool pressed = 0 != (controllerState.ulButtonPressed & mask); + if (pressed || true) { + handleAxisEvent(i, controllerState.rAxis[i].x, controllerState.rAxis[i].y, left); + } else { + handleAxisEvent(i, 0.0f, 0.0f, left); + } } } } @@ -412,16 +418,14 @@ void ViveControllerManager::handlePoseEvent(const mat4& mat, bool left) { const glm::quat signedQuaterZ = glm::angleAxis(sign * PI / 2.0f, glm::vec3(0.0f, 0.0f, 1.0f)); const glm::quat eighthX = glm::angleAxis(PI / 4.0f, glm::vec3(1.0f, 0.0f, 0.0f)); - - const glm::quat rotationOffset = glm::inverse(signedQuaterZ * eighthX) * yFlip * quarterX; - const glm::vec3 translationOffset = glm::vec3(sign * CONTROLLER_LENGTH_OFFSET / 2.0f, - CONTROLLER_LENGTH_OFFSET / 2.0f, - 2.0f * CONTROLLER_LENGTH_OFFSET); - - position += rotation * translationOffset; - rotation = rotation * rotationOffset; - //{quat, x = 0.653281, y = -0.270598, z = 0.653281, w = 0.270598}{vec3, x = 0.0381, y = -0.0381, z = -0.1524} - + const glm::quat rotationOffset = glm::inverse(signedQuaterZ * eighthX) * yFlip * quarterX; + const glm::vec3 translationOffset = glm::vec3(sign * CONTROLLER_LENGTH_OFFSET / 2.0f, + CONTROLLER_LENGTH_OFFSET / 2.0f, + 2.0f * CONTROLLER_LENGTH_OFFSET); + + position += rotation * translationOffset; + rotation = rotation * rotationOffset; + _poseStateMap[left ? controller::LEFT_HAND : controller::RIGHT_HAND] = controller::Pose(position, rotation); } From 0508091130fff5b0c155d75989cfee49a22d2219 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 3 Nov 2015 11:37:57 -0800 Subject: [PATCH 10/10] Rebase fixes --- interface/src/avatar/AvatarActionHold.cpp | 23 ++++++++------- .../input-plugins/ViveControllerManager.cpp | 29 ++++++++++--------- .../src/input-plugins/ViveControllerManager.h | 8 +++-- libraries/script-engine/src/ScriptEngine.cpp | 8 ++--- libraries/script-engine/src/ScriptEngine.h | 2 +- 5 files changed, 38 insertions(+), 32 deletions(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index b6e3ea408f..767c60defe 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -37,17 +37,18 @@ AvatarActionHold::~AvatarActionHold() { } #include #include +#include void AvatarActionHold::updateActionWorker(float deltaTimeStep) { bool gotLock = false; glm::quat rotation; glm::vec3 position; std::shared_ptr holdingAvatar = nullptr; - + gotLock = withTryReadLock([&]{ QSharedPointer avatarManager = DependencyManager::get(); AvatarSharedPointer holdingAvatarData = avatarManager->getAvatarBySessionID(_holderID); holdingAvatar = std::static_pointer_cast(holdingAvatarData); - + if (holdingAvatar) { glm::vec3 offset; glm::vec3 palmPosition; @@ -70,23 +71,23 @@ void AvatarActionHold::updateActionWorker(float deltaTimeStep) { const glm::quat quarterX = glm::angleAxis(PI / 2.0f, glm::vec3(1.0f, 0.0f, 0.0f)); const glm::quat yFlip = glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f)); palmPosition = translation + rotation * vive->getPosition(index); - palmRotation = rotation * vive->getRotation(index) * yFlip * quarterX * glm::angleAxis(PI, glm::vec3(1.0f, 0.0f, 0.0f)) * glm::angleAxis(PI_OVER_TWO, glm::vec3(0.0f, 0.0f, 1.0f)); + palmRotation = rotation * vive->getRotation(index) * yFlip * quarterX * glm::angleAxis(PI, glm::vec3(1.0f, 0.0f, 0.0f)) * glm::angleAxis(PI_OVER_TWO, glm::vec3(0.0f, 0.0f, 1.0f)); } else #endif - if (_hand == "right") { - palmPosition = holdingAvatar->getRightPalmPosition(); - palmRotation = holdingAvatar->getRightPalmRotation(); - } else { - palmPosition = holdingAvatar->getLeftPalmPosition(); - palmRotation = holdingAvatar->getLeftPalmRotation(); - } + if (_hand == "right") { + palmPosition = holdingAvatar->getRightPalmPosition(); + palmRotation = holdingAvatar->getRightPalmRotation(); + } else { + palmPosition = holdingAvatar->getLeftPalmPosition(); + palmRotation = holdingAvatar->getLeftPalmRotation(); + } rotation = palmRotation * _relativeRotation; offset = rotation * _relativePosition; position = palmPosition + offset; } }); - + if (holdingAvatar) { if (gotLock) { gotLock = withTryWriteLock([&]{ diff --git a/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp b/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp index f5863c6ed0..b083d06a2a 100644 --- a/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp +++ b/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp @@ -223,6 +223,7 @@ void ViveControllerManager::renderHand(const controller::Pose& pose, gpu::Batch& batch.drawIndexed(gpu::TRIANGLES, mesh->getNumIndices(), 0); } +#ifdef Q_OS_WIN glm::vec3 ViveControllerManager::getPosition(int hand) const { const mat4& mat = _trackedDevicePoseMat4[hand ? 3 : 4]; return extractTranslation(mat); @@ -231,6 +232,7 @@ glm::quat ViveControllerManager::getRotation(int hand) const { const mat4& mat = _trackedDevicePoseMat4[hand ? 3 : 4]; return glm::quat_cast(mat); } +#endif void ViveControllerManager::update(float deltaTime, bool jointsCaptured) { #ifdef Q_OS_WIN @@ -278,22 +280,22 @@ void ViveControllerManager::update(float deltaTime, bool jointsCaptured) { //qDebug() << "Trackpad: " << controllerState.rAxis[0].x << " " << controllerState.rAxis[0].y; //qDebug() << "Trigger: " << controllerState.rAxis[1].x << " " << controllerState.rAxis[1].y; for (uint32_t i = 0; i < vr::k_EButton_Max; ++i) { - auto mask = vr::ButtonMaskFromId((vr::EVRButtonId)i); - bool pressed = 0 != (controllerState.ulButtonPressed & mask); + auto mask = vr::ButtonMaskFromId((vr::EVRButtonId)i); + bool pressed = 0 != (controllerState.ulButtonPressed & mask); handleButtonEvent(i, pressed, left); } - for (uint32_t i = 0; i < vr::k_unControllerStateAxisCount; i++) { - auto mask = vr::ButtonMaskFromId((vr::EVRButtonId)(i + vr::k_EButton_Axis0)); - bool pressed = 0 != (controllerState.ulButtonPressed & mask); - if (pressed || true) { - handleAxisEvent(i, controllerState.rAxis[i].x, controllerState.rAxis[i].y, left); - } else { - handleAxisEvent(i, 0.0f, 0.0f, left); - } + for (uint32_t i = 0; i < vr::k_unControllerStateAxisCount; i++) { + auto mask = vr::ButtonMaskFromId((vr::EVRButtonId)(i + vr::k_EButton_Axis0)); + bool pressed = 0 != (controllerState.ulButtonPressed & mask); + if (pressed || true) { + handleAxisEvent(i, controllerState.rAxis[i].x, controllerState.rAxis[i].y, left); + } else { + handleAxisEvent(i, 0.0f, 0.0f, left); + } } } } - + auto userInputMapper = DependencyManager::get(); if (numTrackedControllers == 0) { @@ -412,12 +414,13 @@ void ViveControllerManager::handlePoseEvent(const mat4& mat, bool left) { // Q = (deltaQ * inverse(deltaQForAlignedHand)) * (yFlip * quarterTurnAboutX) float sign = left ? -1.0f : 1.0f; - + const glm::quat quarterX = glm::angleAxis(PI / 2.0f, glm::vec3(1.0f, 0.0f, 0.0f)); const glm::quat yFlip = glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f)); - const glm::quat signedQuaterZ = glm::angleAxis(sign * PI / 2.0f, glm::vec3(0.0f, 0.0f, 1.0f)); + const glm::quat signedQuaterZ = glm::angleAxis(sign * PI / 2.0f, glm::vec3(0.0f, 0.0f, 1.0f)); const glm::quat eighthX = glm::angleAxis(PI / 4.0f, glm::vec3(1.0f, 0.0f, 0.0f)); + const glm::quat rotationOffset = glm::inverse(signedQuaterZ * eighthX) * yFlip * quarterX; const glm::vec3 translationOffset = glm::vec3(sign * CONTROLLER_LENGTH_OFFSET / 2.0f, CONTROLLER_LENGTH_OFFSET / 2.0f, diff --git a/libraries/input-plugins/src/input-plugins/ViveControllerManager.h b/libraries/input-plugins/src/input-plugins/ViveControllerManager.h index ad0474073c..eee6083102 100644 --- a/libraries/input-plugins/src/input-plugins/ViveControllerManager.h +++ b/libraries/input-plugins/src/input-plugins/ViveControllerManager.h @@ -27,8 +27,8 @@ class ViveControllerManager : public InputPlugin, public controller::InputDevice { Q_OBJECT public: - static const QString NAME; - + static const QString NAME; + ViveControllerManager(); // Plugin functions @@ -52,9 +52,11 @@ public: void setRenderControllers(bool renderControllers) { _renderControllers = renderControllers; } - int getNumDevices() const; + +#ifdef Q_OS_WIN glm::vec3 getPosition(int device) const; glm::quat getRotation(int device) const; +#endif private: void renderHand(const controller::Pose& pose, gpu::Batch& batch, int sign); diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index 4e53f6e60b..0173009995 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -367,10 +367,10 @@ void ScriptEngine::init() { registerGlobalObject("AnimationCache", DependencyManager::get().data()); qScriptRegisterMetaType(this, animVarMapToScriptValue, animVarMapFromScriptValue); qScriptRegisterMetaType(this, resultHandlerToScriptValue, resultHandlerFromScriptValue); - + // constants globalObject().setProperty("TREE_SCALE", newVariant(QVariant(TREE_SCALE))); - + auto scriptingInterface = DependencyManager::get(); registerGlobalObject("Controller", scriptingInterface.data()); UserInputMapper::registerControllerTypes(this); @@ -706,7 +706,7 @@ void ScriptEngine::run() { } } lastUpdate = now; - + // Debug and clear exceptions hadUncaughtExceptions(*this, _fileNameString); } @@ -1263,4 +1263,4 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS entityScript.property(methodName).call(entityScript, args); } } -} \ No newline at end of file +} diff --git a/libraries/script-engine/src/ScriptEngine.h b/libraries/script-engine/src/ScriptEngine.h index 8ec06dd89d..1ae2aa05ca 100644 --- a/libraries/script-engine/src/ScriptEngine.h +++ b/libraries/script-engine/src/ScriptEngine.h @@ -205,4 +205,4 @@ private: }; -#endif // hifi_ScriptEngine_h \ No newline at end of file +#endif // hifi_ScriptEngine_h