Merge pull request #4163 from PhilipRosedale/master

Popcorn & building blocks scripts, some fixes
This commit is contained in:
Brad Hefta-Gaub 2015-01-29 09:28:23 -08:00
commit 838cb41646
6 changed files with 451 additions and 129 deletions

118
examples/blocks.js Normal file
View file

@ -0,0 +1,118 @@
//
// Blocks.js
//
// Created by Philip Rosedale on January 26, 2015
// Copyright 2015 High Fidelity, Inc.
//
// Create a bunch of building blocks and drop them onto a playing surface in front of you.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
var FLOOR_SIZE = 7.5;
var FLOOR_THICKNESS = 0.10;
var EDGE_THICKESS = 0.25;
var SCALE = 0.25;
var NUM_BLOCKS = 25;
var DROP_HEIGHT = SCALE * 8.0;
var GRAVITY = -1.0;
var LIFETIME = 6000;
var DAMPING = 0.50;
var blockTypes = [];
blockTypes.push({ x: 1, y: 1, z: 1, red: 255, green: 0, blue: 0 });
blockTypes.push({ x: 1, y: 1, z: 2, red: 0, green: 255, blue: 0 });
blockTypes.push({ x: 1, y: 2, z: 5, red: 0, green: 0, blue: 255 });
blockTypes.push({ x: 1, y: 2, z: 2, red: 255, green: 255, blue: 0 });
blockTypes.push({ x: 1, y: 1, z: 5, red: 0, green: 255, blue: 255 });
var center = Vec3.sum(MyAvatar.position, Vec3.multiply(FLOOR_SIZE * 2.0, Quat.getFront(Camera.getOrientation())));
var floor = Entities.addEntity(
{ type: "Box",
position: Vec3.subtract(center, { x: 0, y: SCALE / 2.0, z: 0 }),
dimensions: { x: FLOOR_SIZE, y: FLOOR_THICKNESS, z: FLOOR_SIZE },
color: { red: 128, green: 128, blue: 128 },
gravity: { x: 0, y: 0, z: 0 },
ignoreCollisions: false,
locked: true,
lifetime: LIFETIME });
var edge1 = Entities.addEntity(
{ type: "Box",
position: Vec3.sum(center, { x: FLOOR_SIZE / 2.0, y: FLOOR_THICKNESS / 2.0, z: 0 }),
dimensions: { x: EDGE_THICKESS, y: EDGE_THICKESS, z: FLOOR_SIZE },
color: { red: 128, green: 128, blue: 128 },
gravity: { x: 0, y: 0, z: 0 },
ignoreCollisions: false,
visible: true,
locked: true,
lifetime: LIFETIME });
var edge2 = Entities.addEntity(
{ type: "Box",
position: Vec3.sum(center, { x: -FLOOR_SIZE / 2.0, y: FLOOR_THICKNESS / 2.0, z: 0 }),
dimensions: { x: EDGE_THICKESS, y: EDGE_THICKESS, z: FLOOR_SIZE },
color: { red: 128, green: 128, blue: 128 },
gravity: { x: 0, y: 0, z: 0 },
ignoreCollisions: false,
visible: true,
locked: true,
lifetime: LIFETIME });
var edge3 = Entities.addEntity(
{ type: "Box",
position: Vec3.sum(center, { x: 0, y: FLOOR_THICKNESS / 2.0, z: -FLOOR_SIZE / 2.0 }),
dimensions: { x: FLOOR_SIZE, y: EDGE_THICKESS, z: EDGE_THICKESS },
color: { red: 128, green: 128, blue: 128 },
gravity: { x: 0, y: 0, z: 0 },
ignoreCollisions: false,
visible: true,
locked: true,
lifetime: LIFETIME });
var edge4 = Entities.addEntity(
{ type: "Box",
position: Vec3.sum(center, { x: 0, y: FLOOR_THICKNESS / 2.0, z: FLOOR_SIZE / 2.0 }),
dimensions: { x: FLOOR_SIZE, y: EDGE_THICKESS, z: EDGE_THICKESS },
color: { red: 128, green: 128, blue: 128 },
gravity: { x: 0, y: 0, z: 0 },
ignoreCollisions: false,
visible: true,
locked: true,
lifetime: LIFETIME });
blocks = [];
for (var i = 0; i < NUM_BLOCKS; i++) {
var which = Math.floor(Math.random() * blockTypes.length);
var type = blockTypes[which];
blocks.push(Entities.addEntity(
{ type: "Box",
position: { x: center.x + (Math.random() - 0.5) * (FLOOR_SIZE * 0.75),
y: center.y + DROP_HEIGHT,
z: center.z + (Math.random() - 0.5) * (FLOOR_SIZE * 0.75) },
dimensions: { x: type.x * SCALE, y: type.y * SCALE, z: type.z * SCALE },
color: { red: type.red, green: type.green, blue: type.blue },
gravity: { x: 0, y: GRAVITY, z: 0 },
ignoreCollisions: false,
damping: DAMPING,
lifetime: LIFETIME,
collisionsWillMove: true }));
}
function scriptEnding() {
Entities.deleteEntity(edge1);
Entities.deleteEntity(edge2);
Entities.deleteEntity(edge3);
Entities.deleteEntity(edge4);
Entities.deleteEntity(floor);
for (var i = 0; i < NUM_BLOCKS; i++) {
Entities.deleteEntity(blocks[i]);
}
}
Script.scriptEnding.connect(scriptEnding);

View file

@ -304,7 +304,6 @@ function makePlatform(gravity, scale, size) {
}
function entityCollisionWithEntity(entity1, entity2, collision) {
if (((entity1.id == bulletID.id) || (entity1.id == targetID.id)) &&
((entity2.id == bulletID.id) || (entity2.id == targetID.id))) {
score++;
@ -502,6 +501,7 @@ function scriptEnding() {
Overlays.deleteOverlay(pointer[1]);
Overlays.deleteOverlay(text);
MyAvatar.detachOne(gunModel);
MyAvatar.detachOne(gunModel);
clearPose();
}

View file

@ -19,8 +19,8 @@ var entityPropertyDialogBox = EntityPropertyDialogBox;
var MIN_ANGULAR_SIZE = 2;
var MAX_ANGULAR_SIZE = 45;
var allowLargeModels = false;
var allowSmallModels = false;
var allowLargeModels = true;
var allowSmallModels = true;
var wantEntityGlow = false;
var LEFT = 0;
@ -28,15 +28,16 @@ var RIGHT = 1;
var jointList = MyAvatar.getJointNames();
var STICKS = 0;
var MAPPED = 1;
var mode = STICKS;
var LASER_WIDTH = 3;
var LASER_COLOR = { red: 50, green: 150, blue: 200 };
var DROP_COLOR = { red: 200, green: 200, blue: 200 };
var DROP_WIDTH = 4;
var DROP_DISTANCE = 5.0;
var LASER_WIDTH = 4;
var LASER_COLOR = [{ red: 200, green: 150, blue: 50 }, // STICKS
{ red: 50, green: 150, blue: 200 }]; // MAPPED
var LASER_LENGTH_FACTOR = 500;
var velocity = { x: 0, y: 0, z: 0 };
var lastAccurateIntersection = null;
var accurateIntersections = 0;
var totalIntersections = 0;
@ -107,6 +108,7 @@ function controller(wichSide) {
this.positionAtGrab;
this.rotationAtGrab;
this.gravityAtGrab;
this.modelPositionAtGrab;
this.modelRotationAtGrab;
this.jointsIntersectingFromStart = [];
@ -114,13 +116,21 @@ function controller(wichSide) {
this.laser = Overlays.addOverlay("line3d", {
start: { x: 0, y: 0, z: 0 },
end: { x: 0, y: 0, z: 0 },
color: LASER_COLOR[mode],
color: LASER_COLOR,
alpha: 1,
visible: false,
lineWidth: LASER_WIDTH,
anchor: "MyAvatar"
});
this.dropLine = Overlays.addOverlay("line3d", {
start: { x: 0, y: 0, z: 0 },
end: { x: 0, y: 0, z: 0 },
color: DROP_COLOR,
alpha: 1,
visible: false,
lineWidth: DROP_WIDTH });
this.guideScale = 0.02;
this.ball = Overlays.addOverlay("sphere", {
position: { x: 0, y: 0, z: 0 },
@ -158,6 +168,7 @@ function controller(wichSide) {
this.entityID = entityID;
this.modelURL = properties.modelURL;
this.oldModelPosition = properties.position;
this.oldModelRotation = properties.rotation;
this.oldModelHalfDiagonal = Vec3.length(properties.dimensions) / 2.0;
@ -166,6 +177,10 @@ function controller(wichSide) {
this.rotationAtGrab = this.rotation;
this.modelPositionAtGrab = properties.position;
this.modelRotationAtGrab = properties.rotation;
this.gravityAtGrab = properties.gravity;
Entities.editEntity(entityID, { gravity: { x: 0, y: 0, z: 0 }, velocity: { x: 0, y: 0, z: 0 } });
this.jointsIntersectingFromStart = [];
for (var i = 0; i < jointList.length; i++) {
var distance = Vec3.distance(MyAvatar.getJointPosition(jointList[i]), this.oldModelPosition);
@ -174,10 +189,14 @@ function controller(wichSide) {
}
}
this.showLaser(false);
Overlays.editOverlay(this.dropLine, { visible: true });
}
this.release = function () {
if (this.grabbing) {
Entities.editEntity(this.entityID, { gravity: this.gravityAtGrab });
jointList = MyAvatar.getJointNames();
var closestJointIndex = -1;
@ -216,6 +235,8 @@ function controller(wichSide) {
Entities.deleteEntity(this.entityID);
}
}
Overlays.editOverlay(this.dropLine, { visible: false });
}
this.grabbing = false;
@ -288,7 +309,6 @@ function controller(wichSide) {
end: endPosition
});
Overlays.editOverlay(this.ball, {
position: endPosition
});
@ -300,7 +320,7 @@ function controller(wichSide) {
start: Vec3.sum(endPosition, Vec3.multiply(this.up, 2 * this.guideScale)),
end: Vec3.sum(endPosition, Vec3.multiply(this.up, -2 * this.guideScale))
});
this.showLaser(!this.grabbing || mode == STICKS);
this.showLaser(!this.grabbing);
if (this.glowedIntersectingModel.isKnownID) {
Entities.editEntity(this.glowedIntersectingModel, { glowLevel: 0.0 });
@ -332,7 +352,7 @@ function controller(wichSide) {
Overlays.editOverlay(this.leftRight, { visible: show });
Overlays.editOverlay(this.topDown, { visible: show });
}
this.moveEntity = function () {
this.moveEntity = function (deltaTime) {
if (this.grabbing) {
if (!this.entityID.isKnownID) {
print("Unknown grabbed ID " + this.entityID.id + ", isKnown: " + this.entityID.isKnownID);
@ -344,55 +364,34 @@ function controller(wichSide) {
var newPosition;
var newRotation;
switch (mode) {
case STICKS:
newPosition = Vec3.sum(this.palmPosition,
Vec3.multiply(this.front, this.x));
newPosition = Vec3.sum(newPosition,
Vec3.multiply(this.up, this.y));
newPosition = Vec3.sum(newPosition,
Vec3.multiply(this.right, this.z));
newRotation = Quat.multiply(this.rotation,
Quat.inverse(this.oldRotation));
newRotation = Quat.multiply(newRotation,
this.oldModelRotation);
break;
case MAPPED:
var forward = Vec3.multiplyQbyV(MyAvatar.orientation, { x: 0, y: 0, z: -1 });
var d = Vec3.dot(forward, MyAvatar.position);
var factor1 = Vec3.dot(forward, this.positionAtGrab) - d;
var factor2 = Vec3.dot(forward, this.modelPositionAtGrab) - d;
var vector = Vec3.subtract(this.palmPosition, this.positionAtGrab);
if (factor2 < 0) {
factor2 = 0;
}
if (factor1 <= 0) {
factor1 = 1;
factor2 = 1;
}
newPosition = Vec3.sum(this.modelPositionAtGrab,
Vec3.multiply(vector,
factor2 / factor1));
newRotation = Quat.multiply(this.rotation,
Quat.inverse(this.rotationAtGrab));
newRotation = Quat.multiply(newRotation, newRotation);
newRotation = Quat.multiply(newRotation,
this.modelRotationAtGrab);
break;
var CONSTANT_SCALING_FACTOR = 5.0;
var MINIMUM_SCALING_DISTANCE = 2.0;
var distanceToModel = Vec3.length(Vec3.subtract(this.oldModelPosition, this.palmPosition));
if (distanceToModel < MINIMUM_SCALING_DISTANCE) {
distanceToModel = MINIMUM_SCALING_DISTANCE;
}
var deltaPalm = Vec3.multiply(distanceToModel * CONSTANT_SCALING_FACTOR, Vec3.subtract(this.palmPosition, this.oldPalmPosition));
newPosition = Vec3.sum(this.oldModelPosition, deltaPalm);
newRotation = Quat.multiply(this.rotation,
Quat.inverse(this.rotationAtGrab));
newRotation = Quat.multiply(newRotation, newRotation);
newRotation = Quat.multiply(newRotation,
this.modelRotationAtGrab);
velocity = Vec3.multiply(1.0 / deltaTime, Vec3.subtract(newPosition, this.oldModelPosition));
Entities.editEntity(this.entityID, {
position: newPosition,
rotation: newRotation
rotation: newRotation,
velocity: velocity
});
this.oldModelRotation = newRotation;
this.oldModelPosition = newPosition;
Overlays.editOverlay(this.dropLine, { start: newPosition, end: Vec3.sum(newPosition, { x: 0, y: -DROP_DISTANCE, z: 0 }) });
var indicesToRemove = [];
for (var i = 0; i < this.jointsIntersectingFromStart.length; ++i) {
var distance = Vec3.distance(MyAvatar.getJointPosition(this.jointsIntersectingFromStart[i]), this.oldModelPosition);
@ -428,15 +427,6 @@ function controller(wichSide) {
this.triggerValue = Controller.getTriggerValue(this.trigger);
var bumperValue = Controller.isButtonPressed(this.bumper);
if (bumperValue && !this.bumperValue) {
if (mode === STICKS) {
mode = MAPPED;
} else if (mode === MAPPED) {
mode = STICKS;
}
Overlays.editOverlay(leftController.laser, { color: LASER_COLOR[mode] });
Overlays.editOverlay(rightController.laser, { color: LASER_COLOR[mode] });
}
this.bumperValue = bumperValue;
@ -548,61 +538,37 @@ function controller(wichSide) {
var leftController = new controller(LEFT);
var rightController = new controller(RIGHT);
function moveEntities() {
function moveEntities(deltaTime) {
if (leftController.grabbing && rightController.grabbing && rightController.entityID.id == leftController.entityID.id) {
var newPosition = leftController.oldModelPosition;
var rotation = leftController.oldModelRotation;
var ratio = 1;
var u = Vec3.normalize(Vec3.subtract(rightController.oldPalmPosition, leftController.oldPalmPosition));
var v = Vec3.normalize(Vec3.subtract(rightController.palmPosition, leftController.palmPosition));
switch (mode) {
case STICKS:
var oldLeftPoint = Vec3.sum(leftController.oldPalmPosition, Vec3.multiply(leftController.oldFront, leftController.x));
var oldRightPoint = Vec3.sum(rightController.oldPalmPosition, Vec3.multiply(rightController.oldFront, rightController.x));
var oldMiddle = Vec3.multiply(Vec3.sum(oldLeftPoint, oldRightPoint), 0.5);
var oldLength = Vec3.length(Vec3.subtract(oldLeftPoint, oldRightPoint));
var leftPoint = Vec3.sum(leftController.palmPosition, Vec3.multiply(leftController.front, leftController.x));
var rightPoint = Vec3.sum(rightController.palmPosition, Vec3.multiply(rightController.front, rightController.x));
var middle = Vec3.multiply(Vec3.sum(leftPoint, rightPoint), 0.5);
var length = Vec3.length(Vec3.subtract(leftPoint, rightPoint));
ratio = length / oldLength;
newPosition = Vec3.sum(middle,
Vec3.multiply(Vec3.subtract(leftController.oldModelPosition, oldMiddle), ratio));
break;
case MAPPED:
var u = Vec3.normalize(Vec3.subtract(rightController.oldPalmPosition, leftController.oldPalmPosition));
var v = Vec3.normalize(Vec3.subtract(rightController.palmPosition, leftController.palmPosition));
var cos_theta = Vec3.dot(u, v);
if (cos_theta > 1) {
cos_theta = 1;
}
var angle = Math.acos(cos_theta) / Math.PI * 180;
if (angle < 0.1) {
return;
}
var w = Vec3.normalize(Vec3.cross(u, v));
rotation = Quat.multiply(Quat.angleAxis(angle, w), leftController.oldModelRotation);
leftController.positionAtGrab = leftController.palmPosition;
leftController.rotationAtGrab = leftController.rotation;
leftController.modelPositionAtGrab = leftController.oldModelPosition;
leftController.modelRotationAtGrab = rotation;
rightController.positionAtGrab = rightController.palmPosition;
rightController.rotationAtGrab = rightController.rotation;
rightController.modelPositionAtGrab = rightController.oldModelPosition;
rightController.modelRotationAtGrab = rotation;
break;
var cos_theta = Vec3.dot(u, v);
if (cos_theta > 1) {
cos_theta = 1;
}
var angle = Math.acos(cos_theta) / Math.PI * 180;
if (angle < 0.1) {
return;
}
var w = Vec3.normalize(Vec3.cross(u, v));
rotation = Quat.multiply(Quat.angleAxis(angle, w), leftController.oldModelRotation);
leftController.positionAtGrab = leftController.palmPosition;
leftController.rotationAtGrab = leftController.rotation;
leftController.modelPositionAtGrab = leftController.oldModelPosition;
leftController.modelRotationAtGrab = rotation;
rightController.positionAtGrab = rightController.palmPosition;
rightController.rotationAtGrab = rightController.rotation;
rightController.modelPositionAtGrab = rightController.oldModelPosition;
rightController.modelRotationAtGrab = rotation;
Entities.editEntity(leftController.entityID, {
position: newPosition,
rotation: rotation,
@ -612,7 +578,6 @@ function moveEntities() {
y: leftController.oldModelHalfDiagonal * ratio,
z: leftController.oldModelHalfDiagonal * ratio }
});
leftController.oldModelPosition = newPosition;
leftController.oldModelRotation = rotation;
@ -623,8 +588,8 @@ function moveEntities() {
rightController.oldModelHalfDiagonal *= ratio;
return;
}
leftController.moveEntity();
rightController.moveEntity();
leftController.moveEntity(deltaTime);
rightController.moveEntity(deltaTime);
}
var hydraConnected = false;
@ -642,7 +607,7 @@ function checkController(deltaTime) {
leftController.update();
rightController.update();
moveEntities();
moveEntities(deltaTime);
} else {
if (hydraConnected) {
hydraConnected = false;

View file

@ -10,18 +10,27 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/";
hitSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Collisions-ballhitsandcatches/billiards/collision1.wav");
var rightHandAnimation = HIFI_PUBLIC_BUCKET + "animations/RightHandAnimPhilip.fbx";
var leftHandAnimation = HIFI_PUBLIC_BUCKET + "animations/LeftHandAnimPhilip.fbx";
var BALL_SIZE = 0.08;
var PADDLE_SIZE = 0.20;
var PADDLE_THICKNESS = 0.06;
var PADDLE_COLOR = { red: 184, green: 134, blue: 11 };
var BALL_COLOR = { red: 255, green: 0, blue: 0 };
var LINE_COLOR = { red: 255, green: 255, blue: 0 };
var PADDLE_OFFSET = { x: 0.05, y: 0.0, z: 0.0 };
var PADDLE_BOX_OFFSET = { x: 0.05, y: 0.0, z: 0.0 };
var HOLD_POSITION_OFFSET = { x: -0.15, y: 0.05, z: -0.05 };
var PADDLE_ORIENTATION = Quat.fromPitchYawRollDegrees(0,0,0);
var GRAVITY = 0.0;
var SPRING_FORCE = 15.0;
var lastSoundTime = 0;
var gameOn = false;
var leftHanded = false;
var leftHanded = true;
var controllerID;
if (leftHanded) {
@ -30,10 +39,6 @@ if (leftHanded) {
controllerID = 3;
}
HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/";
hitSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Collisions-ballhitsandcatches/billiards/collision1.wav");
var screenSize = Controller.getViewportDimensions();
var offButton = Overlays.addOverlay("image", {
x: screenSize.x - 48,
@ -73,7 +78,7 @@ function createEntities() {
modelURL = "http://public.highfidelity.io/models/attachments/pong_paddle.fbx";
paddleModel = Entities.addEntity(
{ type: "Model",
position: Vec3.sum(Controller.getSpatialControlPosition(controllerID), PADDLE_OFFSET),
position: Vec3.sum(Controller.getSpatialControlPosition(controllerID), PADDLE_BOX_OFFSET),
dimensions: { x: PADDLE_SIZE * 1.5, y: PADDLE_THICKNESS, z: PADDLE_SIZE * 1.25 },
color: PADDLE_COLOR,
gravity: { x: 0, y: 0, z: 0 },
@ -90,6 +95,9 @@ function createEntities() {
alpha: 1,
visible: true,
lineWidth: 2 });
MyAvatar.stopAnimation(leftHanded ? leftHandAnimation: rightHandAnimation);
MyAvatar.startAnimation(leftHanded ? leftHandAnimation: rightHandAnimation, 15.0, 1.0, false, true, 0.0, 6);
}
function deleteEntities() {
@ -97,6 +105,7 @@ function deleteEntities() {
Entities.deleteEntity(paddle);
Entities.deleteEntity(paddleModel);
Overlays.deleteOverlay(line);
MyAvatar.stopAnimation(leftHanded ? leftHandAnimation: rightHandAnimation);
}
function update(deltaTime) {
@ -120,18 +129,22 @@ function update(deltaTime) {
if (!ball.isKnownID) {
ball = Entities.identifyEntity(ball);
} else {
var paddleWorldOrientation = Quat.multiply(Quat.multiply(MyAvatar.orientation, Controller.getSpatialControlRawRotation(controllerID)), PADDLE_ORIENTATION);
var holdPosition = Vec3.sum(leftHanded ? MyAvatar.getLeftPalmPosition() : MyAvatar.getRightPalmPosition(),
Vec3.multiplyQbyV(paddleWorldOrientation, HOLD_POSITION_OFFSET));
var props = Entities.getEntityProperties(ball);
var spring = Vec3.subtract(palmPosition, props.position);
var paddleWorldOrientation = Quat.multiply(MyAvatar.orientation, Controller.getSpatialControlRawRotation(controllerID));
var spring = Vec3.subtract(holdPosition, props.position);
var springLength = Vec3.length(spring);
spring = Vec3.normalize(spring);
var ballVelocity = Vec3.sum(props.velocity, Vec3.multiply(springLength * SPRING_FORCE * deltaTime, spring));
Entities.editEntity(ball, { velocity: ballVelocity });
Overlays.editOverlay(line, { start: props.position, end: palmPosition });
Entities.editEntity(paddle, { position: palmPosition,
Overlays.editOverlay(line, { start: props.position, end: holdPosition });
Entities.editEntity(paddle, { position: holdPosition,
velocity: Controller.getSpatialControlVelocity(controllerID),
rotation: paddleWorldOrientation });
Entities.editEntity(paddleModel, { position: Vec3.sum(palmPosition, Vec3.multiplyQbyV(paddleWorldOrientation, PADDLE_OFFSET)),
Entities.editEntity(paddleModel, { position: Vec3.sum(holdPosition, Vec3.multiplyQbyV(paddleWorldOrientation, PADDLE_BOX_OFFSET)),
velocity: Controller.getSpatialControlVelocity(controllerID),
rotation: paddleWorldOrientation });
}
@ -164,6 +177,7 @@ function scriptEnding() {
deleteEntities();
}
Overlays.deleteOverlay(offButton);
MyAvatar.stopAnimation(leftHanded ? leftHandAnimation: rightHandAnimation);
}
Entities.entityCollisionWithEntity.connect(entityCollisionWithEntity);

View file

@ -1,4 +1,17 @@
// Pool Table
// Billiards.js
//
// Created by Philip Rosedale on January 21, 2015
// Copyright 2014 High Fidelity, Inc.
//
// Creates a pool table in front of you. Hold and release space ball to shoot a ball.
// Cue ball will return if falls off table. Delete and reset to restart.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/";
var tableParts = [];
var balls = [];
var cueBall;
@ -19,6 +32,10 @@ var cuePosition;
var startStroke = 0;
// Sounds to use
hitSounds = [];
hitSounds.push(SoundCache.getSound(HIFI_PUBLIC_BUCKET + "Collisions-ballhitsandcatches/billiards/collision1.wav"));
HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/";
var screenSize = Controller.getViewportDimensions();
var reticle = Overlays.addOverlay("image", {
@ -122,6 +139,7 @@ function makeBalls(pos) {
}
ballPosition.x += (BALL_GAP + Math.sqrt(3.0) / 2.0 * BALL_SIZE) * SCALE;
}
// Cue Ball
cuePosition = { x: pos.x - (LENGTH / 4.0) * SCALE, y: pos.y + HEIGHT / 2.0 + DROP_HEIGHT, z: pos.z };
cueBall = Entities.addEntity(
@ -138,6 +156,15 @@ function makeBalls(pos) {
}
function isObjectBall(id) {
for (var i; i < balls.length; i++) {
if (balls[i].id == id) {
return true;
}
}
return false;
}
function shootCue(velocity) {
var DISTANCE_FROM_CAMERA = BALL_SIZE * 5.0 * SCALE;
var camera = Camera.getPosition();
@ -213,12 +240,28 @@ function update(deltaTime) {
}
function entityCollisionWithEntity(entity1, entity2, collision) {
/*
NOT WORKING YET
if ((entity1.id == cueBall.id) || (entity2.id == cueBall.id)) {
print("Cue ball collision!");
//audioOptions.position = Vec3.sum(Camera.getPosition(), Quat.getFront(Camera.getOrientation()));
//Audio.playSound(hitSounds[0], { position: Vec3.sum(Camera.getPosition(), Quat.getFront(Camera.getOrientation())) });
}
else if (isObjectBall(entity1.id) || isObjectBall(entity2.id)) {
print("Object ball collision");
} */
}
tableCenter = Vec3.sum(MyAvatar.position, Vec3.multiply(4.0, Quat.getFront(Camera.getOrientation())));
makeTable(tableCenter);
makeBalls(tableCenter);
Entities.entityCollisionWithEntity.connect(entityCollisionWithEntity);
Script.scriptEnding.connect(cleanup);
Controller.keyPressEvent.connect(keyPressEvent);
Controller.keyReleaseEvent.connect(keyReleaseEvent);
Script.update.connect(update);

182
examples/popcorn.js Normal file
View file

@ -0,0 +1,182 @@
//
// popcorn.js
// examples
//
// Created by Philip Rosedale on January 25, 2014
// Copyright 2015 High Fidelity, Inc.
//
// Creates a bunch of physical balls trapped in a box with a rotating wall in the middle that smacks them around,
// and a periodic 'pop' force that shoots them into the air.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
var BALL_SIZE = 0.07;
var WALL_THICKNESS = 0.10;
var SCALE = 1.0;
var GRAVITY = -1.0;
var LIFETIME = 600;
var DAMPING = 0.50;
var center = Vec3.sum(MyAvatar.position, Vec3.multiply(SCALE * 3.0, Quat.getFront(Camera.getOrientation())));
var floor = Entities.addEntity(
{ type: "Box",
position: Vec3.subtract(center, { x: 0, y: SCALE / 2.0, z: 0 }),
dimensions: { x: SCALE, y: WALL_THICKNESS, z: SCALE },
color: { red: 0, green: 255, blue: 0 },
gravity: { x: 0, y: 0, z: 0 },
ignoreCollisions: false,
lifetime: LIFETIME });
var ceiling = Entities.addEntity(
{ type: "Box",
position: Vec3.sum(center, { x: 0, y: SCALE / 2.0, z: 0 }),
dimensions: { x: SCALE, y: WALL_THICKNESS, z: SCALE },
color: { red: 128, green: 128, blue: 128 },
gravity: { x: 0, y: 0, z: 0 },
ignoreCollisions: false,
visible: true,
lifetime: LIFETIME });
var wall1 = Entities.addEntity(
{ type: "Box",
position: Vec3.sum(center, { x: SCALE / 2.0, y: 0, z: 0 }),
dimensions: { x: WALL_THICKNESS, y: SCALE, z: SCALE },
color: { red: 0, green: 255, blue: 0 },
gravity: { x: 0, y: 0, z: 0 },
ignoreCollisions: false,
visible: false,
lifetime: LIFETIME });
var wall2 = Entities.addEntity(
{ type: "Box",
position: Vec3.subtract(center, { x: SCALE / 2.0, y: 0, z: 0 }),
dimensions: { x: WALL_THICKNESS, y: SCALE, z: SCALE },
color: { red: 0, green: 255, blue: 0 },
gravity: { x: 0, y: 0, z: 0 },
ignoreCollisions: false,
visible: false,
lifetime: LIFETIME });
var wall3 = Entities.addEntity(
{ type: "Box",
position: Vec3.subtract(center, { x: 0, y: 0, z: SCALE / 2.0 }),
dimensions: { x: SCALE, y: SCALE, z: WALL_THICKNESS },
color: { red: 0, green: 255, blue: 0 },
gravity: { x: 0, y: 0, z: 0 },
ignoreCollisions: false,
visible: false,
lifetime: LIFETIME });
var wall4 = Entities.addEntity(
{ type: "Box",
position: Vec3.sum(center, { x: 0, y: 0, z: SCALE / 2.0 }),
dimensions: { x: SCALE, y: SCALE, z: WALL_THICKNESS },
color: { red: 0, green: 255, blue: 0 },
gravity: { x: 0, y: 0, z: 0 },
ignoreCollisions: false,
visible: false,
lifetime: LIFETIME });
var corner1 = Entities.addEntity(
{ type: "Box",
position: Vec3.sum(center, { x: -SCALE / 2.0, y: 0, z: SCALE / 2.0 }),
dimensions: { x: WALL_THICKNESS, y: SCALE, z: WALL_THICKNESS },
color: { red: 128, green: 128, blue: 128 },
ignoreCollisions: false,
visible: true,
lifetime: LIFETIME });
var corner2 = Entities.addEntity(
{ type: "Box",
position: Vec3.sum(center, { x: -SCALE / 2.0, y: 0, z: -SCALE / 2.0 }),
dimensions: { x: WALL_THICKNESS, y: SCALE, z: WALL_THICKNESS },
color: { red: 128, green: 128, blue: 128 },
ignoreCollisions: false,
visible: true,
lifetime: LIFETIME });
var corner3 = Entities.addEntity(
{ type: "Box",
position: Vec3.sum(center, { x: SCALE / 2.0, y: 0, z: SCALE / 2.0 }),
dimensions: { x: WALL_THICKNESS, y: SCALE, z: WALL_THICKNESS },
color: { red: 128, green: 128, blue: 128 },
ignoreCollisions: false,
visible: true,
lifetime: LIFETIME });
var corner4 = Entities.addEntity(
{ type: "Box",
position: Vec3.sum(center, { x: SCALE / 2.0, y: 0, z: -SCALE / 2.0 }),
dimensions: { x: WALL_THICKNESS, y: SCALE, z: WALL_THICKNESS },
color: { red: 128, green: 128, blue: 128 },
ignoreCollisions: false,
visible: true,
lifetime: LIFETIME });
var spinner = Entities.addEntity(
{ type: "Box",
position: center,
dimensions: { x: SCALE / 1.5, y: SCALE / 3.0, z: SCALE / 8.0 },
color: { red: 255, green: 0, blue: 0 },
angularVelocity: { x: 0, y: 360, z: 0 },
angularDamping: 0.0,
gravity: { x: 0, y: 0, z: 0 },
ignoreCollisions: false,
visible: true,
lifetime: LIFETIME });
var NUM_BALLS = 70;
balls = [];
for (var i = 0; i < NUM_BALLS; i++) {
balls.push(Entities.addEntity(
{ type: "Sphere",
position: { x: center.x + (Math.random() - 0.5) * (SCALE - BALL_SIZE - WALL_THICKNESS),
y: center.y + (Math.random() - 0.5) * (SCALE - BALL_SIZE - WALL_THICKNESS) ,
z: center.z + (Math.random() - 0.5) * (SCALE - BALL_SIZE - WALL_THICKNESS) },
dimensions: { x: BALL_SIZE, y: BALL_SIZE, z: BALL_SIZE },
color: { red: Math.random() * 255, green: Math.random() * 255, blue: Math.random() * 255 },
gravity: { x: 0, y: GRAVITY, z: 0 },
ignoreCollisions: false,
damping: DAMPING,
lifetime: LIFETIME,
collisionsWillMove: true }));
}
var VEL_MAG = 2.0;
var CHANCE_OF_POP = 0.007; // 0.01;
function update(deltaTime) {
for (var i = 0; i < NUM_BALLS; i++) {
if (Math.random() < CHANCE_OF_POP) {
Entities.editEntity(balls[i], { velocity: { x: (Math.random() - 0.5) * VEL_MAG, y: Math.random() * VEL_MAG, z: (Math.random() - 0.5) * VEL_MAG }});
}
}
}
function scriptEnding() {
Entities.deleteEntity(wall1);
Entities.deleteEntity(wall2);
Entities.deleteEntity(wall3);
Entities.deleteEntity(wall4);
Entities.deleteEntity(corner1);
Entities.deleteEntity(corner2);
Entities.deleteEntity(corner3);
Entities.deleteEntity(corner4);
Entities.deleteEntity(floor);
Entities.deleteEntity(ceiling);
Entities.deleteEntity(spinner);
for (var i = 0; i < NUM_BALLS; i++) {
Entities.deleteEntity(balls[i]);
}
}
Script.scriptEnding.connect(scriptEnding);
Script.update.connect(update);