OK, so I have a play field 512x512that wraps around, -32becomes 512for xand y.
Now I need to calculate the angle between the two objects, I have the following code as a kind of workaround, it works most of the time, but sometimes it fails anyway:
Shooter.getAngle = function(a, b) {
var ax = a.x;
var bx = b.x;
if (a.x < this.width / 4 && b.x > this.width - this.width / 4) {
ax += this.width;
} else if (a.x > this.width - this.width / 4 && b.x < this.width / 4) {
bx += this.width;
}
var ay = a.y;
var by = b.y;
if (a.y < this.height / 4 && b.x > this.height - this.height / 4) {
ay += this.height;
} else if (a.y > this.height - this.height / 4 && b.y < this.height / 4) {
by += this.height;
}
return this.wrapAngle(Math.atan2(ax - bx, ay - by) + Math.PI);
};
I just can't figure out how to design aon b, so that the wrapper counts correctly.
If someone can help, it would be great, since homing missiles that fly away from their target are not so funny.
, , , .
EDIT:
, , , , , :
function getAngle(a, b) {
var tx = a.x - b.x;
var ty = a.y - b.y;
tx = ((tx + 16) % (480 + 32)) - 16;
ty = ((ty + 16) % (480 + 32)) - 16;
var r = Math.atan2(ty, tx) * (180 / Math.PI);
console.log(ty, tx);
console.log(r);
}
function Point(x, y) {
this.x = x;
this.y = y;
}
getAngle(new Point(460, 240), new Point(60, 240));
getAngle(new Point(60, 240), new Point(460, 240));
getAngle(new Point(60, 240), new Point(280, 240));
getAngle(new Point(280, 240), new Point(60, 240));
, - , a.x < width * 0.25 b.x > width * 0.75 .., , , , :/