PDA

View Full Version : Refining a following AI


Nick
2004.08.02, 02:43 AM
I have finally (after many days of trying) gotten one sprite to "target" another sprite and follow it around the screen. The problem is that my code has it automatically adjusting on the X and Y axis at the same time so the following isn't realistic. Below is a link to the project. If you can help me refine the AI so that following seems more realistic I'd appreciate it. I'm thinking I could set up 5 variables to store the last 5 positions of the target sprite and have the follower go to these in order. Thanks for any help or advice.

**EDIT**
I have updated the file because I kept playing with it. Please let me know what you think of it so far. It's far from a game but it has a lot of nifty little sprite things in it. Any advice on organizing what I already have or what I could add next (along with how) would be appreciated.

My Xcode Project File (http://www.freewebs.com/simreality/Thief.sitx)

Wheatie
2004.08.02, 05:14 AM
I couldn't take a look at your project as it was going less than 1 kb/s download last night and I didn't feel like waiting, but what I did, to get one helicopter to chase another, was this (its Java, but the theory should apply).

target is the sprite it the helicopter should follow


public void think()
{
int deltax;
int deltay;

if (target != null)
{
deltax = target.getX()-x; // get the difference in x values
deltay = target.getY()-y; // get the difference in y values
}
else
deltax = deltay = 0; // or idle animation, motion, etc


if (state != CRASHING)
{
velx = (int)deltax/6; // our velocity is dependent on the distance
vely = (int)deltay/6; // makes our sprite go faster to catch up to the target

if (velx > 30.0) velx = 30.0; // cap the speed at a certain amount
if (velx < -30.0) velx = -30.0;

x += (int)(velx * DT); // update our position by multiplying by the amount of time that passed
y += (int)(vely * DT);
}


Once you get a vector from the target to the follower, you can do whatever you want to its magnitude to affect the speed.

Hopefully that helps, and I'll try again later to see if the download goes faster.

Nick
2004.08.02, 02:27 PM
Sorry about the download speed. Freewebs was the only quick, free place I could use to upload/download files from but its speed isn't very impressive.