Skip to content

Commit

Permalink
added target leading
Browse files Browse the repository at this point in the history
  • Loading branch information
aeolus-1 committed Aug 31, 2023
1 parent 6182b6a commit 2c8fbac
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
65 changes: 33 additions & 32 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,54 @@ function v(t, n) {
const ty = dst.y - src.y;
const tvx = dst.vx;
const tvy = dst.vy;

// Get quadratic equation components
const a = tvx * tvx + tvy * tvy - v * v;
const b = 2 * (tvx * tx + tvy * ty);
const c = tx * tx + ty * ty;

// Solve quadratic
const ts = quad(a, b, c); // See quad() function below

const ts = quad(a, b, c); // See quad(), below
// Find smallest positive solution
let sol = null;
if (ts) {
const t0 = ts[0];
const t1 = ts[1];
const t = Math.min(t0, t1);
if (t < 0) {
t = Math.max(t0, t1);
}
if (t > 0) {
sol = {
x: dst.x + dst.vx * t,
y: dst.y + dst.vy * t
};
}
const t0 = ts[0];
const t1 = ts[1];
let t = Math.min(t0, t1);
if (t < 0) t = Math.max(t0, t1);
if (t > 0) {
sol = {
x: dst.x + dst.vx * t,
y: dst.y + dst.vy * t
};
}
}

return sol;
}

function quad(a, b, c) {
}

/**
* Return solutions for quadratic
*/
function quad(a, b, c) {
let sol = null;
if (Math.abs(a) < 1e-6) {
if (Math.abs(b) < 1e-6) {
sol = Math.abs(c) < 1e-6 ? [0, 0] : null;
} else {
sol = [-c / b, -c / b];
}
if (Math.abs(b) < 1e-6) {
sol = Math.abs(c) < 1e-6 ? [0, 0] : null;
} else {
sol = [-c / b, -c / b];
}
} else {
let disc = b * b - 4 * a * c;
if (disc >= 0) {
disc = Math.sqrt(disc);
a = 2 * a;
sol = [(-b - disc) / a, (-b + disc) / a];
}
let disc = b * b - 4 * a * c;
if (disc >= 0) {
disc = Math.sqrt(disc);
a = 2 * a;
sol = [(-b - disc) / a, (-b + disc) / a];
}
}
return sol;
}
}

function fetchAngle(a, b) {
let m = Math.atan((a.y-b.y)/(a.x-b.x))
Expand Down
14 changes: 13 additions & 1 deletion src/enemies.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,20 @@ class Drone extends Enemy {
let rad = this.playerDist
let center = v(player.pos.x+player.scale.x/2, player.pos.y+player.scale.y/4)

const LEAD_TARGET = true

let leadingTarget = intercept(this.pos, {
...center,
vx:player.vel.x,
vy:player.vel.y,
},
10
)

console.log(leadingTarget)

let u = this.pos
let l = center
let l = LEAD_TARGET?(leadingTarget||center):center
let m = (u.y-l.y)/(u.x-l.x)
let c = u.y-m*u.x
let a
Expand Down

0 comments on commit 2c8fbac

Please sign in to comment.