Skip to content

Commit

Permalink
Merge pull request #2 from aeolus-1/main
Browse files Browse the repository at this point in the history
Added target leading
  • Loading branch information
serifonium authored Aug 31, 2023
2 parents 286cefb + 2c8fbac commit 5c13c92
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
width:100%;
}
</style>
<script src="src/peer.js"></script>
<link rel="icon" type="image/x-icon" href="imgs/favicon.ico">
</head>
<body>
Expand All @@ -26,6 +27,9 @@
<script src="src/classes.js"></script>
<script src="src/enemies.js"></script>
<script src="src/weapons.js"></script>

<script src="src/connection.js"></script>

<script src="src/render.js"></script>
<script src="src/main.js"></script>
<script src="src/console.js"></script>
Expand Down
54 changes: 54 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,60 @@ function v(t, n) {
r = t.y - n.y;
return Math.sqrt(Math.pow(e, 2) + Math.pow(r, 2));
}
function intercept(src, dst, v) {
const tx = dst.x - src.x;
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(), below

// Find smallest positive solution
let sol = null;
if (ts) {
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;
}

/**
* 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];
}
} 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];
}
}
return sol;
}

function fetchAngle(a, b) {
let m = Math.atan((a.y-b.y)/(a.x-b.x))
if(a.x < b.x) { return m } else { return Math.PI + m }
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
Loading

0 comments on commit 5c13c92

Please sign in to comment.