mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-09 13:49:23 +02:00
add handedness setting to avatar-hold action. update stick.js to allow use of hydra
This commit is contained in:
parent
8bd80c511e
commit
8ed9a3ca02
5 changed files with 94 additions and 18 deletions
|
@ -10,8 +10,12 @@
|
||||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
//
|
//
|
||||||
|
|
||||||
|
var hand = "left";
|
||||||
|
var nullActionID = "00000000-0000-0000-0000-000000000000";
|
||||||
|
var controllerID;
|
||||||
|
var controllerActive;
|
||||||
var stickID = null;
|
var stickID = null;
|
||||||
var actionID = "00000000-0000-0000-0000-000000000000";
|
var actionID = nullActionID;
|
||||||
// sometimes if this is run immediately the stick doesn't get created? use a timer.
|
// sometimes if this is run immediately the stick doesn't get created? use a timer.
|
||||||
Script.setTimeout(function() {
|
Script.setTimeout(function() {
|
||||||
stickID = Entities.addEntity({
|
stickID = Entities.addEntity({
|
||||||
|
@ -19,12 +23,14 @@ Script.setTimeout(function() {
|
||||||
modelURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx",
|
modelURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx",
|
||||||
compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj",
|
compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj",
|
||||||
dimensions: {x: .11, y: .11, z: .59},
|
dimensions: {x: .11, y: .11, z: .59},
|
||||||
position: MyAvatar.getRightPalmPosition(),
|
position: MyAvatar.getRightPalmPosition(), // initial position doesn't matter, as long as it's close
|
||||||
rotation: MyAvatar.orientation,
|
rotation: MyAvatar.orientation,
|
||||||
damping: .1,
|
damping: .1,
|
||||||
collisionsWillMove: true
|
collisionsWillMove: true
|
||||||
});
|
});
|
||||||
actionID = Entities.addAction("hold", stickID, {relativePosition: {x: 0.0, y: 0.0, z: -0.9}, timeScale: 0.15});
|
actionID = Entities.addAction("hold", stickID, {relativePosition: {x: 0.0, y: 0.0, z: -0.9},
|
||||||
|
hand: hand,
|
||||||
|
timeScale: 0.15});
|
||||||
}, 3000);
|
}, 3000);
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,11 +38,20 @@ function cleanUp() {
|
||||||
Entities.deleteEntity(stickID);
|
Entities.deleteEntity(stickID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function positionStick(stickOrientation) {
|
||||||
|
var baseOffset = {x: 0.0, y: 0.0, z: -0.9};
|
||||||
|
var offset = Vec3.multiplyQbyV(stickOrientation, baseOffset);
|
||||||
|
Entities.updateAction(stickID, actionID, {relativePosition: offset,
|
||||||
|
relativeRotation: stickOrientation});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function mouseMoveEvent(event) {
|
function mouseMoveEvent(event) {
|
||||||
if (!stickID || actionID == "00000000-0000-0000-0000-000000000000") {
|
if (!stickID || actionID == nullActionID) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var windowCenterX = Window.innerWidth/ 2;
|
var windowCenterX = Window.innerWidth / 2;
|
||||||
var windowCenterY = Window.innerHeight / 2;
|
var windowCenterY = Window.innerHeight / 2;
|
||||||
var mouseXCenterOffset = event.x - windowCenterX;
|
var mouseXCenterOffset = event.x - windowCenterX;
|
||||||
var mouseYCenterOffset = event.y - windowCenterY;
|
var mouseYCenterOffset = event.y - windowCenterY;
|
||||||
|
@ -44,13 +59,34 @@ function mouseMoveEvent(event) {
|
||||||
var mouseYRatio = mouseYCenterOffset / windowCenterY;
|
var mouseYRatio = mouseYCenterOffset / windowCenterY;
|
||||||
|
|
||||||
var stickOrientation = Quat.fromPitchYawRollDegrees(mouseYRatio * -90, mouseXRatio * -90, 0);
|
var stickOrientation = Quat.fromPitchYawRollDegrees(mouseYRatio * -90, mouseXRatio * -90, 0);
|
||||||
var baseOffset = {x: 0.0, y: 0.0, z: -0.9};
|
positionStick(stickOrientation);
|
||||||
var offset = Vec3.multiplyQbyV(stickOrientation, baseOffset);
|
|
||||||
|
|
||||||
Entities.updateAction(stickID, actionID, {relativePosition: offset,
|
|
||||||
relativeRotation: stickOrientation,
|
|
||||||
timeScale: 0.15});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function initControls(){
|
||||||
|
if (hand == "right") {
|
||||||
|
controllerID = 3; // right handed
|
||||||
|
} else {
|
||||||
|
controllerID = 4; // left handed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function update(deltaTime){
|
||||||
|
var palmPosition = Controller.getSpatialControlPosition(controllerID);
|
||||||
|
controllerActive = (Vec3.length(palmPosition) > 0);
|
||||||
|
if(!controllerActive){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
stickOrientation = Controller.getSpatialControlRawRotation(controllerID);
|
||||||
|
var adjustment = Quat.fromPitchYawRollDegrees(180, 0, 0);
|
||||||
|
stickOrientation = Quat.multiply(stickOrientation, adjustment);
|
||||||
|
|
||||||
|
positionStick(stickOrientation);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Script.scriptEnding.connect(cleanUp);
|
Script.scriptEnding.connect(cleanUp);
|
||||||
Controller.mouseMoveEvent.connect(mouseMoveEvent);
|
Controller.mouseMoveEvent.connect(mouseMoveEvent);
|
||||||
|
Script.update.connect(update);
|
||||||
|
|
|
@ -29,7 +29,12 @@ AvatarActionHold::~AvatarActionHold() {
|
||||||
|
|
||||||
void AvatarActionHold::updateActionWorker(float deltaTimeStep) {
|
void AvatarActionHold::updateActionWorker(float deltaTimeStep) {
|
||||||
auto myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar();
|
auto myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar();
|
||||||
glm::vec3 palmPosition = myAvatar->getRightPalmPosition();
|
glm::vec3 palmPosition;
|
||||||
|
if (_hand == "right") {
|
||||||
|
palmPosition = myAvatar->getRightPalmPosition();
|
||||||
|
} else {
|
||||||
|
palmPosition = myAvatar->getLeftPalmPosition();
|
||||||
|
}
|
||||||
|
|
||||||
auto rotation = myAvatar->getWorldAlignedOrientation();
|
auto rotation = myAvatar->getWorldAlignedOrientation();
|
||||||
auto offset = rotation * _relativePosition;
|
auto offset = rotation * _relativePosition;
|
||||||
|
@ -55,28 +60,46 @@ bool AvatarActionHold::updateArguments(QVariantMap arguments) {
|
||||||
bool tSOk = true;
|
bool tSOk = true;
|
||||||
float timeScale =
|
float timeScale =
|
||||||
EntityActionInterface::extractFloatArgument("hold", arguments, "timeScale", tSOk, false);
|
EntityActionInterface::extractFloatArgument("hold", arguments, "timeScale", tSOk, false);
|
||||||
|
bool hOk = true;
|
||||||
|
QString hand =
|
||||||
|
EntityActionInterface::extractStringArgument("hold", arguments, "hand", hOk, false);
|
||||||
|
|
||||||
lockForWrite();
|
lockForWrite();
|
||||||
if (rPOk) {
|
if (rPOk) {
|
||||||
_relativePosition = relativePosition;
|
_relativePosition = relativePosition;
|
||||||
} else {
|
} else if (!_parametersSet) {
|
||||||
_relativePosition = glm::vec3(0.0f, 0.0f, 1.0f);
|
_relativePosition = glm::vec3(0.0f, 0.0f, 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rROk) {
|
if (rROk) {
|
||||||
_relativeRotation = relativeRotation;
|
_relativeRotation = relativeRotation;
|
||||||
} else {
|
} else if (!_parametersSet) {
|
||||||
_relativeRotation = glm::quat(0.0f, 0.0f, 0.0f, 1.0f);
|
_relativeRotation = glm::quat(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tSOk) {
|
if (tSOk) {
|
||||||
_linearTimeScale = timeScale;
|
_linearTimeScale = timeScale;
|
||||||
_angularTimeScale = timeScale;
|
_angularTimeScale = timeScale;
|
||||||
} else {
|
} else if (!_parametersSet) {
|
||||||
_linearTimeScale = 0.2;
|
_linearTimeScale = 0.2;
|
||||||
_angularTimeScale = 0.2;
|
_angularTimeScale = 0.2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (hOk) {
|
||||||
|
hand = hand.toLower();
|
||||||
|
if (hand == "left") {
|
||||||
|
_hand = "left";
|
||||||
|
} else if (hand == "right") {
|
||||||
|
_hand = "right";
|
||||||
|
} else {
|
||||||
|
qDebug() << "hold action -- invalid hand argument:" << hand;
|
||||||
|
_hand = "right";
|
||||||
|
}
|
||||||
|
} else if (!_parametersSet) {
|
||||||
|
_hand = "right";
|
||||||
|
}
|
||||||
|
|
||||||
|
_parametersSet = true;
|
||||||
_positionalTargetSet = true;
|
_positionalTargetSet = true;
|
||||||
_rotationalTargetSet = true;
|
_rotationalTargetSet = true;
|
||||||
_active = true;
|
_active = true;
|
||||||
|
|
|
@ -28,6 +28,8 @@ public:
|
||||||
private:
|
private:
|
||||||
glm::vec3 _relativePosition;
|
glm::vec3 _relativePosition;
|
||||||
glm::quat _relativeRotation;
|
glm::quat _relativeRotation;
|
||||||
|
QString _hand;
|
||||||
|
bool _parametersSet = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_AvatarActionHold_h
|
#endif // hifi_AvatarActionHold_h
|
||||||
|
|
|
@ -91,7 +91,6 @@ glm::vec3 EntityActionInterface::extractVec3Argument(QString objectName, QVarian
|
||||||
return glm::vec3(x, y, z);
|
return glm::vec3(x, y, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
glm::quat EntityActionInterface::extractQuatArgument(QString objectName, QVariantMap arguments,
|
glm::quat EntityActionInterface::extractQuatArgument(QString objectName, QVariantMap arguments,
|
||||||
QString argumentName, bool& ok, bool required) {
|
QString argumentName, bool& ok, bool required) {
|
||||||
if (!arguments.contains(argumentName)) {
|
if (!arguments.contains(argumentName)) {
|
||||||
|
@ -139,8 +138,6 @@ glm::quat EntityActionInterface::extractQuatArgument(QString objectName, QVarian
|
||||||
return glm::quat(w, x, y, z);
|
return glm::quat(w, x, y, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
float EntityActionInterface::extractFloatArgument(QString objectName, QVariantMap arguments,
|
float EntityActionInterface::extractFloatArgument(QString objectName, QVariantMap arguments,
|
||||||
QString argumentName, bool& ok, bool required) {
|
QString argumentName, bool& ok, bool required) {
|
||||||
if (!arguments.contains(argumentName)) {
|
if (!arguments.contains(argumentName)) {
|
||||||
|
@ -162,3 +159,18 @@ float EntityActionInterface::extractFloatArgument(QString objectName, QVariantMa
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString EntityActionInterface::extractStringArgument(QString objectName, QVariantMap arguments,
|
||||||
|
QString argumentName, bool& ok, bool required) {
|
||||||
|
if (!arguments.contains(argumentName)) {
|
||||||
|
if (required) {
|
||||||
|
qDebug() << objectName << "requires argument:" << argumentName;
|
||||||
|
}
|
||||||
|
ok = false;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant vV = arguments[argumentName];
|
||||||
|
QString v = vV.toString();
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
|
@ -59,6 +59,9 @@ protected:
|
||||||
QString argumentName, bool& ok, bool required = true);
|
QString argumentName, bool& ok, bool required = true);
|
||||||
static float extractFloatArgument(QString objectName, QVariantMap arguments,
|
static float extractFloatArgument(QString objectName, QVariantMap arguments,
|
||||||
QString argumentName, bool& ok, bool required = true);
|
QString argumentName, bool& ok, bool required = true);
|
||||||
|
static QString extractStringArgument(QString objectName, QVariantMap arguments,
|
||||||
|
QString argumentName, bool& ok, bool required = true);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue