when trigger is squeezed, check for things stuck to hand and unhook them

This commit is contained in:
Seth Alves 2016-02-26 10:14:09 -08:00
parent 783892ac2e
commit 6c9cf386a6
3 changed files with 41 additions and 1 deletions

View file

@ -801,6 +801,8 @@ function MyController(hand) {
this.isInitialGrab = false;
this.doubleParentGrab = false;
this.checkForStrayChildren();
if (this.state == STATE_SEARCHING ? this.triggerSmoothedReleased() : this.bumperReleased()) {
this.setState(STATE_RELEASE);
return;
@ -1751,6 +1753,17 @@ function MyController(hand) {
return data;
};
this.checkForStrayChildren = function() {
// sometimes things can get parented to a hand and this script is unaware. Search for such entities and
// unhook them.
var handJointIndex = MyAvatar.getJointIndex(this.hand === RIGHT_HAND ? "RightHand" : "LeftHand");
var children = Entities.getChildrenIDsOfJoint(MyAvatar.sessionUUID, handJointIndex);
children.forEach(function(childID) {
print("disconnecting stray child of hand: " + childID);
Entities.editEntity(childID, {parentID: NULL_UUID});
});
}
this.deactivateEntity = function(entityID, noVelocity) {
var data = getEntityCustomData(GRAB_USER_DATA_KEY, entityID, {});
if (data && data["refCount"]) {

View file

@ -12,6 +12,7 @@
#include "EntityItemID.h"
#include <VariantMapToScriptValue.h>
#include <SpatialParentFinder.h>
#include "EntitiesLogging.h"
#include "EntityActionFactoryInterface.h"
@ -1063,6 +1064,32 @@ QStringList EntityScriptingInterface::getJointNames(const QUuid& entityID) {
return result;
}
QVector<QUuid> EntityScriptingInterface::getChildrenIDsOfJoint(const QUuid& parentID, int jointIndex) {
QVector<QUuid> result;
if (!_entityTree) {
return result;
}
_entityTree->withReadLock([&] {
QSharedPointer<SpatialParentFinder> parentFinder = DependencyManager::get<SpatialParentFinder>();
if (!parentFinder) {
return;
}
bool success;
SpatiallyNestableWeakPointer parentWP = parentFinder->find(parentID, success);
if (!success) {
return;
}
SpatiallyNestablePointer parent = parentWP.lock();
if (!parent) {
return;
}
parent->forEachChild([&](SpatiallyNestablePointer child) {
result.push_back(child->getID());
});
});
return result;
}
float EntityScriptingInterface::calculateCost(float mass, float oldVelocity, float newVelocity) {
return std::abs(mass * (newVelocity - oldVelocity));
}

View file

@ -168,7 +168,7 @@ public slots:
Q_INVOKABLE int getJointIndex(const QUuid& entityID, const QString& name);
Q_INVOKABLE QStringList getJointNames(const QUuid& entityID);
Q_INVOKABLE QVector<QUuid> getChildrenIDsOfJoint(const QUuid& parentID, int jointIndex);
signals:
void collisionWithEntity(const EntityItemID& idA, const EntityItemID& idB, const Collision& collision);