first cut at flocking birds, change willEmitVisualCallback to update

This commit is contained in:
ZappoMan 2014-03-04 14:40:49 -08:00
parent 12996454f9
commit 374af9eeb5
39 changed files with 106 additions and 86 deletions

View file

@ -36,11 +36,8 @@ public:
public slots:
void run();
void readPendingDatagrams();
signals:
void willSendAudioDataCallback();
void willSendVisualDataCallback();
private:
ScriptEngine _scriptEngine;
VoxelEditPacketSender _voxelEditSender;

View file

@ -18,7 +18,7 @@ var FACTOR = 0.75;
var countParticles = 0; // the first time around we want to create the particle and thereafter to modify it.
var particleID;
function updateParticle() {
function updateParticle(deltaTime) {
// the particle should be placed in front of the user's avatar
var avatarFront = Quat.getFront(MyAvatar.orientation);
@ -62,7 +62,7 @@ function updateParticle() {
}
// register the call back so it fires before each data send
Script.willSendVisualDataCallback.connect(updateParticle);
Script.update.connect(updateParticle);
// register our scriptEnding callback
Script.scriptEnding.connect(function scriptEnding() {});

View file

@ -103,7 +103,7 @@ Avatar.billboardURL = "https://s3-us-west-1.amazonaws.com/highfidelity-public/me
Agent.isAvatar = true;
function updateBehavior() {
function updateBehavior(deltaTime) {
if (Math.random() < CHANCE_OF_SOUND) {
playRandomSound(Avatar.position);
}
@ -142,7 +142,7 @@ function updateBehavior() {
}
}
}
Script.willSendVisualDataCallback.connect(updateBehavior);
Script.update.connect(updateBehavior);
function loadSounds() {
sounds.push(new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Cocktail+Party+Snippets/Raws/AB1.raw"));

View file

@ -20,9 +20,8 @@ var joysticksCaptured = false;
var THRUST_CONTROLLER = 0;
var VIEW_CONTROLLER = 1;
function checkCamera() {
function checkCamera(deltaTime) {
if (Camera.getMode() == "independent") {
var deltaTime = 1/60; // approximately our FPS - maybe better to be elapsed time since last call
var THRUST_MAG_UP = 800.0;
var THRUST_MAG_DOWN = 300.0;
var THRUST_MAG_FWD = 500.0;
@ -80,7 +79,7 @@ function checkCamera() {
}
}
Script.willSendVisualDataCallback.connect(checkCamera);
Script.update.connect(checkCamera);
function mouseMoveEvent(event) {
print("mouseMoveEvent event.x,y=" + event.x + ", " + event.y);

View file

@ -28,7 +28,7 @@ var clapping = new Array();
clapping[0] = false;
clapping[1] = false;
function maybePlaySound() {
function maybePlaySound(deltaTime) {
// Set the location and other info for the sound to play
var palm1Position = Controller.getSpatialControlPosition(0);
var palm2Position = Controller.getSpatialControlPosition(2);
@ -62,4 +62,4 @@ function maybePlaySound() {
}
// Connect a call back that happens every frame
Script.willSendVisualDataCallback.connect(maybePlaySound);
Script.update.connect(maybePlaySound);

View file

@ -58,7 +58,7 @@ var color = {
green: 255,
blue: 0 };
function draw() {
function draw(deltaTime) {
print("hello... draw()... currentIteration=" + currentIteration + "\n");
// on the first iteration, setup a single particle that's slowly moving
@ -150,5 +150,5 @@ function draw() {
// register the call back so it fires before each data send
print("here...\n");
Particles.setPacketsPerSecond(40000);
Script.willSendVisualDataCallback.connect(draw);
Script.update.connect(draw);
print("and here...\n");

View file

@ -16,7 +16,7 @@ for (t = 0; t < numberOfTriggers; t++) {
triggerPulled[t] = false;
}
function checkController() {
function checkController(deltaTime) {
var numberOfTriggers = Controller.getNumberOfTriggers();
var numberOfSpatialControls = Controller.getNumberOfSpatialControls();
var controllersPerTrigger = numberOfSpatialControls / numberOfTriggers;
@ -48,7 +48,7 @@ function checkController() {
}
// register the call back so it fires before each data send
Script.willSendVisualDataCallback.connect(checkController);
Script.update.connect(checkController);
function printKeyEvent(eventName, event) {
print(eventName);

View file

@ -10,8 +10,8 @@
var count = 0;
function displayCount() {
print("count =" + count);
function displayCount(deltaTime) {
print("count =" + count + " deltaTime=" + deltaTime);
count++;
}
@ -20,7 +20,7 @@ function scriptEnding() {
}
// register the call back so it fires before each data send
Script.willSendVisualDataCallback.connect(displayCount);
Script.update.connect(displayCount);
// register our scriptEnding callback
Script.scriptEnding.connect(scriptEnding);

View file

@ -31,7 +31,7 @@ var strokeSpeed = new Array();
strokeSpeed[0] = 0.0;
strokeSpeed[1] = 0.0;
function checkSticks() {
function checkSticks(deltaTime) {
for (var palm = 0; palm < 2; palm++) {
var palmVelocity = Controller.getSpatialControlVelocity(palm * 2 + 1);
var speed = length(palmVelocity);
@ -69,4 +69,4 @@ function checkSticks() {
}
// Connect a call back that happens every frame
Script.willSendVisualDataCallback.connect(checkSticks);
Script.update.connect(checkSticks);

View file

@ -42,7 +42,7 @@ var positionDelta = { x: 0.05, y: 0, z: 0 };
var particleID = Particles.addParticle(originalProperties);
function moveParticle() {
function moveParticle(deltaTime) {
if (count >= moveUntil) {
// delete it...
@ -85,5 +85,5 @@ function moveParticle() {
// register the call back so it fires before each data send
Script.willSendVisualDataCallback.connect(moveParticle);
Script.update.connect(moveParticle);

View file

@ -1329,7 +1329,7 @@ function checkControllers() {
}
}
function update() {
function update(deltaTime) {
var newWindowDimensions = Controller.getViewportDimensions();
if (newWindowDimensions.x != windowDimensions.x || newWindowDimensions.y != windowDimensions.y) {
windowDimensions = newWindowDimensions;
@ -1399,6 +1399,6 @@ function scriptEnding() {
}
Script.scriptEnding.connect(scriptEnding);
Script.willSendVisualDataCallback.connect(update);
Script.update.connect(update);
setupMenus();

View file

@ -60,7 +60,7 @@ function printProperties(properties) {
}
}
function findParticles() {
function findParticles(deltaTime) {
// run for a while, then clean up
// stop it...
@ -122,7 +122,7 @@ function findParticles() {
// register the call back so it fires before each data send
Script.willSendVisualDataCallback.connect(findParticles);
Script.update.connect(findParticles);
// register our scriptEnding callback
Script.scriptEnding.connect(scriptEnding);

View file

@ -41,7 +41,7 @@ var position = { x: 5.0, y: 0.6, z: 5.0 };
Voxels.setVoxel(position.x, 0, position.z, 0.5, 0, 0, 255);
var totalParticles = 0;
function makeFountain() {
function makeFountain(deltaTime) {
if (Math.random() < 0.10) {
//print("Made particle!\n");
var properties = {
@ -64,4 +64,4 @@ function makeFountain() {
}
}
// register the call back so it fires before each data send
Script.willSendVisualDataCallback.connect(makeFountain);
Script.update.connect(makeFountain);

View file

@ -114,7 +114,7 @@ function sendNextCells() {
var sentFirstBoard = false;
function step() {
function step(deltaTime) {
if (sentFirstBoard) {
// we've already sent the first full board, perform a step in time
updateCells();
@ -127,6 +127,6 @@ function step() {
}
print("here");
Script.willSendVisualDataCallback.connect(step);
Script.update.connect(step);
Voxels.setPacketsPerSecond(200);
print("now here");

View file

@ -73,7 +73,7 @@ function particleCollisionWithVoxel(particle, voxel) {
Audio.playSound(impactSound, audioOptions);
}
function update() {
function update(deltaTime) {
// Check for mouseLook movement, update rotation
// rotate body yaw for yaw received from mouse
@ -178,7 +178,7 @@ function scriptEnding() {
Particles.particleCollisionWithVoxel.connect(particleCollisionWithVoxel);
Script.scriptEnding.connect(scriptEnding);
Script.willSendVisualDataCallback.connect(update);
Script.update.connect(update);
Controller.mousePressEvent.connect(mousePressEvent);
Controller.mouseReleaseEvent.connect(mouseReleaseEvent);
Controller.mouseMoveEvent.connect(mouseMoveEvent);

View file

@ -29,7 +29,6 @@ var grabbingWithLeftHand = false;
var wasGrabbingWithLeftHand = false;
var EPSILON = 0.000001;
var velocity = { x: 0, y: 0, z: 0};
var deltaTime = 1/60; // approximately our FPS - maybe better to be elapsed time since last call
var THRUST_MAG_UP = 800.0;
var THRUST_MAG_DOWN = 300.0;
var THRUST_MAG_FWD = 500.0;
@ -77,7 +76,7 @@ function getAndResetGrabRotation() {
}
// handles all the grab related behavior: position (crawl), velocity (flick), and rotate (twist)
function handleGrabBehavior() {
function handleGrabBehavior(deltaTime) {
// check for and handle grab behaviors
grabbingWithRightHand = Controller.isButtonPressed(RIGHT_BUTTON_4);
grabbingWithLeftHand = Controller.isButtonPressed(LEFT_BUTTON_4);
@ -156,7 +155,7 @@ function handleGrabBehavior() {
}
// Main update function that handles flying and grabbing behaviort
function flyWithHydra() {
function flyWithHydra(deltaTime) {
var thrustJoystickPosition = Controller.getJoystickPosition(THRUST_CONTROLLER);
if (thrustJoystickPosition.x != 0 || thrustJoystickPosition.y != 0) {
@ -193,10 +192,10 @@ function flyWithHydra() {
var newPitch = MyAvatar.headPitch + (viewJoystickPosition.y * JOYSTICK_PITCH_MAG * deltaTime);
MyAvatar.headPitch = newPitch;
}
handleGrabBehavior();
handleGrabBehavior(deltaTime);
}
Script.willSendVisualDataCallback.connect(flyWithHydra);
Script.update.connect(flyWithHydra);
Controller.captureJoystick(THRUST_CONTROLLER);
Controller.captureJoystick(VIEW_CONTROLLER);

View file

@ -45,13 +45,13 @@ function releaseMovementKeys() {
}
var cameraPosition = Camera.getPosition();
function moveCamera() {
function moveCamera(update) {
if (lookingAtSomething) {
Camera.setPosition(cameraPosition);
}
}
Script.willSendVisualDataCallback.connect(moveCamera);
Script.update.connect(moveCamera);
function mousePressEvent(event) {

View file

@ -49,7 +49,7 @@ function mouseMoveEvent(event) {
}
}
function update() {
function update(deltaTime) {
if (wantDebugging) {
print("update()...");
}
@ -91,5 +91,5 @@ MyAvatar.bodyPitch = 0;
MyAvatar.bodyRoll = 0;
// would be nice to change to update
Script.willSendVisualDataCallback.connect(update);
Script.update.connect(update);
Script.scriptEnding.connect(scriptEnding);

View file

@ -43,7 +43,7 @@ function touchUpdateEvent(event) {
lastY = event.y;
}
function update() {
function update(deltaTime) {
// rotate body yaw for yaw received from mouse
var newOrientation = Quat.multiply(MyAvatar.orientation, Quat.fromVec3( { x: 0, y: yawFromMouse, z: 0 } ));
if (wantDebugging) {
@ -82,5 +82,5 @@ MyAvatar.bodyPitch = 0;
MyAvatar.bodyRoll = 0;
// would be nice to change to update
Script.willSendVisualDataCallback.connect(update);
Script.update.connect(update);
Script.scriptEnding.connect(scriptEnding);

View file

@ -12,7 +12,7 @@ var colorEdge = { r:255, g:250, b:175 };
var frame = 0;
var thisColor = color;
function moveVoxel() {
function moveVoxel(deltaTime) {
frame++;
if (frame % 3 == 0) {
// Get a new position
@ -41,4 +41,4 @@ function moveVoxel() {
Voxels.setPacketsPerSecond(300);
// Connect a call back that happens every frame
Script.willSendVisualDataCallback.connect(moveVoxel);
Script.update.connect(moveVoxel);

View file

@ -90,7 +90,7 @@ Controller.touchEndEvent.connect(touchEndEvent);
function update() {
function update(deltaTime) {
// rotate body yaw for yaw received from multitouch rotate
var newOrientation = Quat.multiply(MyAvatar.orientation, Quat.fromVec3( { x: 0, y: yawFromMultiTouch, z: 0 } ));
if (wantDebugging) {
@ -110,7 +110,7 @@ function update() {
MyAvatar.headPitch = newPitch;
pitchFromMultiTouch = 0;
}
Script.willSendVisualDataCallback.connect(update);
Script.update.connect(update);
function scriptEnding() {

View file

@ -186,7 +186,7 @@ var toolAVisible = false;
var count = 0;
// Our update() function is called at approximately 60fps, and we will use it to animate our various overlays
function update() {
function update(deltaTime) {
count++;
// every second or so, toggle the visibility our our blinking tool
@ -226,7 +226,7 @@ function update() {
// update our 3D line to go from origin to our avatar's position
Overlays.editOverlay(line3d, { end: MyAvatar.position } );
}
Script.willSendVisualDataCallback.connect(update);
Script.update.connect(update);
// The slider is handled in the mouse event callbacks.

View file

@ -13,7 +13,7 @@ for (t = 0; t < numberOfTriggers; t++) {
triggerPulled[t] = false;
}
function checkController() {
function checkController(deltaTime) {
var numberOfTriggers = Controller.getNumberOfTriggers();
var numberOfSpatialControls = Controller.getNumberOfSpatialControls();
var controllersPerTrigger = numberOfSpatialControls / numberOfTriggers;
@ -93,4 +93,4 @@ function checkController() {
// register the call back so it fires before each data send
Script.willSendVisualDataCallback.connect(checkController);
Script.update.connect(checkController);

View file

@ -99,7 +99,7 @@ var properties = {
var range = 1.0; // Distance around avatar where I can move
// Create the actual bird
var particleID = Particles.addParticle(properties);
function moveBird() {
function moveBird(deltaTime) {
// check to see if we've been running long enough that our bird is dead
var nowTimeInSeconds = new Date().getTime() / 1000;
@ -194,4 +194,4 @@ function moveBird() {
}
// register the call back so it fires before each data send
Script.willSendVisualDataCallback.connect(moveBird);
Script.update.connect(moveBird);

View file

@ -47,7 +47,7 @@ var modelAParticleID = Particles.addParticle(modelPropertiesA);
var modelBParticleID = Particles.addParticle(modelPropertiesB);
var ballParticleID = Particles.addParticle(ballProperties);
function endAfterAWhile() {
function endAfterAWhile(deltaTime) {
// stop it...
if (count >= stopAfter) {
print("calling Script.stop()");
@ -60,5 +60,5 @@ function endAfterAWhile() {
// register the call back so it fires before each data send
Script.willSendVisualDataCallback.connect(endAfterAWhile);
Script.update.connect(endAfterAWhile);

View file

@ -5,7 +5,7 @@
// First, load the clap sound from a URL
var clap = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Animals/bushtit_1.raw");
function maybePlaySound() {
function maybePlaySound(deltaTime) {
if (Math.random() < 0.01) {
// Set the location and other info for the sound to play
var options = new AudioInjectionOptions();
@ -17,4 +17,4 @@ function maybePlaySound() {
}
// Connect a call back that happens every frame
Script.willSendVisualDataCallback.connect(maybePlaySound);
Script.update.connect(maybePlaySound);

View file

@ -122,7 +122,7 @@ var velocity;
var hueAngle = 0;
var smoothedOffset;
function step() {
function step(deltaTime) {
if (stateHistory.length === 0) {
// start at a random position within the bounds, with a random velocity
position = randomVector(BOUNDS_MIN, BOUNDS_MAX);
@ -170,4 +170,4 @@ function step() {
hueAngle = (hueAngle + 1) % MAX_HUE_ANGLE;
}
Script.willSendVisualDataCallback.connect(step);
Script.update.connect(step);

View file

@ -22,7 +22,7 @@ var particleA = Particles.addParticle(
lifetime: (lengthOfRide * 60) + 1
});
function rideWithParticle() {
function rideWithParticle(deltaTime) {
if (iteration <= lengthOfRide) {
@ -46,5 +46,5 @@ function rideWithParticle() {
// register the call back so it fires before each data send
Script.willSendVisualDataCallback.connect(rideWithParticle);
Script.update.connect(rideWithParticle);

View file

@ -31,7 +31,7 @@ function init() {
}
}
function keepLooking() {
function keepLooking(deltaTime) {
//print("count =" + count);
if (count == 0) {
@ -63,7 +63,7 @@ function scriptEnding() {
}
// register the call back so it fires before each data send
Script.willSendVisualDataCallback.connect(keepLooking);
Script.update.connect(keepLooking);
// register our scriptEnding callback
Script.scriptEnding.connect(scriptEnding);

View file

@ -207,7 +207,7 @@ function displayGameOver() {
print("Game over...");
}
function update() {
function update(deltaTime) {
if (!gameOver) {
//print("updating space invaders... iteration="+iteration);
iteration++;
@ -257,7 +257,7 @@ function update() {
}
// register the call back so it fires before each data send
Script.willSendVisualDataCallback.connect(update);
Script.update.connect(update);
function cleanupGame() {
print("cleaning up game...");

View file

@ -208,7 +208,7 @@ function checkControllerSide(whichSide) {
}
function checkController() {
function checkController(deltaTime) {
var numberOfButtons = Controller.getNumberOfButtons();
var numberOfTriggers = Controller.getNumberOfTriggers();
var numberOfSpatialControls = Controller.getNumberOfSpatialControls();
@ -226,4 +226,4 @@ function checkController() {
// register the call back so it fires before each data send
Script.willSendVisualDataCallback.connect(checkController);
Script.update.connect(checkController);

View file

@ -73,7 +73,7 @@ var moved = true;
var CHANCE_OF_MOVING = 0.05;
var CHANCE_OF_TWEETING = 0.05;
function moveBird() {
function moveBird(deltaTime) {
frame++;
if (frame % 3 == 0) {
// Tweeting behavior
@ -130,4 +130,4 @@ function moveBird() {
Voxels.setPacketsPerSecond(10000);
// Connect a call back that happens every frame
Script.willSendVisualDataCallback.connect(moveBird);
Script.update.connect(moveBird);

View file

@ -70,14 +70,13 @@ function clamp(valueToClamp, minValue, maxValue) {
return Math.max(minValue, Math.min(maxValue, valueToClamp));
}
function produceCollisionSound(palm, voxelDetail) {
function produceCollisionSound(deltaTime, palm, voxelDetail) {
// Collision between finger and a voxel plays sound
var palmVelocity = Controller.getSpatialControlVelocity(palm * 2);
var speed = Vec3.length(palmVelocity);
var fingerTipPosition = Controller.getSpatialControlPosition(palm * 2 + 1);
var deltaTime = 1/60; //close enough
var LOWEST_FREQUENCY = 100.0;
var HERTZ_PER_RGB = 3.0;
var DECAY_PER_SAMPLE = 0.0005;
@ -97,9 +96,7 @@ function produceCollisionSound(palm, voxelDetail) {
Audio.startDrumSound(volume, frequency, DURATION_MAX, DECAY_PER_SAMPLE, audioOptions);
}
function update() {
var deltaTime = 1/60; //close enough
function update(deltaTime) {
// Voxel Drumming with fingertips if enabled
if (Menu.isOptionChecked("Voxel Drumming")) {
@ -111,7 +108,7 @@ function update() {
if (!isColliding[palm]) {
// Collision has just started
isColliding[palm] = true;
produceCollisionSound(palm, voxel);
produceCollisionSound(deltaTime, palm, voxel);
// Set highlight voxel
Overlays.editOverlay(highlightVoxel,
@ -156,7 +153,7 @@ function update() {
} // palm loop
} // menu item check
}
Script.willSendVisualDataCallback.connect(update);
Script.update.connect(update);
function scriptEnding() {
Overlays.deleteOverlay(highlightVoxel);

View file

@ -11,6 +11,8 @@
#include <glm/gtx/vector_angle.hpp>
#include <QDebug>
#include <OctreeConstants.h>
#include <SharedUtil.h>
#include "Quat.h"
@ -56,3 +58,7 @@ glm::quat Quat::mix(const glm::quat& q1, const glm::quat& q2, float alpha) {
return safeMix(q1, q2, alpha);
}
void Quat::print(const QString& lable, const glm::quat& q) {
qDebug() << qPrintable(lable) << q.x << "," << q.y << "," << q.z << "," << q.w;
}

View file

@ -13,7 +13,9 @@
#define __hifi__Quat__
#include <glm/gtc/quaternion.hpp>
#include <QtCore/QObject>
#include <QObject>
#include <QString>
/// Scriptable interface a Quaternion helper class object. Used exclusively in the JavaScript API
class Quat : public QObject {
@ -30,6 +32,7 @@ public slots:
glm::vec3 safeEulerAngles(const glm::quat& orientation);
glm::quat angleAxis(float angle, const glm::vec3& v);
glm::quat mix(const glm::quat& q1, const glm::quat& q2, float alpha);
void print(const QString& lable, const glm::quat& q);
};
#endif /* defined(__hifi__Quat__) */

View file

@ -215,6 +215,8 @@ void ScriptEngine::run() {
int thisFrame = 0;
NodeList* nodeList = NodeList::getInstance();
qint64 lastUpdate = usecTimestampNow();
while (!_isFinished) {
int usecToSleep = usecTimestamp(&startTime) + (thisFrame++ * VISUAL_DATA_CALLBACK_USECS) - usecTimestampNow();
@ -259,7 +261,10 @@ void ScriptEngine::run() {
nodeList->broadcastToNodes(avatarPacket, NodeSet() << NodeType::AvatarMixer);
}
emit willSendVisualDataCallback();
qint64 now = usecTimestampNow();
float deltaTime = (float)(now - lastUpdate)/(float)USECS_PER_SECOND;
emit update(deltaTime);
lastUpdate = now;
if (_engine.hasUncaughtException()) {
int line = _engine.uncaughtExceptionLineNumber();

View file

@ -71,8 +71,7 @@ public slots:
void clearTimeout(QObject* timer) { stopTimer(reinterpret_cast<QTimer*>(timer)); }
signals:
void willSendAudioDataCallback();
void willSendVisualDataCallback();
void update(float deltaTime);
void scriptEnding();
void finished(const QString& fileNameString);
void cleanupMenuItem(const QString& menuItemString);

View file

@ -9,10 +9,12 @@
//
//
#include <QDebug>
#include "Vec3.h"
glm::vec3 Vec3::multiply(const glm::vec3& v1, const glm::vec3& v2) {
return v1 * v2;
glm::vec3 Vec3::cross(const glm::vec3& v1, const glm::vec3& v2) {
return glm::cross(v1,v2);
}
glm::vec3 Vec3::multiply(const glm::vec3& v1, float f) {
@ -29,6 +31,15 @@ glm::vec3 Vec3::sum(const glm::vec3& v1, const glm::vec3& v2) {
glm::vec3 Vec3::subtract(const glm::vec3& v1, const glm::vec3& v2) {
return v1 - v2;
}
float Vec3::length(const glm::vec3& v) {
return glm::length(v);
}
glm::vec3 Vec3::normalize(const glm::vec3& v) {
return glm::normalize(v);
}
void Vec3::print(const QString& lable, const glm::vec3& v) {
qDebug() << qPrintable(lable) << v.x << "," << v.y << "," << v.z;
}

View file

@ -14,19 +14,23 @@
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
#include <QtCore/QObject>
#include <QObject>
#include <QString>
/// Scriptable interface a Vec3ernion helper class object. Used exclusively in the JavaScript API
class Vec3 : public QObject {
Q_OBJECT
public slots:
glm::vec3 multiply(const glm::vec3& v1, const glm::vec3& v2);
glm::vec3 cross(const glm::vec3& v1, const glm::vec3& v2);
glm::vec3 multiply(const glm::vec3& v1, float f);
glm::vec3 multiplyQbyV(const glm::quat& q, const glm::vec3& v);
glm::vec3 sum(const glm::vec3& v1, const glm::vec3& v2);
glm::vec3 subtract(const glm::vec3& v1, const glm::vec3& v2);
float length(const glm::vec3& v);
glm::vec3 normalize(const glm::vec3& v);
void print(const QString& lable, const glm::vec3& v);
};