Skip to content

Instantly share code, notes, and snippets.

@usagi
Created April 14, 2012 11:07
Show Gist options
  • Select an option

  • Save usagi/2383615 to your computer and use it in GitHub Desktop.

Select an option

Save usagi/2383615 to your computer and use it in GitHub Desktop.
var vector = function(x, y){
switch(arguments.length){
case 0: x = 0;
case 1: y = 0;
}
this.x = x;
this.y = y;
};
var my_object = function(p, v){
switch(arguments.length){
case 0: p = new vector();
case 1: v = new vector();
}
this.position = p;
this.velocity = v;
};
my_object.prototype = {
update: function(t){
this.position.x += this.velocity.x * t;
this.position.y += this.velocity.y * t;
}
};
var world = function(){};
world.prototype = {
time: 0,
objects: [],
append: function(o){
this.objects.push(o);
},
update: function(t){
var os = this.objects;
for (k in os)
os[k].update(t);
this.time += t;
},
dump: function(){
var r = '=== dump of world ===\n';
r += 'time: ' + this.time + '[sec]\n';
var os = this.objects;
for (k in os){
var o = os[k];
r += 'os[' + k +
'] p(' + o.position.x + ', ' + o.position.y +
') v(' + o.velocity.x + ', ' + o.velocity.y +
')\n'
;
}
return r;
},
};
var main = function(){
var w;
var initialize = function() {
w = new world();
var os = [
new my_object(),
new my_object(
new vector(3,3)
),
new my_object(
new vector(0,0),
new vector(1,2)
),
new my_object(
new vector(3.5,-4.0),
new vector(1.1, 0.5)
),
new my_object(
new vector(-10.0 ,-10.0 ),
new vector( 1.7320504, 1.41421356)
),
];
for (k in os)
w.append(os[k]);
};
var test = function() {
console.log(w.dump());
w.update(1.0);
console.log(w.dump());
w.update(2.0);
console.log(w.dump());
w.update(3.0);
console.log(w.dump());
};
initialize();
test();
};
main();
@usagi
Copy link
Copy Markdown
Author

usagi commented Apr 14, 2012

=== dump of world ===
time: 0[sec]
os[0] p(0, 0) v(0, 0)
os[1] p(3, 3) v(0, 0)
os[2] p(0, 0) v(1, 2)
os[3] p(3.5, -4) v(1.1, 0.5)
os[4] p(-10, -10) v(1.7320504, 1.41421356)

=== dump of world ===
time: 1[sec]
os[0] p(0, 0) v(0, 0)
os[1] p(3, 3) v(0, 0)
os[2] p(1, 2) v(1, 2)
os[3] p(4.6, -3.5) v(1.1, 0.5)
os[4] p(-8.2679496, -8.58578644) v(1.7320504, 1.41421356)

=== dump of world ===
time: 3[sec]
os[0] p(0, 0) v(0, 0)
os[1] p(3, 3) v(0, 0)
os[2] p(3, 6) v(1, 2)
os[3] p(6.8, -2.5) v(1.1, 0.5)
os[4] p(-4.803848799999999, -5.757359319999999) v(1.7320504, 1.41421356)

=== dump of world ===
time: 6[sec]
os[0] p(0, 0) v(0, 0)
os[1] p(3, 3) v(0, 0)
os[2] p(6, 12) v(1, 2)
os[3] p(10.1, -1) v(1.1, 0.5)
os[4] p(0.39230240000000105, -1.514718639999999) v(1.7320504, 1.41421356)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment