Merge branch 'master' of github.com:highfidelity/hifi into box-stacking-1

This commit is contained in:
Seth Alves 2015-05-01 10:22:19 -07:00
commit 00fd7208b5
2 changed files with 137 additions and 140 deletions

View file

@ -5,17 +5,13 @@ var deltaMouse = {
z: 0 z: 0
} }
var entityProps; var entityProps;
var box, box2, ground; var targetPosition;
var baseMoveFactor = .001;
var finalMoveMultiplier;
var avatarEntityDistance;
var camYaw, dv;
var prevPosition;
var newPosition;
var flingVelocity;
var flingMultiplier = 10;
var moveUpDown = false; var moveUpDown = false;
var savedGravity;
var currentPosition, currentVelocity;
var grabSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/eric/sounds/CloseClamp.wav");
var releaseSound = SoundCache.getSound("https://hifi-public.s3.amazonaws.com/eric/sounds/ReleaseClamp.wav");
var DROP_DISTANCE = 5.0; var DROP_DISTANCE = 5.0;
var DROP_COLOR = { var DROP_COLOR = {
@ -23,14 +19,9 @@ var DROP_COLOR = {
green: 200, green: 200,
blue: 200 blue: 200
}; };
var DROP_WIDTH = 4; var DROP_WIDTH = 2;
var autoBox = false;
if (autoBox) {
setUpTestObjects();
}
var dropLine = Overlays.addOverlay("line3d", { var dropLine = Overlays.addOverlay("line3d", {
start: { start: {
x: 0, x: 0,
@ -50,104 +41,80 @@ var dropLine = Overlays.addOverlay("line3d", {
function mousePressEvent(event) { function mousePressEvent(event) {
if(!event.isLeftButton){ if (!event.isLeftButton) {
return; return;
} }
var pickRay = Camera.computePickRay(event.x, event.y); var pickRay = Camera.computePickRay(event.x, event.y);
var intersection = Entities.findRayIntersection(pickRay); var intersection = Entities.findRayIntersection(pickRay);
if (intersection.intersects && intersection.properties.collisionsWillMove) { if (intersection.intersects && intersection.properties.collisionsWillMove) {
grabbedEntity = intersection.entityID; grabbedEntity = intersection.entityID;
var props = Entities.getEntityProperties(grabbedEntity) var props = Entities.getEntityProperties(grabbedEntity)
prevPosition = props.position;
isGrabbing = true; isGrabbing = true;
savedGravity = props.gravity; targetPosition = props.position;
Overlays.editOverlay(dropLine, { currentPosition = props.position;
currentVelocity = props.velocity;
updateDropLine(targetPosition);
Audio.playSound(grabSound, {
position: props.position,
volume: 0.4
});
}
}
function updateDropLine(position) {
Overlays.editOverlay(dropLine, {
visible: true, visible: true,
start: props.position, start: position,
end: Vec3.sum(props.position, { end: Vec3.sum(position, {
x: 0, x: 0,
y: -DROP_DISTANCE, y: -DROP_DISTANCE,
z: 0 z: 0
}) })
}); })
Entities.editEntity(grabbedEntity, {
gravity: {
x: 0,
y: 0,
z: 0
}
});
//We need to store entity's current gravity, and then disable it while we move
}
} }
function mouseReleaseEvent() { function mouseReleaseEvent() {
if (isGrabbing) { if (isGrabbing) {
// flingObject(); isGrabbing = false;
Entities.editEntity(grabbedEntity, { Overlays.editOverlay(dropLine, {
gravity: savedGravity visible: false
}); });
targetPosition = null;
Audio.playSound(grabSound, {
position: entityProps.position,
volume: 0.25
});
} }
isGrabbing = false;
Overlays.editOverlay(dropLine, {
visible: false
});
}
function flingObject() {
//calculate velocity to give object base on current and previous position
entityProps = Entities.getEntityProperties(grabbedEntity);
flingVelocity = Vec3.subtract(entityProps.position, prevPosition);
flingVelocity = Vec3.multiply(flingMultiplier, flingVelocity);
Entities.editEntity(grabbedEntity, {
velocity: flingVelocity
});
} }
function mouseMoveEvent(event) { function mouseMoveEvent(event) {
if (isGrabbing) { if (isGrabbing) {
entityProps = Entities.getEntityProperties(grabbedEntity);
avatarEntityDistance = Vec3.distance(MyAvatar.position, entityProps.position);
finalMoveMultiplier = baseMoveFactor * Math.pow(avatarEntityDistance, 1.5);
deltaMouse.x = event.x - prevMouse.x; deltaMouse.x = event.x - prevMouse.x;
if (!moveUpDown) { if (!moveUpDown) {
deltaMouse.z = event.y - prevMouse.y; deltaMouse.z = event.y - prevMouse.y;
deltaMouse.y = 0;
} else { } else {
deltaMouse.y = (event.y - prevMouse.y) * -1; deltaMouse.y = (event.y - prevMouse.y) * -1;
deltaMouse.z = 0;
} }
finalMoveMultiplier = baseMoveFactor * Math.pow(avatarEntityDistance, 1.5); // Update the target position by the amount the mouse moved
deltaMouse = Vec3.multiply(deltaMouse, finalMoveMultiplier); var camYaw = Quat.safeEulerAngles(Camera.getOrientation()).y;
camYaw = Quat.safeEulerAngles(Camera.getOrientation()).y; var dPosition = Vec3.multiplyQbyV(Quat.fromPitchYawRollDegrees(0, camYaw, 0), deltaMouse);
dv = Vec3.multiplyQbyV(Quat.fromPitchYawRollDegrees(0, camYaw, 0), deltaMouse); // Adjust target position for the object by the mouse move
newPosition = Vec3.sum(entityProps.position, dv); var avatarEntityDistance = Vec3.distance(Camera.getPosition(), currentPosition);
Entities.editEntity(grabbedEntity, { // Scale distance we want to move by the distance from the camera to the grabbed object
position: newPosition // TODO: Correct SCREEN_TO_METERS to be correct for the actual FOV, resolution
}); var SCREEN_TO_METERS = 0.001;
Overlays.editOverlay(dropLine, { targetPosition = Vec3.sum(targetPosition, Vec3.multiply(dPosition, avatarEntityDistance * SCREEN_TO_METERS));
start: newPosition,
end: Vec3.sum(newPosition, {
x: 0,
y: -DROP_DISTANCE,
z: 0
})
});
flingVelocity = Vec3.subtract(entityProps.position, prevPosition);
flingVelocity = Vec3.multiply(flingMultiplier, flingVelocity);
Entities.editEntity(grabbedEntity, {
velocity: flingVelocity
});
prevPosition = entityProps.position;
} }
prevMouse.x = event.x; prevMouse.x = event.x;
prevMouse.y = event.y; prevMouse.y = event.y;
} }
function keyReleaseEvent(event) { function keyReleaseEvent(event) {
if (event.text === "SHIFT") { if (event.text === "SHIFT") {
moveUpDown = false; moveUpDown = false;
@ -160,69 +127,34 @@ function keyPressEvent(event) {
} }
} }
function update(deltaTime) {
if (isGrabbing) {
function setUpTestObjects() { entityProps = Entities.getEntityProperties(grabbedEntity);
var distance = 4; currentPosition = entityProps.position;
box = Entities.addEntity({ currentVelocity = entityProps.velocity;
type: 'Box',
position: Vec3.sum(MyAvatar.position, Vec3.multiply(distance, Quat.getFront(Camera.getOrientation()))),
dimensions: {
x: .5,
y: .5,
z: .5
},
color: {
red: 200,
green: 50,
blue: 192
},
collisionsWillMove: true,
gravity: {
x: 0,
y: -1,
z: 0
}
});
box2 = Entities.addEntity({ var dPosition = Vec3.subtract(targetPosition, currentPosition);
type: 'Box', var CLOSE_ENOUGH = 0.001;
position: Vec3.sum(MyAvatar.position, Vec3.multiply(distance + 1, Quat.getFront(Camera.getOrientation()))), if (Vec3.length(dPosition) > CLOSE_ENOUGH) {
dimensions: { // compute current velocity in the direction we want to move
x: .5, var velocityTowardTarget = Vec3.dot(currentVelocity, Vec3.normalize(dPosition));
y: .5, // compute the speed we would like to be going toward the target position
z: .5 var SPRING_RATE = 0.35;
}, var DAMPING_RATE = 0.55;
color: { var desiredVelocity = Vec3.multiply(dPosition, (1.0 / deltaTime) * SPRING_RATE);
red: 200, // compute how much we want to add to the existing velocity
green: 50, var addedVelocity = Vec3.subtract(desiredVelocity, velocityTowardTarget);
blue: 192 var newVelocity = Vec3.sum(currentVelocity, addedVelocity);
}, // Add Damping
collisionsWillMove: true, newVelocity = Vec3.subtract(newVelocity, Vec3.multiply(newVelocity, DAMPING_RATE));
gravity: { // Update entity
x: 0, Entities.editEntity(grabbedEntity, {
y: -1, velocity: newVelocity
z: 0 })
} }
}); updateDropLine(currentPosition);
}
ground = Entities.addEntity({
type: 'Box',
position: {
x: MyAvatar.position.x,
y: MyAvatar.position.y - 5,
z: MyAvatar.position.z
},
dimensions: {
x: 100,
y: 2,
z: 100
},
color: {
red: 20,
green: 200,
blue: 50
}
});
} }
Controller.mouseMoveEvent.connect(mouseMoveEvent); Controller.mouseMoveEvent.connect(mouseMoveEvent);
@ -230,3 +162,5 @@ Controller.mousePressEvent.connect(mousePressEvent);
Controller.mouseReleaseEvent.connect(mouseReleaseEvent); Controller.mouseReleaseEvent.connect(mouseReleaseEvent);
Controller.keyPressEvent.connect(keyPressEvent); Controller.keyPressEvent.connect(keyPressEvent);
Controller.keyReleaseEvent.connect(keyReleaseEvent); Controller.keyReleaseEvent.connect(keyReleaseEvent);
Script.update.connect(update);

63
examples/lotsoBlocks.js Normal file
View file

@ -0,0 +1,63 @@
var NUM_BLOCKS = 200;
var size;
var SPAWN_RANGE = 10;
var boxes = [];
var basePosition, avatarRot;
var isAssignmentScript = false;
if(isAssignmentScript){
basePosition = {x: 8000, y: 8000, z: 8000};
}
else {
avatarRot = Quat.fromPitchYawRollDegrees(0, MyAvatar.bodyYaw, 0.0);
basePosition = Vec3.sum(MyAvatar.position, Vec3.multiply(SPAWN_RANGE * 3, Quat.getFront(avatarRot)));
}
basePosition.y -= SPAWN_RANGE;
var ground = Entities.addEntity({
type: "Model",
modelURL: "https://hifi-public.s3.amazonaws.com/eric/models/woodFloor.fbx",
dimensions: {
x: 100,
y: 2,
z: 100
},
position: basePosition,
shapeType: 'box'
});
basePosition.y += SPAWN_RANGE + 2;
for (var i = 0; i < NUM_BLOCKS; i++) {
size = randFloat(-.2, 0.7);
boxes.push(Entities.addEntity({
type: 'Box',
dimensions: {
x: size,
y: size,
z: size
},
position: {
x: basePosition.x + randFloat(-SPAWN_RANGE, SPAWN_RANGE),
y: basePosition.y - randFloat(-SPAWN_RANGE, SPAWN_RANGE),
z: basePosition.z + randFloat(-SPAWN_RANGE, SPAWN_RANGE)
},
color: {red: Math.random() * 255, green: Math.random() * 255, blue: Math.random() * 255},
collisionsWillMove: true,
gravity: {x: 0, y: 0, z: 0}
}));
}
function cleanup() {
Entities.deleteEntity(ground);
boxes.forEach(function(box){
Entities.deleteEntity(box);
});
}
Script.scriptEnding.connect(cleanup);
function randFloat(low, high) {
return low + Math.random() * ( high - low );
}