Итак, я работаю над самообучением Canvas (HTML5), и у меня закодирована большая часть простого игрового движка. Это 2-е представление космической сцены (планеты, звезды, небесные тела и т. Д.). У моего класса "Sprite" по умолчанию есть слушатель фрейма, такой как:
«baseClass» содержит функцию, которая разрешает наследование и применяет «a» к «this.a». Итак, "var aTest = new Sprite ({foo: 'bar'});" сделает "aTest.foo = 'bar'". Вот так я выставляю свои объекты друг другу.
Sprite = baseClass.extend({
init: function(a){
baseClass.init(this, a);
this.fields = new Array(); // list of fields of gravity one is in. Not sure if this is a good idea.
this.addFL(function(tick){ // this will change to be independent of framerate soon.
// gobjs is an array of all the Sprite objects in the "world".
for(i = 0; i < gobjs.length; i++){
// Make sure its got setup correctly, make sure it -wants- gravity, and make sure it's not -this- sprite.
if(typeof(gobjs[i].a) != undefined && !gobjs[i].a.ignoreGravity && gobjs[i].id != this.id){
// Check if it's within a certain range (obviously, gravity doesn't work this way... But I plan on having a large "space" area,
// And I can't very well have all objects accounted for at all times, can I?
if(this.distanceTo(gobjs[i]) < this.a.size*10 && gobjs[i].fields.indexOf(this.id) == -1){
gobjs[i].fields.push(this.id);
}
}
}
for(i = 0; i < this.fields.length; i++){
distance = this.distanceTo(gobjs[this.fields[i]]);
angletosun = this.angleTo(gobjs[this.fields[i]])*(180/Math.PI); // .angleTo works very well, returning the angle in radians, which I convert to degrees here.
// I have no idea what should happen here, although through trial and error (and attempting to read Maths papers on gravity (eeeeek!)), this sort of mimics gravity.
// angle is its orientation, currently I assign a constant velocity to one of my objects, and leave the other static (it ignores gravity, but still emits it).
// This cant be right, because it just _sets_ the angle regardless of whatever it was.
// This is where we need to find "the average of the forces".
this.a.angle = angletosun+(75+(distance*-1)/5); //todo: omg learn math
if(this.distanceTo(gobjs[this.fields[i]]) > gobjs[this.fields[i]].a.size*10){
this.fields.splice(i); // out of range, stop effecting.
}
}
});
//draw objects based on new position (from fixed velocity and angle).
}
});
Спасибо заранее. Настоящий трюк в том, что одна строка, и, кстати, я знаю, что в этом нет никакого смысла. Степени + расстояние = отказ.
this.a.angle = angletosun+(75+(distance*-1)/5);
Это больше вопрос физики, чем Javascript, но я искал, искал и прочитал way во многих вики-статьях по орбитальной математике. Это пролетает над моей головой очень быстро.