Getting the closest target

Non-platform specific questions.

Getting the closest target

Postby Hblade » Wed Nov 23, 2011 9:42 pm

How would I go about finding the closest enemy?
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Getting the closest target

Postby jimmynewguy » Wed Nov 23, 2011 10:20 pm

did a sort of demo thing on this a pretty long time ago. Should do the trick!
viewtopic.php?f=4&t=6654&p=48310&hilit=+closest#p48310
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: Getting the closest target

Postby Hblade » Wed Nov 23, 2011 10:25 pm

Thanks Ill check it out. +1
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Getting the closest target

Postby Hblade » Thu Nov 24, 2011 5:24 am

Can you pin-point and explain how this is done? ^^
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Getting the closest target

Postby jimmynewguy » Thu Nov 24, 2011 12:29 pm

Code: Select all
int n;
double mindist = 10000;//set closest distance to out of range so the first check will work
Actor *actors, *closest = NULL;// NULL since 0 can be an actor technically and the check will fail
actors = getAllActorsInCollision("Event Actor", &n);// get all actors in the collision
// actors is the actors, n is the number of actors

if(actors)// if we have actors
{
  int i;
  for(i = 0; i < n; i++)// cycle through them
  {
    if(strcmp(actors[i].name, "a") == 0 ||// js it actor a
    strcmp(actors[i].name, "c") == 0)// or is it actor c
 
    {
       double d = distance(parent.x, parent.y, actors[i].x, actors[i].y);// d is the distance of this actor from the player
       if(d < mindist)// if the actor is closer than the the previous closest actor
       {
           closest = (actors + i);//make the closest actor
           mindist = d;//new mindist
       }
    }
  }
}
if(closest)//if we have a closest actor
{
MoveTo("Parent Actor", 0.000000, 0.000000, 2.000000, closest->clonename, "");// move our parent to the closest actor
}

This code in the range actor does it all. It checks all the actors in it's collision when the timer goes. The number of actors is n and the name of the actor is actors. If there are actors [if(actors)] it does a loop through the clones to check if the actors' name is "a" or "c". If yes, then it gets the distance and sets this to d. d is then checked to see if it is less than the mindist variable (which is set to 10000 in the beginning (aka out of range)) if d is less than the mindist than the mindist will now be d and the closest actor will now be the actor that was just checked. Once the loop is done we check if there is a closest actor and move our parent to it. [if(closest)]
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: Getting the closest target

Postby Hblade » Thu Nov 24, 2011 1:39 pm

thanks =)
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest