var bulletSpeed = 10;
var enemyVec = vec_sub(targetSprite.getCenter(), originSprite.getCenter());
var dist = vec_mag(enemyVec);
enemyVec = vec_add(enemyVec, vec_mul(targetSprite.getSpeed(), dist/bulletSpeed));
var bulletTrajectory = vec_mul(vec_normal(enemyVec), bulletSpeed);
bulletSprite.speedX = bulletTrajectory.x;
bulletSprite.speedY = bulletTrajectory.y;
sprite.prototype.getCenter = function() {
return {
x: this.x+(this.width/2),
y: this.y+(this.height/2)
};
};
sprite.prototype.getSpeed = function() {
return {
x: this.speedX,
y: this.speedY
};
};
function vec_mag(vec) {
return Math.sqrt( vec.x * vec.x + vec.y * vec.y);
}
function vec_sub(a,b) {
return { x: a.x-b.x, y: a.y-b.y };
}
function vec_add(a,b) {
return { x: a.x + b.x, y: a.y + b.y };
}
function vec_mul(a,c) {
return { x: a.x * c, y: a.y * c };
}
function vec_div(a,c) {
return vec_mul(a, 1.0/c);
}
function vec_normal(a) {
return vec_div(a, vec_mag(a));
}