Jump to content
  • Advertisement
Sign in to follow this  
blueshogun96

Need a better 2D tracking/pathfinding algorithm.

This topic is 2565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:



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.

Share this post


Link to post
Share on other sites
Advertisement
Okay, fixed it! It was a matter of normalizing the clamp values. The abs() calls caused the blasts not to move at all, but now it works fine. Thanks anyway. Hopefully someone down the line can find this thread and solve their problem(s) too. Code below.


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);

#if 0
clamp_float( &homingblasts.vx, -speed, speed );
clamp_float( &homingblasts.vy, -speed, speed );
#else
cx = cos( angle_to_target );
cy = sin( angle_to_target );
/*cx = (abs(cx));
cy = (abs(cy));*/

clamp_float( &homingblasts.vx, -speed*cx, speed*cx );
clamp_float( &homingblasts.vy, -speed*cy, speed*cy );
#endif

homingblasts.x += homingblasts.vx;
homingblasts.y += homingblasts.vy;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!