Skip to content

Instantly share code, notes, and snippets.

@SeijiEmery
Last active May 1, 2018 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeijiEmery/2a01a04a7d21e3408f17eb20be539d0e to your computer and use it in GitHub Desktop.
Save SeijiEmery/2a01a04a7d21e3408f17eb20be539d0e to your computer and use it in GitHub Desktop.
// harmonicOscillator.js
//
// Created by Philip Rosedale on May 5, 2015
// Copyright 2015 High Fidelity, Inc.
//
// An object moves around the edge of a disc while changing color.
// The script is continuously updating position, velocity, rotation, and color.
// The movement should appear perfectly smooth to someone else,
// provided their network connection is good.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
var fwd = Camera.orientation.fwd;
var center = Camera.position.add(fwd.multiply(3.0));
var axis = Vec3.UP;
var amplitude = 1.0;
var omega = 2.0 * Math.PI / 32.0; // radians/sec
var ball = new Entity({
type: "Box",
position: center,
dimensions: new Vec3(0.1, 0.1, 0.1);
color: new Color("#ff00ff"),
dynamic: false,
collisionless: true
});
var disc = new Entity({
type: "Sphere",
position: center,
dimensions: new Vec3(0.8, 1.0 / 20, 0.8).multiply(amplitude),
color: new Color("#7f7f7f")
});
var time = 0.0;
function update(deltaTime) {
time += deltaTime;
var tw = time * omega;
var s = Math.sin(tw);
var c = Math.cos(tw);
var angle = tw * 180.0 / Math.PI; // degrees
var factor = 0.5 * (s + 1.0);
ball.update({
color: new Vec3(factor, 1.0 - factor, 0.0).toColor(),
position: new Vec3(s, 0.0, c).multiply(amplitude).add(center),
velocity: new Vec3(c, 0.0, -s).multiply(amplitude * omega),
rotation: axis.toRotation(angle),
});
}
function scriptEnding() {
ball.destroy();
disc.destroy();
}
Script.update.connect(update);
Script.scriptEnding.connect(scriptEnding);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment