Update to use new grab methods, only specifically request properties that we are actually using

This commit is contained in:
James B. Pollack 2015-09-21 17:38:49 -07:00
parent 92152b250a
commit f8d743aff0
2 changed files with 47 additions and 103 deletions

View file

@ -18,35 +18,35 @@ var WAND_SCRIPT_URL = Script.resolvePath("wand.js");
//create the wand in front of the avatar //create the wand in front of the avatar
var center = Vec3.sum(Vec3.sum(MyAvatar.position, { var center = Vec3.sum(Vec3.sum(MyAvatar.position, {
x: 0, x: 0,
y: 0.5, y: 0.5,
z: 0 z: 0
}), Vec3.multiply(0.5, Quat.getFront(Camera.getOrientation()))); }), Vec3.multiply(0.5, Quat.getFront(Camera.getOrientation())));
var wand = Entities.addEntity({ var wand = Entities.addEntity({
name: 'Bubble Wand', name: 'Bubble Wand',
type: "Model", type: "Model",
modelURL: WAND_MODEL, modelURL: WAND_MODEL,
position: center, position: center,
gravity: { gravity: {
x: 0, x: 0,
y: 0, y: 0,
z: 0, z: 0,
}, },
dimensions: { dimensions: {
x: 0.05, x: 0.05,
y: 0.25, y: 0.25,
z: 0.05 z: 0.05
}, },
//must be enabled to be grabbable in the physics engine //must be enabled to be grabbable in the physics engine
collisionsWillMove: true, collisionsWillMove: true,
compoundShapeURL: WAND_COLLISION_SHAPE, compoundShapeURL: WAND_COLLISION_SHAPE,
script: WAND_SCRIPT_URL script: WAND_SCRIPT_URL
}); });
function cleanup() { function cleanup() {
// the line below this is commented out to make the wand that you create persistent. // the line below this is commented out to make the wand that you create persistent.
Entities.deleteEntity(wand); // Entities.deleteEntity(wand);
} }

View file

@ -36,10 +36,6 @@
var WAND_TIP_OFFSET = 0.095; var WAND_TIP_OFFSET = 0.095;
var VELOCITY_THRESHOLD = 0.5; var VELOCITY_THRESHOLD = 0.5;
var GRAB_USER_DATA_KEY = "grabKey";
var _this;
function interval() { function interval() {
var lastTime = new Date().getTime() / 1000; var lastTime = new Date().getTime() / 1000;
@ -53,6 +49,7 @@
var checkInterval = interval(); var checkInterval = interval();
var _this;
var BubbleWand = function() { var BubbleWand = function() {
_this = this; _this = this;
@ -63,73 +60,27 @@
currentBubble: null, currentBubble: null,
preload: function(entityID) { preload: function(entityID) {
this.entityID = entityID; this.entityID = entityID;
this.properties = Entities.getEntityProperties(this.entityID);
Script.update.connect(this.update); Script.update.connect(this.update);
}, },
unload: function(entityID) { unload: function() {
Script.update.disconnect(this.update); Script.update.disconnect(this.update);
},
update: function(deltaTime) {
this.timePassed = deltaTime;
var defaultGrabData = {
activated: false,
avatarId: null
};
var grabData = getEntityCustomData(GRAB_USER_DATA_KEY, _this.entityID, defaultGrabData);
if (grabData.activated && grabData.avatarId === MyAvatar.sessionUUID) {
// remember we're being grabbed so we can detect being released
_this.beingGrabbed = true;
//the first time we want to make a bubble
if (_this.currentBubble === null) {
_this.createBubbleAtTipOfWand();
}
var properties = Entities.getEntityProperties(_this.entityID);
var dt = deltaTime;
_this.growBubbleWithWandVelocity(properties, dt);
var wandTipPosition = _this.getWandTipPosition(properties);
//update the bubble to stay with the wand tip
Entities.editEntity(_this.currentBubble, {
position: wandTipPosition,
});
} else if (_this.beingGrabbed) {
// if we are not being grabbed, and we previously were, then we were just released, remember that
_this.beingGrabbed = false;
//remove the current bubble when the wand is released
Entities.deleteEntity(_this.currentBubble);
_this.currentBubble = null
return
}
}, },
getWandTipPosition: function(properties) { getWandTipPosition: function(properties) {
//the tip of the wand is going to be in a different place than the center, so we move in space relative to the model to find that position //the tip of the wand is going to be in a different place than the center, so we move in space relative to the model to find that position
var upVector = Quat.getUp(properties.rotation); var upVector = Quat.getUp(properties.rotation);
var frontVector = Quat.getFront(properties.rotation); var frontVector = Quat.getFront(properties.rotation);
var upOffset = Vec3.multiply(upVector, WAND_TIP_OFFSET); var upOffset = Vec3.multiply(upVector, WAND_TIP_OFFSET);
var wandTipPosition = Vec3.sum(properties.position, upOffset); var wandTipPosition = Vec3.sum(properties.position, upOffset);
this.wandTipPosition = wandTipPosition;
return wandTipPosition return wandTipPosition
}, },
addCollisionsToBubbleAfterCreation: function(bubble) { addCollisionsToBubbleAfterCreation: function(bubble) {
//if the bubble collide immediately, we get weird effects. so we add collisions after release
Entities.editEntity(bubble, { Entities.editEntity(bubble, {
collisionsWillMove: true collisionsWillMove: true
}) })
}, },
randomizeBubbleGravity: function() { randomizeBubbleGravity: function() {
//change up the gravity a little bit for variation in floating effects
var randomNumber = randInt(0, 3); var randomNumber = randInt(0, 3);
var gravity = { var gravity = {
x: 0, x: 0,
@ -139,9 +90,11 @@
return gravity return gravity
}, },
growBubbleWithWandVelocity: function(properties, deltaTime) { growBubbleWithWandVelocity: function(properties, deltaTime) {
//get the wand and tip position for calculations
var wandPosition = properties.position; var wandPosition = properties.position;
var wandTipPosition = this.getWandTipPosition(properties) var wandTipPosition = this.getWandTipPosition(properties)
// velocity = distance / time
var distance = Vec3.subtract(wandPosition, this.lastPosition); var distance = Vec3.subtract(wandPosition, this.lastPosition);
var velocity = Vec3.multiply(distance, 1 / deltaTime); var velocity = Vec3.multiply(distance, 1 / deltaTime);
@ -152,7 +105,7 @@
this.lastPosition = wandPosition; this.lastPosition = wandPosition;
//actually grow the bubble //actually grow the bubble
var dimensions = Entities.getEntityProperties(this.currentBubble).dimensions; var dimensions = Entities.getEntityProperties(this.currentBubble, "dimensions").dimensions;
if (velocityStrength > VELOCITY_THRESHOLD) { if (velocityStrength > VELOCITY_THRESHOLD) {
//add some variation in bubble sizes //add some variation in bubble sizes
@ -165,6 +118,7 @@
//bubbles pop after existing for a bit -- so set a random lifetime //bubbles pop after existing for a bit -- so set a random lifetime
var lifetime = randInt(BUBBLE_LIFETIME_MIN, BUBBLE_LIFETIME_MAX); var lifetime = randInt(BUBBLE_LIFETIME_MIN, BUBBLE_LIFETIME_MAX);
//edit the bubble properties at release
Entities.editEntity(this.currentBubble, { Entities.editEntity(this.currentBubble, {
velocity: velocity, velocity: velocity,
lifetime: lifetime, lifetime: lifetime,
@ -178,8 +132,7 @@
this.createBubbleAtTipOfWand(); this.createBubbleAtTipOfWand();
return return
} else { } else {
//grow small bubbles
// if the bubble is not yet full size, make the current bubble bigger
dimensions.x += GROWTH_FACTOR * velocityStrength; dimensions.x += GROWTH_FACTOR * velocityStrength;
dimensions.y += GROWTH_FACTOR * velocityStrength; dimensions.y += GROWTH_FACTOR * velocityStrength;
dimensions.z += GROWTH_FACTOR * velocityStrength; dimensions.z += GROWTH_FACTOR * velocityStrength;
@ -203,11 +156,10 @@
createBubbleAtTipOfWand: function() { createBubbleAtTipOfWand: function() {
//create a new bubble at the tip of the wand //create a new bubble at the tip of the wand
var properties = Entities.getEntityProperties(this.entityID); var properties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]);
var wandPosition = properties.position; var wandPosition = properties.position;
wandTipPosition = this.getWandTipPosition(properties); wandTipPosition = this.getWandTipPosition(properties);
this.wandTipPosition = wandTipPosition;
//store the position of the tip for use in velocity calculations //store the position of the tip for use in velocity calculations
this.lastPosition = wandPosition; this.lastPosition = wandPosition;
@ -225,37 +177,29 @@
shapeType: "sphere" shapeType: "sphere"
}); });
}, },
startNearGrab: function() { startNearGrab: function() {
print('START NEAR GRAB') if (this.currentBubble === null) {
if (_this.currentBubble === null) { this.createBubbleAtTipOfWand();
_this.createBubbleAtTipOfWand();
} }
}, },
continueNearGrab: function() { continueNearGrab: function() {
print('time passed:::' + checkInterval()); var deltaTime = checkInterval()
if (this.timePassed === null) { var properties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]);
this.timePassed = Date.now();
} else {
var newTime = = Date.now() - this.timePassed;
// this.timePassed = newTime;
}
print('CONTINUE NEAR GRAB::' + this.timePassed);
this.growBubbleWithWandVelocity(properties, deltaTime);
var wandTipPosition = this.getWandTipPosition(properties);
//update the bubble to stay with the wand tip
Entities.editEntity(this.currentBubble, {
position: wandTipPosition,
});
}, },
releaseGrab: function() { releaseGrab: function() {
//delete the lights and reset state //delete the current buble and reset state when the wand is released
if (this.hasSpotlight) { Entities.deleteEntity(this.currentBubble);
Entities.deleteEntity(this.spotlight); this.currentBubble = null
Entities.deleteEntity(this.glowLight);
this.hasSpotlight = false;
this.glowLight = null;
this.spotlight = null;
this.whichHand = null;
}
}, },
} }