Skip to content

Instantly share code, notes, and snippets.

@jherico
Created June 30, 2015 23:44
Show Gist options
  • Save jherico/6ac62a55a48a56ff74dc to your computer and use it in GitHub Desktop.
Save jherico/6ac62a55a48a56ff74dc to your computer and use it in GitHub Desktop.
//
// planets.js
//
// Created by Philip Rosedale on January 26, 2015
// Copyright 2015 High Fidelity, Inc.
//
// Some planets are created in front of you. A physical object shot or thrown between them will move
// correctly.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
var MAX_RANGE = 75.0;
var MAX_TRANSLATION = MAX_RANGE / 20.0;
var LIFETIME = 600;
var DAMPING = 0.0;
var G = 3.0;
// In this section, setup where you want your 'Planets' that will exert gravity on the
// smaller test particles. Use the first one for the simplest 'planets around sun' simulation.
// Add additional planets to make things a lot more complicated!
var planetTypes = [];
planetTypes.push({ radius: 10, red: 0, green: 0, blue: 255, x: 0.0, y:0, z: 0.0 });
var center = Vec3.sum(MyAvatar.position, Vec3.multiply(MAX_RANGE, Quat.getFront(Camera.getOrientation())));
var NUM_INITIAL_PARTICLES = 100;
var PARTICLE_MIN_SIZE = 0.50;
var PARTICLE_MAX_SIZE = 4.50;
var INITIAL_VELOCITY = 5.0;
var DEGREES_TO_RADIANS = Math.PI / 180.0;
var planets = [];
var particles = [];
// Create planets that will extert gravity on test particles
for (var i = 0; i < planetTypes.length; i++) {
// NOTE: rotationalVelocity is in radians/sec
var position = { x: planetTypes[i].x, y: planetTypes[i].y, z: planetTypes[i].z };
position = Vec3.multiply(MAX_RANGE / 2, position);
position = Vec3.sum(center, position);
planets.push(Entities.addEntity({
type: "Sphere",
position: position,
dimensions: { x: planetTypes[i].radius, y: planetTypes[i].radius, z: planetTypes[i].radius },
color: { red: planetTypes[i].red, green: planetTypes[i].green, blue: planetTypes[i].blue },
gravity: { x: 0, y: 0, z: 0 },
angularVelocity: { x: 0, y: 0, z: 0 },
angularDamping: 0.0,
ignoreCollisions: true,
lifetime: LIFETIME,
collisionsWillMove: false
}));
}
Script.setTimeout(createParticles, 1000);
function createParticles() {
// Create initial test particles that will move according to gravity from the planets
for (var i = 0; i < NUM_INITIAL_PARTICLES; i++) {
var radius = PARTICLE_MIN_SIZE + Math.random() * PARTICLE_MAX_SIZE;
var gray = Math.random() * 155;
var whichPlanet = 0;
var position = { x: 10 , y: i * 3, z: 0 };
var separation = Vec3.length(position);
var color = { red: 100 + gray, green: 100 + gray, blue: 100 + gray };
if (i == 0) {
color = { red: 255, green: 0, blue: 0 };
radius = 10
}
particles.push(Entities.addEntity({
type: "Sphere",
position: Vec3.sum(center, position),
dimensions: { x: radius, y: radius, z: radius },
color: color,
gravity: { x: 0, y: 0, z: 0 },
angularVelocity: { x: 0, y: 0, z: 0 },
velocity: { x: 0, y: 0, z: 0 },
ignoreCollisions: true,
damping: DAMPING,
lifetime: LIFETIME,
collisionsWillMove: false
}));
}
Script.update.connect(update);
}
function scriptEnding() {
for (var i = 0; i < planetTypes.length; i++) {
Entities.deleteEntity(planets[i]);
}
for (var i = 0; i < particles.length; i++) {
Entities.deleteEntity(particles[i]);
}
}
var totalTime = 0.0;
function update(deltaTime) {
// Apply gravitational force from planets
totalTime += deltaTime;
var planetProperties = Entities.getEntityProperties(planets[0]);
var center = planetProperties.position;
var particlePos = Entities.getEntityProperties(particles[0]).position;
var relativePos = Vec3.subtract(particlePos.position, center);
for (var t = 0; t < particles.length; t++) {
var thetaDelta = (Math.PI * 2.0 / NUM_INITIAL_PARTICLES) * t;
var y = Math.sin(totalTime + thetaDelta) * 10.0;
var x = Math.cos(totalTime + thetaDelta) * 10.0;
var newBasePos = Vec3.sum({ x: 0, y: y, z: x }, center);
Entities.editEntity(particles[t], { position: newBasePos});
}
}
Script.scriptEnding.connect(scriptEnding);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment