Author Topic: Advanced Aiming Algorithm?  (Read 1760 times)

Minverva

  • Active Member
  • ***
  • Posts: 126
  • Karma: +0/-0
    • View Profile
Advanced Aiming Algorithm?
« on: Tue, Jan 27, 2015 »
It is pretty easy to aim bullets at the player's exact spot, but does anyone have an experience aiming precisely at a moving player?

Clearly distance, the velocity of the player, and the velocity of the bullet have to be taken into account, but the last part of it seems most confusing to me since the velocity of the bullet is mutable based on the other factors. 

Has anyone solved this?

Minverva

  • Active Member
  • ***
  • Posts: 126
  • Karma: +0/-0
    • View Profile
Re: Advanced Aiming Algorithm?
« Reply #1 on: Tue, Jan 27, 2015 »
The code here works pretty well:

http://www.kirupa.com/forum/showthread.php?347334-Aiming-and-hitting-a-moving-target

I'm not sure what this bit of code is for though:

// Check we're not breaking into complex numbers
   var q:Number = b * b - 4 * a * c;
   if (q < 0) return null;

I got rid of it when converting the algorithm for my program and it seems to work fine. Any clues?


Also, what, how and why is the question mark used for in this bit of code:

var t:Number = ((a < 0 ? -1 : 1)*Math.sqrt(q) - b) / (2 * a);

Here is the full code copied over from their website for posterity:

/**
 * Shoot at a target
 *
 * @param   t   The target to be shot
 * @return      The bullet to be fired (or null if cannot hit)
 */
public function shoot(targ:Target, bulletSpeed:Number = BULLET_SPEED):Bullet
{
   var dx:Number = targ.x - this.x;
   var dy:Number = targ.y - this.y;
   
   var a:Number = targ.vx * targ.vx + targ.vy * targ.vy - bulletSpeed * bulletSpeed;
   var b:Number = 2 * (targ.vx * dx + targ.vy * dy);
   var c:Number = dx * dx + dy * dy;
   
   // Check we're not breaking into complex numbers
   var q:Number = b * b - 4 * a * c;
   if (q < 0) return null;
   
   // The time that we will hit the target
   var t:Number = ((a < 0 ? -1 : 1)*Math.sqrt(q) - b) / (2 * a);
   
   // Aim for where the target will be after time t
   dx += t * targ.vx;
   dy += t * targ.vy;
   var theta:Number = Math.atan2(dy, dx);
   
   // Fire the bullet
   var bullet:Bullet = new Bullet();
   bullet.target = targ;
   bullet.hitPoint = new Point(targ.x + targ.vx * t, targ.y + targ.vy * t);
   
   bullet.x = this.x;
   bullet.y = this.y;
   bullet.vx = bulletSpeed * Math.cos(theta);
   bullet.vy = bulletSpeed * Math.sin(theta);
   
   return bullet;
}


Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
Re: Advanced Aiming Algorithm?
« Reply #2 on: Tue, Jan 27, 2015 »
I'm not sure what this bit of code is for though:

// Check we're not breaking into complex numbers
   var q:Number = b * b - 4 * a * c;
   if (q < 0) return null;

It's using the quadratic formula to get the solution for the angle and speed. That q value that is being calculated is then square rooted. If q is less than 0, you're going to be taking the square root of a negative, which puts you into the realm of complex/imaginary numbers. This is a safety check for that (I would guess since you got rid of it and saw no difference, that normally you won't get into that situation).

Code: [Select]
Also, what, how and why is the question mark used for in this bit of code:

var t:Number = ((a < 0 ? -1 : 1)*Math.sqrt(q) - b) / (2 * a);

Question mark is the "ternary operator". You can google it for more details, but most languages have it, and it's an inline conditional allowing you to simplify logic you could do with an "if then else". For example:

Code: [Select]
var x:int = a < 0 ? -1 : 1;
Is equivalent to:

Code: [Select]
var x:int;
if (a < 0) {
  x = -1;
} else {
  x = 1;
}

Some languages actually let you inline conditionals and do something like "x = if a < 0 then -1 else 1 end". This is just an operator to do that, and is common in most languages.

Minverva

  • Active Member
  • ***
  • Posts: 126
  • Karma: +0/-0
    • View Profile
Re: Advanced Aiming Algorithm?
« Reply #3 on: Wed, Jan 28, 2015 »
It's using the quadratic formula to get the solution for the angle and speed. That q value that is being calculated is then square rooted. If q is less than 0, you're going to be taking the square root of a negative, which puts you into the realm of complex/imaginary numbers. This is a safety check for that (I would guess since you got rid of it and saw no difference, that normally you won't get into that situation).

Very interesting, I vaguely remember the quadratic formula from school, but I'm sure we were taught to use it in a very specialized application. I didn't realize it could be used for other applications.

Quote
Question mark is the "ternary operator".

Your example was very helpful. Hopeful I remember it when I come across a situation for it.

Also, while my computer was out of commission, did you publish any new games?

Edit:

Since you seem pretty knowledgeable about mathematics. Do you think you could give examples on how to modify this equation in a few ways?

Accelerating bullets seems like it would be a challenge. Mainly because you would have to keep the maxvelocities in mind.

A more practical and interesting example would be with projectiles with gravity like grenades. Again though, because it uses acceleration the maxvelocity would likely screw up a normal equation.
« Last Edit: Wed, Jan 28, 2015 by Minverva »

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
Re: Advanced Aiming Algorithm?
« Reply #4 on: Wed, Jan 28, 2015 »
Also, while my computer was out of commission, did you publish any new games?

Nah. I've put coding on a complete hiatus for awhile (outside of work). So nothing new or interesting from me.

Quote
Since you seem pretty knowledgeable about mathematics. Do you think you could give examples on how to modify this equation in a few ways?

Accelerating bullets seems like it would be a challenge. Mainly because you would have to keep the maxvelocities in mind.

A more practical and interesting example would be with projectiles with gravity like grenades. Again though, because it uses acceleration the maxvelocity would likely screw up a normal equation.

I'm going to be completely useless here. If your accelerating grenade follows a parabola you can easily get the equation for, you can probably use the quadratic formula (http://zonalandeducation.com/mmts/intersections/intersectionLineAndParabolla1/intersectionLineParabolla1.html), but my guess is solving for collision of an accelerating bullet would be entering the realm of linear algebra and/or solving systems of equations (you have equations that describe the trajectories, and that you need them to meet, and need to solve for specific variables). However, that's the kind of math that I avoid at all costs and I'm terrible at and I've purged from my brain. Any time I've done anything similar I just approximate things because I've never actually need a "correct" solution for this (I just always experimented and found that if I set the x velocity to Y% of the players xspeed and the y velocity to 2 * the difference between their y positions, assuming gravity is Z, then it would come within range of the hitbox, or something silly like that).