[WIP] New Demo ✨
This commit is contained in:
parent
5af08e5cd7
commit
fc7d76637d
126
build.js
126
build.js
@ -507,15 +507,113 @@
|
||||
|
||||
|
||||
|
||||
System.register("src/lib/play", ["src/lib/frames", "src/lib/gravity", "src/main"], function ($__export) {
|
||||
System.register("src/lib/forces/global-gravity", ["src/lib/vector", "src/main"], function ($__export) {
|
||||
"use strict";
|
||||
var __moduleName = "src/lib/forces/global-gravity";
|
||||
var Vector, Bolt;
|
||||
function GlobalGravity(mass) {
|
||||
var g = arguments[1] !== void 0 ? arguments[1] : Bolt.ugravitation || 10;
|
||||
this.force = new Vector(0, -mass * g, 0);
|
||||
}
|
||||
return {
|
||||
setters: [function (m) {
|
||||
Vector = m["default"];
|
||||
}, function (m) {
|
||||
Bolt = m["default"];
|
||||
}],
|
||||
execute: function () {
|
||||
GlobalGravity.prototype.apply = function (particle, duration) {
|
||||
particle.acceleration.add(this.force);
|
||||
};
|
||||
$__export("default", GlobalGravity);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
|
||||
System.register("src/lib/forces/gravity", ["src/lib/vector", "src/main"], function ($__export) {
|
||||
"use strict";
|
||||
var __moduleName = "src/lib/forces/gravity";
|
||||
var Vector, Bolt;
|
||||
function Gravity(o1, o2) {
|
||||
var mass = o1.mass * o2.mass;
|
||||
var g = Bolt.ugravitation || 10;
|
||||
var acc = mass * g;
|
||||
var distance = new Vector(o1.position.x - o2.position.x, o1.position.y - o2.position.y, o1.position.z - o2.position.z);
|
||||
this.force = new Vector(acc / (distance.x * distance.x), acc / (distance.y * distance.y), acc / (distance.z * distance.z));
|
||||
}
|
||||
return {
|
||||
setters: [function (m) {
|
||||
Vector = m["default"];
|
||||
}, function (m) {
|
||||
Bolt = m["default"];
|
||||
}],
|
||||
execute: function () {
|
||||
Gravity.prototype.apply = function (particle, duration) {
|
||||
particle.acceleration.add(this.force);
|
||||
};
|
||||
$__export("default", Gravity);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
|
||||
System.register("src/lib/force", ["src/lib/vector", "src/lib/forces/global-gravity", "src/lib/forces/gravity"], function ($__export) {
|
||||
"use strict";
|
||||
var __moduleName = "src/lib/force";
|
||||
var Vector, GlobalGravity, Gravity, Force;
|
||||
return {
|
||||
setters: [function (m) {
|
||||
Vector = m["default"];
|
||||
}, function (m) {
|
||||
GlobalGravity = m["default"];
|
||||
}, function (m) {
|
||||
Gravity = m["default"];
|
||||
}],
|
||||
execute: function () {
|
||||
Force = {
|
||||
registry: [],
|
||||
generators: {
|
||||
GlobalGravity: GlobalGravity,
|
||||
Gravity: Gravity
|
||||
},
|
||||
add: function (particle, force) {
|
||||
this.registry.push([particle, force]);
|
||||
return this.forces;
|
||||
},
|
||||
remove: function (particle, force) {
|
||||
var reg = this.registry;
|
||||
this.registry.forEach(function (el, i) {
|
||||
if (el[0] === particle && el[1] === force) reg.splice(i, 1);
|
||||
});
|
||||
},
|
||||
clear: function () {
|
||||
this.registry.length = 0;
|
||||
},
|
||||
run: function (duration) {
|
||||
this.registry.forEach(function (el) {
|
||||
el[1].apply(el[0], duration);
|
||||
});
|
||||
}
|
||||
};
|
||||
$__export("default", Force);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
|
||||
System.register("src/lib/play", ["src/lib/frames", "src/lib/force", "src/main"], function ($__export) {
|
||||
"use strict";
|
||||
var __moduleName = "src/lib/play";
|
||||
var Frames, Gravity, Bolt, reqAnimFrame, cancelAnimFrame, Play;
|
||||
var Frames, Force, Bolt, reqAnimFrame, cancelAnimFrame, Play;
|
||||
return {
|
||||
setters: [function (m) {
|
||||
Frames = m["default"];
|
||||
}, function (m) {
|
||||
Gravity = m["default"];
|
||||
Force = m["default"];
|
||||
}, function (m) {
|
||||
Bolt = m["default"];
|
||||
}],
|
||||
@ -532,13 +630,14 @@
|
||||
len = Bolt.objects.length; i < len; i++) {
|
||||
var object = Bolt.objects[i];
|
||||
if (Bolt.configs.globalGravity) {
|
||||
var force = Gravity.global(object.inverseMass);
|
||||
object.position.add(object.velocity.clone().scalar(Frames.elapsed));
|
||||
var acc = object.acceleration.clone();
|
||||
acc.add(force);
|
||||
object.velocity.add(acc.scalar(Frames.elapsed));
|
||||
object.velocity.scalar(Math.pow(object.damping, Frames.elapsed));
|
||||
var gravity = new Force.generators.GlobalGravity(object.mass);
|
||||
Force.add(object, gravity);
|
||||
}
|
||||
object.position.add(object.velocity.clone().scalar(Frames.elapsed));
|
||||
object.acceleration.scalar(Frames.elapsed);
|
||||
object.velocity.add(object.acceleration);
|
||||
Force.run();
|
||||
Force.clear();
|
||||
}
|
||||
if (fn) fn.apply(this, arguments);
|
||||
loop();
|
||||
@ -561,10 +660,10 @@
|
||||
|
||||
|
||||
|
||||
System.register("src/main", ["src/lib/vector", "src/lib/particle", "src/lib/gravity", "src/lib/frames", "src/lib/play"], function ($__export) {
|
||||
System.register("src/main", ["src/lib/vector", "src/lib/particle", "src/lib/gravity", "src/lib/frames", "src/lib/play", "src/lib/force"], function ($__export) {
|
||||
"use strict";
|
||||
var __moduleName = "src/main";
|
||||
var Vector, Particle, Gravity, Frames, Play, Bolt;
|
||||
var Vector, Particle, Gravity, Frames, Play, Force, Bolt;
|
||||
return {
|
||||
setters: [function (m) {
|
||||
Vector = m["default"];
|
||||
@ -576,6 +675,8 @@
|
||||
Frames = m["default"];
|
||||
}, function (m) {
|
||||
Play = m["default"];
|
||||
}, function (m) {
|
||||
Force = m["default"];
|
||||
}],
|
||||
execute: function () {
|
||||
Bolt = {
|
||||
@ -591,7 +692,8 @@
|
||||
Particle: Particle,
|
||||
Gravity: Gravity,
|
||||
Frames: Frames,
|
||||
Play: Play
|
||||
Play: Play,
|
||||
Force: Force
|
||||
};
|
||||
window.Bolt = Bolt;
|
||||
$__export("default", Bolt);
|
||||
|
File diff suppressed because one or more lines are too long
@ -55,7 +55,7 @@ Bolt.Play.start(function() {
|
||||
fireworks.push(new Firework({
|
||||
type: 0,
|
||||
position: Bolt.Vector(window.innerWidth/4, 0, 0),
|
||||
mass: 4
|
||||
mass: 2
|
||||
}));
|
||||
count = 0;
|
||||
}
|
||||
@ -79,7 +79,7 @@ Bolt.Play.start(function() {
|
||||
type: 1,
|
||||
position: fw.particle.position.clone(),
|
||||
velocity: vel,
|
||||
mass: 2
|
||||
mass: 1
|
||||
});
|
||||
fireworks.push(childFirework);
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Bolt</title>
|
||||
<title>Bolt | Fireworks Demo</title>
|
||||
</head>
|
||||
<body style="margin: 0">
|
||||
<p style="position: absolute;top:5px;left:10px;" id='fps'></p>
|
||||
|
68
demos/mouse-gravitation/demo.js
Normal file
68
demos/mouse-gravitation/demo.js
Normal file
@ -0,0 +1,68 @@
|
||||
var world = new LiThree.World(),
|
||||
renderer = new LiThree.WebGLRenderer(window.innerWidth, window.innerHeight, world),
|
||||
camera = renderer.camera;
|
||||
|
||||
camera.position.z -= 10;
|
||||
|
||||
var light = new LiThree.Light.Point();
|
||||
light.diffuseColor.hex = '#16A086';
|
||||
light.position.x = -10;
|
||||
light.position.z = -20;
|
||||
light.position.y = 0;
|
||||
world.add(light);
|
||||
|
||||
// renderer.canvas.style.setProperty('background', 'black');
|
||||
|
||||
|
||||
var mouse = new Bolt.Particle({
|
||||
position: Bolt.Vector(0, 0, 0),
|
||||
mass: 1
|
||||
});
|
||||
|
||||
var middle = new Bolt.Vector(window.innerWidth / 2, window.innerHeight / 2, 0);
|
||||
|
||||
for(var i = 0; i < 20; i++) {
|
||||
var p = new Bolt.Particle({
|
||||
position: middle.clone(),
|
||||
mass: 1
|
||||
});
|
||||
|
||||
var sphere = new LiThree.ObjectFactory.Sphere(50, 20, 20);
|
||||
sphere.color.rgb(0.2, 0.2, 0.2);
|
||||
|
||||
world.add(sphere);
|
||||
|
||||
p.view = sphere;
|
||||
}
|
||||
|
||||
renderer.initShapes();
|
||||
|
||||
function syncPositions(particle) {
|
||||
particle.view.position.x = particle.position.x;
|
||||
particle.view.position.y = particle.position.y;
|
||||
particle.view.position.z = particle.position.z;
|
||||
}
|
||||
|
||||
Bolt.configs.globalGravity = false;
|
||||
|
||||
Bolt.Play.start(function() {
|
||||
// i = 1 Ignore mouse
|
||||
for(var i = 1, len = Bolt.objects.length; i < len; i++) {
|
||||
var p = Bolt.objects[i];
|
||||
|
||||
// var mouseGravity = new Bolt.Force.generators.Gravity(p, mouse);
|
||||
|
||||
// Bolt.Force.add(p, mouseGravity);
|
||||
|
||||
syncPositions(p);
|
||||
}
|
||||
|
||||
renderer.draw();
|
||||
});
|
||||
|
||||
document.body.appendChild(renderer.canvas);
|
||||
|
||||
renderer.canvas.addEventListener('mousemove', function(e) {
|
||||
mouse.position.x = e.pageX;
|
||||
mouse.position.y = e.pageY;
|
||||
});
|
14
demos/mouse-gravitation/index.html
Normal file
14
demos/mouse-gravitation/index.html
Normal file
@ -0,0 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Bolt | Mouse Gravitation Demo</title>
|
||||
</head>
|
||||
<body style="margin: 0">
|
||||
<p style="position: absolute;top:5px;left:10px;" id='fps'></p>
|
||||
|
||||
<script src='../../build.js'></script>
|
||||
<script src='lithree.min.js'></script>
|
||||
<script src='demo.js'></script>
|
||||
</body>
|
||||
</html>
|
2
demos/mouse-gravitation/lithree.min.js
vendored
Normal file
2
demos/mouse-gravitation/lithree.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
33
src/lib/force.js
Normal file
33
src/lib/force.js
Normal file
@ -0,0 +1,33 @@
|
||||
import Vector from './vector';
|
||||
import GlobalGravity from './forces/global-gravity';
|
||||
import Gravity from './forces/gravity';
|
||||
|
||||
var Force = {
|
||||
registry: [],
|
||||
generators: {
|
||||
GlobalGravity: GlobalGravity,
|
||||
Gravity: Gravity
|
||||
},
|
||||
add: function(particle, force) {
|
||||
this.registry.push([particle, force]);
|
||||
|
||||
return this.forces;
|
||||
},
|
||||
remove: function(particle, force) {
|
||||
var reg = this.registry;
|
||||
this.registry.forEach(function(el, i) {
|
||||
if(el[0] === particle && el[1] === force) reg.splice(i, 1);
|
||||
});
|
||||
},
|
||||
clear: function() {
|
||||
this.registry.length = 0;
|
||||
},
|
||||
run: function(duration) {
|
||||
this.registry.forEach(function(el) {
|
||||
// force.run(particle, duration);
|
||||
el[1].apply(el[0], duration);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export default Force;
|
12
src/lib/forces/global-gravity.js
Normal file
12
src/lib/forces/global-gravity.js
Normal file
@ -0,0 +1,12 @@
|
||||
import Vector from '../vector';
|
||||
import Bolt from '../../main';
|
||||
|
||||
function GlobalGravity(mass, g = Bolt.ugravitation || 10) {
|
||||
this.force = new Vector(0, -mass*g, 0);
|
||||
}
|
||||
|
||||
GlobalGravity.prototype.apply = function(particle, duration) {
|
||||
particle.acceleration.add(this.force);
|
||||
};
|
||||
|
||||
export default GlobalGravity;
|
26
src/lib/forces/gravity.js
Normal file
26
src/lib/forces/gravity.js
Normal file
@ -0,0 +1,26 @@
|
||||
import Vector from '../vector';
|
||||
import Bolt from '../../main';
|
||||
|
||||
function Gravity(o1, o2) {
|
||||
var mass = o1.mass * o2.mass;
|
||||
var g = Bolt.ugravitation || 10;
|
||||
var acc = mass*g;
|
||||
|
||||
var distance = new Vector(
|
||||
o1.position.x - o2.position.x,
|
||||
o1.position.y - o2.position.y,
|
||||
o1.position.z - o2.position.z
|
||||
);
|
||||
|
||||
this.force = new Vector(
|
||||
acc / (distance.x*distance.x),
|
||||
acc / (distance.y*distance.y),
|
||||
acc / (distance.z*distance.z)
|
||||
);
|
||||
}
|
||||
|
||||
Gravity.prototype.apply = function(particle, duration) {
|
||||
particle.acceleration.add(this.force);
|
||||
};
|
||||
|
||||
export default Gravity;
|
@ -30,6 +30,6 @@ Object.defineProperties(Particle.prototype, {
|
||||
|
||||
Particle.prototype.destroy = function() {
|
||||
return Bolt.objects.splice(Bolt.objects.indexOf(this), 1);
|
||||
}
|
||||
};
|
||||
|
||||
export default Particle;
|
@ -1,5 +1,5 @@
|
||||
import Frames from './frames';
|
||||
import Gravity from './gravity';
|
||||
import Force from './force';
|
||||
import Bolt from '../main';
|
||||
|
||||
|
||||
@ -22,29 +22,24 @@ var Play = {
|
||||
|
||||
(function loop() {
|
||||
reqAnimFrame(function(now) {
|
||||
// ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
// ctx.fillStyle = 'black';
|
||||
// ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||
var avg = Frames.tick(now);
|
||||
|
||||
for(var i = 0, len = Bolt.objects.length; i < len; i++) {
|
||||
var object = Bolt.objects[i];
|
||||
|
||||
if(Bolt.configs.globalGravity) {
|
||||
var force = Gravity.global(object.inverseMass);
|
||||
var gravity = new Force.generators.GlobalGravity(object.mass);
|
||||
Force.add(object, gravity);
|
||||
}
|
||||
|
||||
object.position.add(object.velocity.clone().scalar(Frames.elapsed));
|
||||
// object.velocity.scalar(Math.pow(object.damping, Frames.elapsed));
|
||||
|
||||
var acc = object.acceleration.clone();
|
||||
acc.add(force);
|
||||
object.velocity.add(acc.scalar(Frames.elapsed));
|
||||
object.velocity.scalar(Math.pow(object.damping, Frames.elapsed));
|
||||
object.acceleration.scalar(Frames.elapsed);
|
||||
object.velocity.add(object.acceleration);
|
||||
|
||||
// ctx.beginPath();
|
||||
// ctx.fillStyle = object.color;
|
||||
// ctx.arc(object.position.x, object.position.y, object.mass, 0, 2*Math.PI);
|
||||
// ctx.fill();
|
||||
}
|
||||
Force.run();
|
||||
Force.clear();
|
||||
}
|
||||
|
||||
if(fn) fn.apply(this, arguments);
|
||||
|
@ -3,6 +3,7 @@ import Particle from './lib/particle';
|
||||
import Gravity from './lib/gravity';
|
||||
import Frames from './lib/frames';
|
||||
import Play from './lib/play';
|
||||
import Force from './lib/force';
|
||||
|
||||
var Bolt = {
|
||||
configs: {
|
||||
@ -22,7 +23,8 @@ var Bolt = {
|
||||
Particle: Particle,
|
||||
Gravity: Gravity,
|
||||
Frames: Frames,
|
||||
Play: Play
|
||||
Play: Play,
|
||||
Force: Force
|
||||
};
|
||||
|
||||
window.Bolt = Bolt;
|
||||
|
Loading…
Reference in New Issue
Block a user