Original Post
I have a 2D game I've been working on for quite some time and it's going smoothly, but I really need to work on a new tracking algorithm. I searched google but didn't find much (maybe I was using the wrong keywords?). The basic idea is to move the crosshair over an enemy with a mouse or gamepad, get a lock, then shoot, and the blast follows the enemy until it's been hit. If you don't understand what I'm trying to do, think of how Panzer Dragoon's gameplay is when it comes to targeting enemies and what not.
My algorithm works okay, but the main problem is that it doesn't work feasibly for what I need it to do. For instance, let's say that the homing blast misses an enemy initially... it should circle right back and hit it. Mine circles around, but it keeps circling around until it fades away and often never hits the enemy. The slower the blast moves, the more accurate it is, but I can't have them move too slow because then it will effect game play. This is the algorithm I'm using:
So, what this algorithm does is generate some snake-like movements. Maybe my algorithm is fine and just needs an adjustment and not a complete replacement. Oh, and clamp float is pretty self explanitory.
I hope I explained this well enough for everyone to understand. Any ideas? Thanks.
My algorithm works okay, but the main problem is that it doesn't work feasibly for what I need it to do. For instance, let's say that the homing blast misses an enemy initially... it should circle right back and hit it. Mine circles around, but it keeps circling around until it fades away and often never hits the enemy. The slower the blast moves, the more accurate it is, but I can't have them move too slow because then it will effect game play. This is the algorithm I'm using:
float angle( float sx, float sy, float dx, float dy )
{
return (float) atan2( dy-sy, dx-sx );
}
/* Actual tracking code */
float angle_to_target = angle( homingblasts.x, homingblasts.y,
enemies[homingblasts.target].x /*-(enemies[homingblasts.target].sx/2.0f)*/,
enemies[homingblasts.target].y /*-(enemies[homingblasts.target].sy/2.0f)*/ );
float speed = 10.0f;
homingblasts.vx += cos(angle_to_target);
homingblasts.vy += sin(angle_to_target);
clamp_float( &homingblasts.vx, -speed, speed );
clamp_float( &homingblasts.vy, -speed, speed );
homingblasts.x += homingblasts.vx;
homingblasts.y += homingblasts.vy;
So, what this algorithm does is generate some snake-like movements. Maybe my algorithm is fine and just needs an adjustment and not a complete replacement. Oh, and clamp float is pretty self explanitory.
I hope I explained this well enough for everyone to understand. Any ideas? Thanks.