make stick follow mouse motion

This commit is contained in:
Seth Alves 2015-06-10 21:08:00 -07:00
parent ba4fd7adf6
commit 8bd80c511e

View file

@ -1,7 +1,20 @@
// stick.js
// examples
//
// Created by Seth Alves on 2015-6-10
// Copyright 2015 High Fidelity, Inc.
//
// Allow avatar to hold a stick
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
var stickID = null;
var actionID = "00000000-0000-0000-0000-000000000000";
// sometimes if this is run immediately the stick doesn't get created? use a timer.
Script.setTimeout(function() {
var stickID = Entities.addEntity({
stickID = Entities.addEntity({
type: "Model",
modelURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx",
compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj",
@ -11,8 +24,33 @@ Script.setTimeout(function() {
damping: .1,
collisionsWillMove: true
});
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}, timeScale: 0.15});
}, 3000);
function cleanup() { Entities.deleteEntity(stickID); }
Script.scriptEnding.connect(cleanup);
function cleanUp() {
Entities.deleteEntity(stickID);
}
function mouseMoveEvent(event) {
if (!stickID || actionID == "00000000-0000-0000-0000-000000000000") {
return;
}
var windowCenterX = Window.innerWidth/ 2;
var windowCenterY = Window.innerHeight / 2;
var mouseXCenterOffset = event.x - windowCenterX;
var mouseYCenterOffset = event.y - windowCenterY;
var mouseXRatio = mouseXCenterOffset / windowCenterX;
var mouseYRatio = mouseYCenterOffset / windowCenterY;
var stickOrientation = Quat.fromPitchYawRollDegrees(mouseYRatio * -90, mouseXRatio * -90, 0);
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,
timeScale: 0.15});
}
Script.scriptEnding.connect(cleanUp);
Controller.mouseMoveEvent.connect(mouseMoveEvent);