From 9ea53f8070b11aa37a0866994af4fc4e9c5d2980 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 4 Jun 2014 11:38:46 -0700 Subject: [PATCH 1/9] some testing with editModels --- examples/editModels.js | 76 ++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 29 deletions(-) diff --git a/examples/editModels.js b/examples/editModels.js index 24ab7da1a1..c2072a46cd 100644 --- a/examples/editModels.js +++ b/examples/editModels.js @@ -152,6 +152,7 @@ function controller(wichSide) { this.jointsIntersectingFromStart.push(i); } } + this.showLaser(false); } } @@ -196,6 +197,7 @@ function controller(wichSide) { this.grabbing = false; this.modelID.isKnownID = false; this.jointsIntersectingFromStart = []; + this.showLaser(true); } this.checkTrigger = function () { @@ -258,41 +260,43 @@ function controller(wichSide) { Overlays.editOverlay(this.laser, { position: startPosition, - end: endPosition, - visible: true + end: endPosition }); Overlays.editOverlay(this.ball, { - position: endPosition, - visible: true + position: endPosition }); Overlays.editOverlay(this.leftRight, { position: Vec3.sum(endPosition, Vec3.multiply(this.right, 2 * this.guideScale)), - end: Vec3.sum(endPosition, Vec3.multiply(this.right, -2 * this.guideScale)), - visible: true + end: Vec3.sum(endPosition, Vec3.multiply(this.right, -2 * this.guideScale)) }); Overlays.editOverlay(this.topDown, {position: Vec3.sum(endPosition, Vec3.multiply(this.up, 2 * this.guideScale)), - end: Vec3.sum(endPosition, Vec3.multiply(this.up, -2 * this.guideScale)), - visible: true + end: Vec3.sum(endPosition, Vec3.multiply(this.up, -2 * this.guideScale)) }); + this.showLaser(!this.grabbing); } - this.hideLaser = function() { - Overlays.editOverlay(this.laser, { visible: false }); - Overlays.editOverlay(this.ball, { visible: false }); - Overlays.editOverlay(this.leftRight, { visible: false }); - Overlays.editOverlay(this.topDown, { visible: false }); + this.showLaser = function(show) { + Overlays.editOverlay(this.laser, { visible: show }); + Overlays.editOverlay(this.ball, { visible: show }); + Overlays.editOverlay(this.leftRight, { visible: show }); + Overlays.editOverlay(this.topDown, { visible: show }); } this.moveModel = function () { if (this.grabbing) { - var newPosition = Vec3.sum(this.palmPosition, - Vec3.multiply(this.front, this.x)); - newPosition = Vec3.sum(newPosition, - Vec3.multiply(this.up, this.y)); - newPosition = Vec3.sum(newPosition, - Vec3.multiply(this.right, this.z)); + var forward = Vec3.multiplyQbyV(MyAvatar.orientation, { x: 0, y: 0, z: -1 }); + var d = Vec3.dot(forward, MyAvatar.position); + + var factor1 = Vec3.dot(forward, this.palmPosition) - d; + var factor2 = Vec3.dot(forward, this.oldModelPosition) - d; + var vector = Vec3.subtract(this.palmPosition, this.oldPalmPosition); + + var newPosition = Vec3.sum(this.oldModelPosition, + Vec3.multiply(vector, + factor2 / factor1)); + var newRotation = Quat.multiply(this.rotation, Quat.inverse(this.oldRotation)); @@ -457,20 +461,34 @@ function moveModels() { var newPosition = Vec3.sum(middle, Vec3.multiply(Vec3.subtract(leftController.oldModelPosition, oldMiddle), ratio)); - //Vec3.print("Ratio : " + ratio + " New position: ", newPosition); - var rotation = Quat.multiply(leftController.rotation, - Quat.inverse(leftController.oldRotation)); + + + var u = Vec3.normalize(Vec3.subtract(rightController.oldPalmPosition, leftController.oldPalmPosition)); + var v = Vec3.normalize(Vec3.subtract(rightController.palmPosition, leftController.palmPosition)); + + var cos_theta = Vec3.dot(Vec3.normalize(u), Vec3.normalize(v)); + var angle = Math.acos(cos_theta); + var w = Vec3.normalize(Vec3.cross(u, v)); + + + var rotation = Quat.angleAxis(angle, w); + + rotation = Quat.multiply(rotation, leftController.oldModelRotation); Models.editModel(leftController.modelID, { - position: newPosition, - //modelRotation: rotation, - radius: leftController.oldModelRadius * ratio + //position: newPosition, + modelRotation: rotation, + //radius: leftController.oldModelRadius * ratio }); - leftController.oldModelPosition = newPosition; + //leftController.oldModelPosition = newPosition; leftController.oldModelRotation = rotation; - leftController.oldModelRadius *= ratio; + //leftController.oldModelRadius *= ratio; + + //rightController.oldModelPosition = newPosition; + rightController.oldModelRotation = rotation; + //rightController.oldModelRadius *= ratio; return; } @@ -498,8 +516,8 @@ function checkController(deltaTime) { if (hydraConnected) { hydraConnected = false; - leftController.hideLaser(); - rightController.hideLaser(); + leftController.showLaser(false); + rightController.showLaser(false); } } From 15863198d1f8a0792361e8af317080aa5cc0f75c Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 4 Jun 2014 13:52:31 -0700 Subject: [PATCH 2/9] change editModels icon --- examples/editModels.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/editModels.js b/examples/editModels.js index c2072a46cd..47da9b0c0a 100644 --- a/examples/editModels.js +++ b/examples/editModels.js @@ -528,7 +528,7 @@ function initToolBar() { toolBar = new ToolBar(0, 0, ToolBar.VERTICAL); // New Model newModel = toolBar.addTool({ - imageURL: toolIconUrl + "voxel-tool.svg", + imageURL: toolIconUrl + "add-model-tool.svg", subImage: { x: 0, y: Tool.IMAGE_WIDTH, width: Tool.IMAGE_WIDTH, height: Tool.IMAGE_HEIGHT }, width: toolWidth, height: toolHeight, visible: true, From 79092e3e81a7605c49d38050521c133c92595d88 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 4 Jun 2014 14:18:28 -0700 Subject: [PATCH 3/9] editModels now has 2 modes --- examples/editModels.js | 150 ++++++++++++++++++++++++++++------------- 1 file changed, 102 insertions(+), 48 deletions(-) diff --git a/examples/editModels.js b/examples/editModels.js index 47da9b0c0a..24360c526d 100644 --- a/examples/editModels.js +++ b/examples/editModels.js @@ -42,6 +42,8 @@ var toolBar; var jointList = MyAvatar.getJointNames(); +var mode = 0; + function isLocked(properties) { // special case to lock the ground plane model in hq. if (location.hostname == "hq.highfidelity.io" && @@ -57,6 +59,7 @@ function controller(wichSide) { this.palm = 2 * wichSide; this.tip = 2 * wichSide + 1; this.trigger = wichSide; + this.bumper = 6 * wichSide + 5; this.oldPalmPosition = Controller.getSpatialControlPosition(this.palm); this.palmPosition = Controller.getSpatialControlPosition(this.palm); @@ -77,6 +80,7 @@ function controller(wichSide) { this.rotation = this.oldRotation; this.triggerValue = Controller.getTriggerValue(this.trigger); + this.bumperValue = Controller.isButtonPressed(this.bumper); this.pressed = false; // is trigger pressed this.pressing = false; // is trigger being pressed (is pressed now but wasn't previously) @@ -274,7 +278,7 @@ function controller(wichSide) { Overlays.editOverlay(this.topDown, {position: Vec3.sum(endPosition, Vec3.multiply(this.up, 2 * this.guideScale)), end: Vec3.sum(endPosition, Vec3.multiply(this.up, -2 * this.guideScale)) }); - this.showLaser(!this.grabbing); + this.showLaser(!this.grabbing || mode == 0); } this.showLaser = function(show) { @@ -286,20 +290,44 @@ function controller(wichSide) { this.moveModel = function () { if (this.grabbing) { - var forward = Vec3.multiplyQbyV(MyAvatar.orientation, { x: 0, y: 0, z: -1 }); - var d = Vec3.dot(forward, MyAvatar.position); + var newPosition; + var newRotation; - var factor1 = Vec3.dot(forward, this.palmPosition) - d; - var factor2 = Vec3.dot(forward, this.oldModelPosition) - d; - var vector = Vec3.subtract(this.palmPosition, this.oldPalmPosition); - - var newPosition = Vec3.sum(this.oldModelPosition, - Vec3.multiply(vector, - factor2 / factor1)); + switch (mode) { + case 0: + newPosition = Vec3.sum(this.palmPosition, + Vec3.multiply(this.front, this.x)); + newPosition = Vec3.sum(newPosition, + Vec3.multiply(this.up, this.y)); + newPosition = Vec3.sum(newPosition, + Vec3.multiply(this.right, this.z)); + break; + case 1: + var forward = Vec3.multiplyQbyV(MyAvatar.orientation, { x: 0, y: 0, z: -1 }); + var d = Vec3.dot(forward, MyAvatar.position); + + var factor1 = Vec3.dot(forward, this.palmPosition) - d; + var factor2 = Vec3.dot(forward, this.oldModelPosition) - d; + var vector = Vec3.subtract(this.palmPosition, this.oldPalmPosition); + + if (factor2 < 0) { + factor2 = 0; + } + if (factor1 <= 0) { + factor1 = 1; + factor2 = 1; + } + + newPosition = Vec3.sum(this.oldModelPosition, + Vec3.multiply(vector, + factor2 / factor1)); + break; + } - var newRotation = Quat.multiply(this.rotation, - Quat.inverse(this.oldRotation)); + + newRotation = Quat.multiply(this.rotation, + Quat.inverse(this.oldRotation)); newRotation = Quat.multiply(newRotation, this.oldModelRotation); @@ -345,6 +373,21 @@ function controller(wichSide) { this.triggerValue = Controller.getTriggerValue(this.trigger); + var bumperValue = Controller.isButtonPressed(this.bumper); + if (bumperValue && !this.bumperValue) { + if (mode == 0) { + mode = 1; + Overlays.editOverlay(leftController.laser, { color: { red: 0, green: 0, blue: 255 } }); + Overlays.editOverlay(rightController.laser, { color: { red: 0, green: 0, blue: 255 } }); + } else { + mode = 0; + Overlays.editOverlay(leftController.laser, { color: { red: 255, green: 0, blue: 0 } }); + Overlays.editOverlay(rightController.laser, { color: { red: 255, green: 0, blue: 0 } }); + } + } + this.bumperValue = bumperValue; + + this.checkTrigger(); this.moveLaser(); @@ -443,52 +486,63 @@ var rightController = new controller(RIGHT); function moveModels() { if (leftController.grabbing && rightController.grabbing && rightController.modelID.id == leftController.modelID.id) { - //print("Both controllers"); - var oldLeftPoint = Vec3.sum(leftController.oldPalmPosition, Vec3.multiply(leftController.oldFront, leftController.x)); - var oldRightPoint = Vec3.sum(rightController.oldPalmPosition, Vec3.multiply(rightController.oldFront, rightController.x)); - - var oldMiddle = Vec3.multiply(Vec3.sum(oldLeftPoint, oldRightPoint), 0.5); - var oldLength = Vec3.length(Vec3.subtract(oldLeftPoint, oldRightPoint)); + var newPosition = this.oldModelPosition; + var rotation = this.oldModelRotation; + var ratio = 1; - var leftPoint = Vec3.sum(leftController.palmPosition, Vec3.multiply(leftController.front, leftController.x)); - var rightPoint = Vec3.sum(rightController.palmPosition, Vec3.multiply(rightController.front, rightController.x)); - - var middle = Vec3.multiply(Vec3.sum(leftPoint, rightPoint), 0.5); - var length = Vec3.length(Vec3.subtract(leftPoint, rightPoint)); - - var ratio = length / oldLength; - - var newPosition = Vec3.sum(middle, - Vec3.multiply(Vec3.subtract(leftController.oldModelPosition, oldMiddle), ratio)); - - - var u = Vec3.normalize(Vec3.subtract(rightController.oldPalmPosition, leftController.oldPalmPosition)); - var v = Vec3.normalize(Vec3.subtract(rightController.palmPosition, leftController.palmPosition)); - - var cos_theta = Vec3.dot(Vec3.normalize(u), Vec3.normalize(v)); - var angle = Math.acos(cos_theta); - var w = Vec3.normalize(Vec3.cross(u, v)); - - - var rotation = Quat.angleAxis(angle, w); - - - rotation = Quat.multiply(rotation, leftController.oldModelRotation); + switch (mode) { + case 0: + var oldLeftPoint = Vec3.sum(leftController.oldPalmPosition, Vec3.multiply(leftController.oldFront, leftController.x)); + var oldRightPoint = Vec3.sum(rightController.oldPalmPosition, Vec3.multiply(rightController.oldFront, rightController.x)); + + var oldMiddle = Vec3.multiply(Vec3.sum(oldLeftPoint, oldRightPoint), 0.5); + var oldLength = Vec3.length(Vec3.subtract(oldLeftPoint, oldRightPoint)); + + + var leftPoint = Vec3.sum(leftController.palmPosition, Vec3.multiply(leftController.front, leftController.x)); + var rightPoint = Vec3.sum(rightController.palmPosition, Vec3.multiply(rightController.front, rightController.x)); + + var middle = Vec3.multiply(Vec3.sum(leftPoint, rightPoint), 0.5); + var length = Vec3.length(Vec3.subtract(leftPoint, rightPoint)); + + + ratio = length / oldLength; + newPosition = Vec3.sum(middle, + Vec3.multiply(Vec3.subtract(leftController.oldModelPosition, oldMiddle), ratio)); + break; + case 1: + var u = Vec3.normalize(Vec3.subtract(rightController.oldPalmPosition, leftController.oldPalmPosition)); + var v = Vec3.normalize(Vec3.subtract(rightController.palmPosition, leftController.palmPosition)); + + var cos_theta = Vec3.dot(u, v); + if (cos_theta > 1) { + cos_theta = 1; + } + var angle = Math.acos(cos_theta) / Math.PI * 180; + if (angle < 0.1) { + return; + + } + var w = Vec3.normalize(Vec3.cross(u, v)); + + rotation = Quat.multiply(Quat.angleAxis(angle, w), leftController.oldModelRotation); + break; + } Models.editModel(leftController.modelID, { - //position: newPosition, + position: newPosition, modelRotation: rotation, - //radius: leftController.oldModelRadius * ratio + radius: leftController.oldModelRadius * ratio }); - //leftController.oldModelPosition = newPosition; + leftController.oldModelPosition = newPosition; leftController.oldModelRotation = rotation; - //leftController.oldModelRadius *= ratio; + leftController.oldModelRadius *= ratio; - //rightController.oldModelPosition = newPosition; + rightController.oldModelPosition = newPosition; rightController.oldModelRotation = rotation; - //rightController.oldModelRadius *= ratio; + rightController.oldModelRadius *= ratio; return; } From 145a159233604285fb2f180e9111fd677b601e1b Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 5 Jun 2014 15:45:25 -0700 Subject: [PATCH 4/9] Slight glow on laser intersection --- examples/editModels.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/examples/editModels.js b/examples/editModels.js index 24360c526d..8e2f6b4911 100644 --- a/examples/editModels.js +++ b/examples/editModels.js @@ -252,6 +252,7 @@ function controller(wichSide) { return { valid: false }; } + this.glowedIntersectingModel = { isKnownID: false }; this.moveLaser = function () { // the overlays here are anchored to the avatar, which means they are specified in the avatar's local frame @@ -279,6 +280,22 @@ function controller(wichSide) { end: Vec3.sum(endPosition, Vec3.multiply(this.up, -2 * this.guideScale)) }); this.showLaser(!this.grabbing || mode == 0); + + + if (!this.grabbing) { + if (this.glowedIntersectingModel.isKnownID) { + Models.editModel(this.glowedIntersectingModel, { glowLevel: 0.0 }); + } + + var intersection = Models.findRayIntersection({ + origin: this.palmPosition, + direction: this.front + }); + if (intersection.accurate && intersection.modelID.isKnownID) { + this.glowedIntersectingModel = intersection.modelID; + Models.editModel(this.glowedIntersectingModel, { glowLevel: 0.25 }); + } + } } this.showLaser = function(show) { From 0bdd20abc9d06a8d986a44c0232cd8c75dbc6ea7 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 5 Jun 2014 15:46:07 -0700 Subject: [PATCH 5/9] Coding Standard --- libraries/models/src/ModelTree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/models/src/ModelTree.cpp b/libraries/models/src/ModelTree.cpp index e88a969061..466d4c5273 100644 --- a/libraries/models/src/ModelTree.cpp +++ b/libraries/models/src/ModelTree.cpp @@ -185,7 +185,7 @@ void ModelTree::addModel(const ModelItemID& modelID, const ModelItemProperties& glm::vec3 position = model.getPosition(); float size = std::max(MINIMUM_MODEL_ELEMENT_SIZE, model.getRadius()); - ModelTreeElement* element = (ModelTreeElement*)getOrCreateChildElementAt(position.x, position.y, position.z, size); + ModelTreeElement* element = static_cast(getOrCreateChildElementAt(position.x, position.y, position.z, size)); element->storeModel(model); _isDirty = true; From 150477eea44d0438137333499fcda498072c76ea Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 5 Jun 2014 15:47:25 -0700 Subject: [PATCH 6/9] Models.addModel now returns a more accurate modelID --- libraries/models/src/ModelItem.cpp | 31 +++++-------------- libraries/models/src/ModelItem.h | 1 + .../models/src/ModelsScriptingInterface.cpp | 3 +- 3 files changed, 11 insertions(+), 24 deletions(-) diff --git a/libraries/models/src/ModelItem.cpp b/libraries/models/src/ModelItem.cpp index b6f4fe6c1d..810129a81f 100644 --- a/libraries/models/src/ModelItem.cpp +++ b/libraries/models/src/ModelItem.cpp @@ -47,6 +47,12 @@ uint32_t ModelItem::getNextCreatorTokenID() { return creatorTokenID; } +uint32_t ModelItem::getNextModelItemID() { + uint32_t modelID = _nextID; + _nextID++; + return modelID; +} + void ModelItem::handleAddModelResponse(const QByteArray& packet) { const unsigned char* dataAt = reinterpret_cast(packet.data()); int numBytesPacketHeader = numBytesForPacketHeader(packet); @@ -70,31 +76,10 @@ ModelItem::ModelItem() { } ModelItem::ModelItem(const ModelItemID& modelItemID, const ModelItemProperties& properties) { - _id = modelItemID.id; _creatorTokenID = modelItemID.creatorTokenID; - - // init values with defaults before calling setProperties - uint64_t now = usecTimestampNow(); - _lastEdited = now; - _lastUpdated = now; - - _position = glm::vec3(0,0,0); - _radius = 0; - rgbColor noColor = { 0, 0, 0 }; - memcpy(_color, noColor, sizeof(_color)); - _shouldDie = false; - _modelURL = MODEL_DEFAULT_MODEL_URL; - _modelRotation = MODEL_DEFAULT_MODEL_ROTATION; - // animation related - _animationURL = MODEL_DEFAULT_ANIMATION_URL; - _animationIsPlaying = false; - _animationFrameIndex = 0.0f; - _animationFPS = MODEL_DEFAULT_ANIMATION_FPS; - _glowLevel = 0.0f; - - _jointMappingCompleted = false; - _lastAnimated = now; + rgbColor defaultColor = { 0, 0, 0 }; + init(glm::vec3(), 0.0f, defaultColor, modelItemID.id); setProperties(properties); } diff --git a/libraries/models/src/ModelItem.h b/libraries/models/src/ModelItem.h index 9a558f2ef4..563d394419 100644 --- a/libraries/models/src/ModelItem.h +++ b/libraries/models/src/ModelItem.h @@ -276,6 +276,7 @@ public: // these methods allow you to create models, and later edit them. static uint32_t getIDfromCreatorTokenID(uint32_t creatorTokenID); static uint32_t getNextCreatorTokenID(); + static uint32_t getNextModelItemID(); static void handleAddModelResponse(const QByteArray& packet); void mapJoints(const QStringList& modelJointNames); diff --git a/libraries/models/src/ModelsScriptingInterface.cpp b/libraries/models/src/ModelsScriptingInterface.cpp index 7e08571fe5..f4cbf14086 100644 --- a/libraries/models/src/ModelsScriptingInterface.cpp +++ b/libraries/models/src/ModelsScriptingInterface.cpp @@ -28,8 +28,9 @@ ModelItemID ModelsScriptingInterface::addModel(const ModelItemProperties& proper // The application will keep track of creatorTokenID uint32_t creatorTokenID = ModelItem::getNextCreatorTokenID(); + uint32_t modelID = ModelItem::getNextModelItemID(); - ModelItemID id(NEW_MODEL, creatorTokenID, false ); + ModelItemID id(modelID, creatorTokenID, false ); // queue the packet queueModelMessage(PacketTypeModelAddOrEdit, id, properties); From 9322e1f71b5d3e0f31127f3ef3b1762a24bdcd4f Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 5 Jun 2014 17:36:56 -0700 Subject: [PATCH 7/9] More work on editModels --- examples/editModels.js | 205 ++++++++++++++++++++++++++++------------- 1 file changed, 139 insertions(+), 66 deletions(-) diff --git a/examples/editModels.js b/examples/editModels.js index 8e2f6b4911..67e2bdb198 100644 --- a/examples/editModels.js +++ b/examples/editModels.js @@ -18,7 +18,8 @@ var toolWidth = 50; var LASER_WIDTH = 4; var LASER_COLOR = { red: 255, green: 0, blue: 0 }; -var LASER_LENGTH_FACTOR = 5; +var LASER_LENGTH_FACTOR = 500 +; var LEFT = 0; var RIGHT = 1; @@ -92,6 +93,11 @@ function controller(wichSide) { this.oldModelPosition; this.oldModelRadius; + this.positionAtGrab; + this.rotationAtGrab; + this.modelPositionAtGrab; + this.modelRotationAtGrab; + this.jointsIntersectingFromStart = []; this.laser = Overlays.addOverlay("line3d", { @@ -149,6 +155,11 @@ function controller(wichSide) { this.oldModelRotation = properties.modelRotation; this.oldModelRadius = properties.radius; + this.positionAtGrab = this.palmPosition; + this.rotationAtGrab = this.rotation; + this.modelPositionAtGrab = properties.position; + this.modelRotationAtGrab = properties.modelRotation; + this.jointsIntersectingFromStart = []; for (var i = 0; i < jointList.length; i++) { var distance = Vec3.distance(MyAvatar.getJointPosition(jointList[i]), this.oldModelPosition); @@ -174,12 +185,16 @@ function controller(wichSide) { } } - print("closestJoint: " + jointList[closestJointIndex]); - print("closestJointDistance (attach max distance): " + closestJointDistance + " (" + this.oldModelRadius + ")"); + if (closestJointIndex != -1) { + print("closestJoint: " + jointList[closestJointIndex]); + print("closestJointDistance (attach max distance): " + closestJointDistance + " (" + this.oldModelRadius + ")"); + } if (closestJointDistance < this.oldModelRadius) { - if (this.jointsIntersectingFromStart.indexOf(closestJointIndex) != -1) { + if (this.jointsIntersectingFromStart.indexOf(closestJointIndex) != -1 || + (leftController.grabbing && rightController.grabbing && + leftController.modelID.id == rightController.modelID.id)) { // Do nothing } else { print("Attaching to " + jointList[closestJointIndex]); @@ -193,6 +208,7 @@ function controller(wichSide) { MyAvatar.attach(this.modelURL, jointList[closestJointIndex], attachmentOffset, attachmentRotation, 2.0 * this.oldModelRadius, true, false); + Models.deleteModel(this.modelID); } } @@ -281,12 +297,11 @@ function controller(wichSide) { }); this.showLaser(!this.grabbing || mode == 0); - + if (this.glowedIntersectingModel.isKnownID) { + Models.editModel(this.glowedIntersectingModel, { glowLevel: 0.0 }); + this.glowedIntersectingModel.isKnownID = false; + } if (!this.grabbing) { - if (this.glowedIntersectingModel.isKnownID) { - Models.editModel(this.glowedIntersectingModel, { glowLevel: 0.0 }); - } - var intersection = Models.findRayIntersection({ origin: this.palmPosition, direction: this.front @@ -307,6 +322,14 @@ function controller(wichSide) { this.moveModel = function () { if (this.grabbing) { + if (!this.modelID.isKnownID) { + print("Unknown grabbed ID " + this.modelID.id + ", isKnown: " + this.modelID.isKnownID); + this.modelID = Models.findRayIntersection({ + origin: this.palmPosition, + direction: this.front + }).modelID; + print("Identified ID " + this.modelID.id + ", isKnown: " + this.modelID.isKnownID); + } var newPosition; var newRotation; @@ -318,15 +341,22 @@ function controller(wichSide) { Vec3.multiply(this.up, this.y)); newPosition = Vec3.sum(newPosition, Vec3.multiply(this.right, this.z)); + + + newRotation = Quat.multiply(this.rotation, + Quat.inverse(this.oldRotation)); + newRotation = Quat.multiply(newRotation, + this.oldModelRotation); break; case 1: var forward = Vec3.multiplyQbyV(MyAvatar.orientation, { x: 0, y: 0, z: -1 }); var d = Vec3.dot(forward, MyAvatar.position); - var factor1 = Vec3.dot(forward, this.palmPosition) - d; - var factor2 = Vec3.dot(forward, this.oldModelPosition) - d; - var vector = Vec3.subtract(this.palmPosition, this.oldPalmPosition); + var factor1 = Vec3.dot(forward, this.positionAtGrab) - d; + var factor2 = Vec3.dot(forward, this.modelPositionAtGrab) - d; + var vector = Vec3.subtract(this.palmPosition, this.positionAtGrab); + print("factor1: " + factor1 + ", factor2: " + factor2); if (factor2 < 0) { factor2 = 0; } @@ -335,19 +365,17 @@ function controller(wichSide) { factor2 = 1; } - newPosition = Vec3.sum(this.oldModelPosition, + newPosition = Vec3.sum(this.modelPositionAtGrab, Vec3.multiply(vector, factor2 / factor1)); + + newRotation = Quat.multiply(this.rotation, + Quat.inverse(this.rotationAtGrab)); + newRotation = Quat.multiply(newRotation, + this.modelRotationAtGrab); break; } - - - newRotation = Quat.multiply(this.rotation, - Quat.inverse(this.oldRotation)); - newRotation = Quat.multiply(newRotation, - this.oldModelRotation); - Models.editModel(this.modelID, { position: newPosition, modelRotation: newRotation @@ -367,8 +395,46 @@ function controller(wichSide) { for (var i = 0; i < indicesToRemove.length; ++i) { this.jointsIntersectingFromStart.splice(this.jointsIntersectingFromStart.indexOf(indicesToRemove[i], 1)); } + + + jointList = MyAvatar.getJointNames(); + + var closestJointIndex = -1; + var closestJointDistance = 999999; + for (var i = 0; i < jointList.length; i++) { + var distance = Vec3.distance(MyAvatar.getJointPosition(jointList[i]), this.oldModelPosition); + if (distance < closestJointDistance) { + closestJointDistance = distance; + closestJointIndex = i; + } + } + + if (closestJointDistance < this.oldModelRadius) { + if (this.jointsIntersectingFromStart.indexOf(closestJointIndex) != -1 || + (leftController.grabbing && rightController.grabbing && + leftController.modelID.id == rightController.modelID.id)) { + // Do nothing + } else { + Vec3.print("Ball at: ", MyAvatar.getJointPosition(closestJointIndex)); + Overlays.editOverlay(this.ballGlowingJoint, { + position: MyAvatar.getJointPosition(closestJointIndex), + glowLevel: 0.25, + visible: true + }); + } + } else { + Overlays.editOverlay(this.ballGlowingJoint, { glowLevel: 0.0, visible: false }); + } } } + this.ballGlowingJoint = Overlays.addOverlay("sphere", { + position: { x: 0, y: 0, z: 0 }, + size: 1, + solid: true, + color: { red: 0, green: 255, blue: 0 }, + alpha: 1, + visible: false, + anchor: "MyAvatar"}); this.update = function () { this.oldPalmPosition = this.palmPosition; @@ -420,8 +486,12 @@ function controller(wichSide) { var attachmentIndex = -1; var attachmentX = LASER_LENGTH_FACTOR; + var newModel; + var newProperties; + for (var i = 0; i < attachments.length; ++i) { - var position = Vec3.sum(MyAvatar.getJointPosition(attachments[i].jointName), attachments[i].translation); + var position = Vec3.sum(MyAvatar.getJointPosition(attachments[i].jointName), + Vec3.multiplyQbyV(MyAvatar.getJointCombinedRotation(attachments[i].jointName), attachments[i].translation)); var scale = attachments[i].scale; var A = this.palmPosition; @@ -439,53 +509,56 @@ function controller(wichSide) { } if (attachmentIndex != -1) { + print("Detaching: " + attachments[attachmentIndex].modelURL); MyAvatar.detachOne(attachments[attachmentIndex].modelURL, attachments[attachmentIndex].jointName); - Models.addModel({ - position: Vec3.sum(MyAvatar.getJointPosition(attachments[attachmentIndex].jointName), - attachments[attachmentIndex].translation), - modelRotation: Quat.multiply(MyAvatar.getJointCombinedRotation(attachments[attachmentIndex].jointName), - attachments[attachmentIndex].rotation), - radius: attachments[attachmentIndex].scale / 2.0, - modelURL: attachments[attachmentIndex].modelURL - }); - } - - // There is none so ... - // Checking model tree - Vec3.print("Looking at: ", this.palmPosition); - var pickRay = { origin: this.palmPosition, - direction: Vec3.normalize(Vec3.subtract(this.tipPosition, this.palmPosition)) }; - var foundIntersection = Models.findRayIntersection(pickRay); - - if(!foundIntersection.accurate) { - return; - } - var foundModel = foundIntersection.modelID; - - if (!foundModel.isKnownID) { - var identify = Models.identifyModel(foundModel); - if (!identify.isKnownID) { - print("Unknown ID " + identify.id + " (update loop " + foundModel.id + ")"); - return; - } - foundModel = identify; - } - - var properties = Models.getModelProperties(foundModel); - print("foundModel.modelURL=" + properties.modelURL); - - if (isLocked(properties)) { - print("Model locked " + properties.id); + + newProperties = { + position: Vec3.sum(MyAvatar.getJointPosition(attachments[attachmentIndex].jointName), + Vec3.multiplyQbyV(MyAvatar.getJointCombinedRotation(attachments[attachmentIndex].jointName), attachments[attachmentIndex].translation)), + modelRotation: Quat.multiply(MyAvatar.getJointCombinedRotation(attachments[attachmentIndex].jointName), + attachments[attachmentIndex].rotation), + radius: attachments[attachmentIndex].scale / 2.0, + modelURL: attachments[attachmentIndex].modelURL + }; + newModel = Models.addModel(newProperties); } else { - print("Checking properties: " + properties.id + " " + properties.isKnownID); - var check = this.checkModel(properties); - if (check.valid) { - this.grab(foundModel, properties); - this.x = check.x; - this.y = check.y; - this.z = check.z; + // There is none so ... + // Checking model tree + Vec3.print("Looking at: ", this.palmPosition); + var pickRay = { origin: this.palmPosition, + direction: Vec3.normalize(Vec3.subtract(this.tipPosition, this.palmPosition)) }; + var foundIntersection = Models.findRayIntersection(pickRay); + + if(!foundIntersection.accurate) { + print("No accurate intersection"); return; } + newModel = foundIntersection.modelID; + + if (!newModel.isKnownID) { + var identify = Models.identifyModel(newModel); + if (!identify.isKnownID) { + print("Unknown ID " + identify.id + " (update loop " + newModel.id + ")"); + return; + } + newModel = identify; + } + newProperties = Models.getModelProperties(newModel); + } + + + print("foundModel.modelURL=" + newProperties.modelURL); + + if (isLocked(newProperties)) { + print("Model locked " + newProperties.id); + } else { + this.grab(newModel, newProperties); + + var check = this.checkModel(newProperties); + this.x = check.x; + this.y = check.y; + this.z = check.z; + return; } } } @@ -503,8 +576,8 @@ var rightController = new controller(RIGHT); function moveModels() { if (leftController.grabbing && rightController.grabbing && rightController.modelID.id == leftController.modelID.id) { - var newPosition = this.oldModelPosition; - var rotation = this.oldModelRotation; + var newPosition = leftController.oldModelPosition; + var rotation = leftController.oldModelRotation; var ratio = 1; From 4209c1102f56420a0024baa1ec997b008f3a329e Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 6 Jun 2014 10:26:08 -0700 Subject: [PATCH 8/9] Revert "Models.addModel now returns a more accurate modelID" This reverts commit 150477eea44d0438137333499fcda498072c76ea. --- libraries/models/src/ModelItem.cpp | 31 ++++++++++++++----- libraries/models/src/ModelItem.h | 1 - .../models/src/ModelsScriptingInterface.cpp | 3 +- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/libraries/models/src/ModelItem.cpp b/libraries/models/src/ModelItem.cpp index 810129a81f..b6f4fe6c1d 100644 --- a/libraries/models/src/ModelItem.cpp +++ b/libraries/models/src/ModelItem.cpp @@ -47,12 +47,6 @@ uint32_t ModelItem::getNextCreatorTokenID() { return creatorTokenID; } -uint32_t ModelItem::getNextModelItemID() { - uint32_t modelID = _nextID; - _nextID++; - return modelID; -} - void ModelItem::handleAddModelResponse(const QByteArray& packet) { const unsigned char* dataAt = reinterpret_cast(packet.data()); int numBytesPacketHeader = numBytesForPacketHeader(packet); @@ -76,10 +70,31 @@ ModelItem::ModelItem() { } ModelItem::ModelItem(const ModelItemID& modelItemID, const ModelItemProperties& properties) { + _id = modelItemID.id; _creatorTokenID = modelItemID.creatorTokenID; + + // init values with defaults before calling setProperties + uint64_t now = usecTimestampNow(); + _lastEdited = now; + _lastUpdated = now; + + _position = glm::vec3(0,0,0); + _radius = 0; + rgbColor noColor = { 0, 0, 0 }; + memcpy(_color, noColor, sizeof(_color)); + _shouldDie = false; + _modelURL = MODEL_DEFAULT_MODEL_URL; + _modelRotation = MODEL_DEFAULT_MODEL_ROTATION; - rgbColor defaultColor = { 0, 0, 0 }; - init(glm::vec3(), 0.0f, defaultColor, modelItemID.id); + // animation related + _animationURL = MODEL_DEFAULT_ANIMATION_URL; + _animationIsPlaying = false; + _animationFrameIndex = 0.0f; + _animationFPS = MODEL_DEFAULT_ANIMATION_FPS; + _glowLevel = 0.0f; + + _jointMappingCompleted = false; + _lastAnimated = now; setProperties(properties); } diff --git a/libraries/models/src/ModelItem.h b/libraries/models/src/ModelItem.h index 563d394419..9a558f2ef4 100644 --- a/libraries/models/src/ModelItem.h +++ b/libraries/models/src/ModelItem.h @@ -276,7 +276,6 @@ public: // these methods allow you to create models, and later edit them. static uint32_t getIDfromCreatorTokenID(uint32_t creatorTokenID); static uint32_t getNextCreatorTokenID(); - static uint32_t getNextModelItemID(); static void handleAddModelResponse(const QByteArray& packet); void mapJoints(const QStringList& modelJointNames); diff --git a/libraries/models/src/ModelsScriptingInterface.cpp b/libraries/models/src/ModelsScriptingInterface.cpp index f4cbf14086..7e08571fe5 100644 --- a/libraries/models/src/ModelsScriptingInterface.cpp +++ b/libraries/models/src/ModelsScriptingInterface.cpp @@ -28,9 +28,8 @@ ModelItemID ModelsScriptingInterface::addModel(const ModelItemProperties& proper // The application will keep track of creatorTokenID uint32_t creatorTokenID = ModelItem::getNextCreatorTokenID(); - uint32_t modelID = ModelItem::getNextModelItemID(); - ModelItemID id(modelID, creatorTokenID, false ); + ModelItemID id(NEW_MODEL, creatorTokenID, false ); // queue the packet queueModelMessage(PacketTypeModelAddOrEdit, id, properties); From 69adf8d0dd8edcb3314016ce8076d9b4c26e429a Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 6 Jun 2014 11:19:43 -0700 Subject: [PATCH 9/9] Removed extra debug, fix model jumping when releasing one of the two triggers. --- examples/editModels.js | 50 ++++++++++-------------------------------- 1 file changed, 11 insertions(+), 39 deletions(-) diff --git a/examples/editModels.js b/examples/editModels.js index 67e2bdb198..93a34b9a3a 100644 --- a/examples/editModels.js +++ b/examples/editModels.js @@ -356,7 +356,6 @@ function controller(wichSide) { var factor2 = Vec3.dot(forward, this.modelPositionAtGrab) - d; var vector = Vec3.subtract(this.palmPosition, this.positionAtGrab); - print("factor1: " + factor1 + ", factor2: " + factor2); if (factor2 < 0) { factor2 = 0; } @@ -395,46 +394,8 @@ function controller(wichSide) { for (var i = 0; i < indicesToRemove.length; ++i) { this.jointsIntersectingFromStart.splice(this.jointsIntersectingFromStart.indexOf(indicesToRemove[i], 1)); } - - - jointList = MyAvatar.getJointNames(); - - var closestJointIndex = -1; - var closestJointDistance = 999999; - for (var i = 0; i < jointList.length; i++) { - var distance = Vec3.distance(MyAvatar.getJointPosition(jointList[i]), this.oldModelPosition); - if (distance < closestJointDistance) { - closestJointDistance = distance; - closestJointIndex = i; - } - } - - if (closestJointDistance < this.oldModelRadius) { - if (this.jointsIntersectingFromStart.indexOf(closestJointIndex) != -1 || - (leftController.grabbing && rightController.grabbing && - leftController.modelID.id == rightController.modelID.id)) { - // Do nothing - } else { - Vec3.print("Ball at: ", MyAvatar.getJointPosition(closestJointIndex)); - Overlays.editOverlay(this.ballGlowingJoint, { - position: MyAvatar.getJointPosition(closestJointIndex), - glowLevel: 0.25, - visible: true - }); - } - } else { - Overlays.editOverlay(this.ballGlowingJoint, { glowLevel: 0.0, visible: false }); - } } } - this.ballGlowingJoint = Overlays.addOverlay("sphere", { - position: { x: 0, y: 0, z: 0 }, - size: 1, - solid: true, - color: { red: 0, green: 255, blue: 0 }, - alpha: 1, - visible: false, - anchor: "MyAvatar"}); this.update = function () { this.oldPalmPosition = this.palmPosition; @@ -617,6 +578,17 @@ function moveModels() { var w = Vec3.normalize(Vec3.cross(u, v)); rotation = Quat.multiply(Quat.angleAxis(angle, w), leftController.oldModelRotation); + + + leftController.positionAtGrab = leftController.palmPosition; + leftController.rotationAtGrab = leftController.rotation; + leftController.modelPositionAtGrab = leftController.oldModelPosition; + leftController.modelRotationAtGrab = rotation; + + rightController.positionAtGrab = rightController.palmPosition; + rightController.rotationAtGrab = rightController.rotation; + rightController.modelPositionAtGrab = rightController.oldModelPosition; + rightController.modelRotationAtGrab = rotation; break; }