Projectile Animation too slow
Posted: Fri Dec 22, 2006 6:29 pm
I am currently using this code to animate a projectile flying through the air. The animation takes a long time to draw, however. Are there any obvious inefficiencies in this code, or do I just have to say, "That's the way it is?" It's not too slow, but could be zippier.
[/syntax]
Code: Select all
// require_once 'classes/Math.js';
ProjectileLab = {
Version: '1.0.0',
Author: 'Edward Z. Yang',
plot_interval: .0125,
animate_interval: 20,
plotProjectile: function (speed, angle, height, range) {
var interval = this.plot_interval;
if (speed == 0 && (height == 0 || angle == 0)) return Array(Array(0,0));
if (height == 0 && angle == 0) return Array(Array(0,0));
var rad = angle * Math.RpD;
var speed_x = speed * Math.cos(rad);
var speed_y = speed * Math.sin(rad); // will change
var d_x = speed_x * interval;
var d_speed_y = -4.9 * interval;
var time = range / speed_x;
var x = 0;
var y = height;
var points = Array();
for (i = 0; y >= 0; i += interval) {
x += d_x;
y += speed_y * interval;
speed_y += d_speed_y;
points.push(Array(x, y));
}
return points;
},
animateProjectile: function (id, points) {
var px_per_meter = 5;
var area = $(id);
var dots = Array();
for (i = 0; i < points.length; i++) {
var dot = document.createElement('div');
dot.className = 'dot';
dot.style.left = points[i][0] * px_per_meter + "px";
dot.style.bottom = points[i][1] * px_per_meter + "px";
dot.style.backgroundColor = '#FFF';
area.appendChild(dot);
dots.push(dot);
}
var frame = 0;
var callback = function () {
dots[frame++].style.backgroundColor = '#000';
if (frame >= dots.length) {
clearInterval(interval_id1);
//clearInterval(interval_id2);
}
};
var interval_id1 = setInterval(callback, this.animate_interval);
//var interval_id2 = setInterval(callback, this.animate_interval);
},
}