SolvedDistance Calculation

Posts 1–7 of 7 · Page 1 of 1
Distance Calculation
So for my aimbot i wanted to add a new method of aiming which was to aim at the closest player. Here is my function to calculate the closest player but it isn't returning the closest player just another player. Here is my function:


Code:
int GetClosestEnemy(int myindex)
{
	int closestplayerindex = 0;
	float smallnum;
	bool bfirstenemy = false;
	float targets[16];
	PlayerTable* enemies[16];
	PlayerTable* myplayer = (PlayerTable*)GetPlayerInfo(myindex);
	for(int i = 0; i < 16; i++)
	{
		if(enemies[i] != 0)
		enemies[i] = GetPlayerInfo(i);
		if(enemies[i] && myplayer && enemies[i]->Pos && myplayer->Pos)
		targets[i] = sqrtf(((enemies[i]->Pos->x - myplayer->Pos->x)*(enemies[i]->Pos->x - myplayer->Pos->x)) + ((enemies[i]->Pos->y - myplayer->Pos->y)*(enemies[i]->Pos->y - myplayer->Pos->y)) + ((enemies[i]->Pos->z - myplayer->Pos->z)*(enemies[i]->Pos->z - myplayer->Pos->z)));

		if(enemies[i]->team != myplayer->team)
		{
			if(bfirstenemy == false)
			{
				bfirstenemy = true;
				smallnum = targets[i];
			}
			if(smallnum < targets[i])
			{
				smallnum = targets[i];
				closestplayerindex = i;
			}
			
		}
	}

	
	return closestplayerindex;
	
}
What exactly is this line trying to achieve?

Code:
if(enemies[i] != 0)
    enemies[i] = GetPlayerInfo(i);
You never initialize the values of 'enemies' before the loop so the test is redundant and would depend on the state of the memory before you allocated it.

Secondly I'd just declare a function to find the distance between two players, it'll be much easier in the long run to read and maintain.

Code:
float GetPlayerDistance(PlayerTable *me, PlayerTable *enemy)
{
    float xD = pow(me->Pos->x - enemy->Pos->x, 2);
    float yD = pow(me->Pos->y - enemy->Pos->y, 2);
    float zD = pow(me->Pos->z - enemy->Pos->z, 2);

    return sqrt(xD + yD + zD);
}
Code:
PlayerTable *GetClosestEnemy(int myindex)
{
    PlayerTable *pClosest = NULL;
    PlayerTable *pEnemy = NULL;
    PlayerTable *pMe = (PlayerTable*)GetPlayerInfo(myindex);
    float fClosest = 9999999999f; //some stupid value
    float fCurrent = 0.0f;

    if (pMe != NULL)
    {
        for(int i = 0; i < 16; i++)
        {
            if (i != myindex)
            {
                pEnemy = (PlayerTable*)GetPlayerInfo(i);
                if (pEnemy == NULL) { continue; }
                fCurrent = GetPlayerDistance(pMe, pEnemy);
                if (fCurrent < fClosest && pEnemy->team != pMe->team)
                {
                    fClosest = fCurrent;
                    pClosest = pEnemy;
                }
            }
        }
    }
    return pClosest;
}
Something like that. I'm at work so I can't do much.
Thx for ur reply @Jason I will try out ur advice and report back. Ur method seems a lot more efficient than mine. Thx a bunch I appreciate it.
don't square root the distance, saves some processing time and therefor makes the aimbot faster
I have edited your code to work for my case since the game was crashing and it works great thanks. But my problem now is that it's slow. Like after i kill a player then it will automatically go to the next player but it takes like 4 seconds to get to the next player. Here is my code:

Code:
int GetClosestEnemy(int myindex)
{
    PlayerTable *pClosest = NULL;
    PlayerTable *pEnemy = NULL;
    PlayerTable *pMe = (PlayerTable*)GetPlayerInfo(myindex);
    float fClosest = 99999999999999999999999999999999999999.0f; //some stupid value
    float fCurrent = 0.0f;
	int index = 0;
   
        for(int i = 0; i < 16; i++)
        {
            
                pEnemy = (PlayerTable*)GetPlayerInfo(i);
                if (pEnemy && pMe && pEnemy->Pos && pMe->Pos)
				{
					 xD = (pMe->Pos->x - pEnemy->Pos->x)*(pMe->Pos->x - pEnemy->Pos->x);
					 yD = (pMe->Pos->y - pEnemy->Pos->y)*(pMe->Pos->y - pEnemy->Pos->y);
					 zD = (pMe->Pos->z - pEnemy->Pos->z) * (pMe->Pos->z - pEnemy->Pos->z);
				
 
                fCurrent = (xD + yD + zD);
                if (fCurrent < fClosest && pEnemy->team != pMe->team)
                {
                    fClosest = fCurrent;
                    pClosest = pEnemy;
					index = i;
                }
            
				}
			
		}
    
    return index;
}


---------- Post added at 09:01 PM ---------- Previous post was at 08:47 PM ----------

EDIT:

NEVER MIND, I just put in a check to make sure that the player isn't dead and now it works. Now i just gotta make a visibility check lol. Thx guys.
Quote Originally Posted by 258456 View Post
I have edited your code to work for my case since the game was crashing and it works great thanks. But my problem now is that it's slow. Like after i kill a player then it will automatically go to the next player but it takes like 4 seconds to get to the next player. Here is my code:

Code:
int GetClosestEnemy(int myindex)
{
    PlayerTable *pClosest = NULL;
    PlayerTable *pEnemy = NULL;
    PlayerTable *pMe = (PlayerTable*)GetPlayerInfo(myindex);
    float fClosest = 99999999999999999999999999999999999999.0f; //some stupid value
    float fCurrent = 0.0f;
	int index = 0;
   
        for(int i = 0; i < 16; i++)
        {
            
                pEnemy = (PlayerTable*)GetPlayerInfo(i);
                if (pEnemy && pMe && pEnemy->Pos && pMe->Pos)
				{
					 xD = (pMe->Pos->x - pEnemy->Pos->x)*(pMe->Pos->x - pEnemy->Pos->x);
					 yD = (pMe->Pos->y - pEnemy->Pos->y)*(pMe->Pos->y - pEnemy->Pos->y);
					 zD = (pMe->Pos->z - pEnemy->Pos->z) * (pMe->Pos->z - pEnemy->Pos->z);
				
 
                fCurrent = (xD + yD + zD);
                if (fCurrent < fClosest && pEnemy->team != pMe->team)
                {
                    fClosest = fCurrent;
                    pClosest = pEnemy;
					index = i;
                }
            
				}
			
		}
    
    return index;
}


---------- Post added at 09:01 PM ---------- Previous post was at 08:47 PM ----------

EDIT:

NEVER MIND, I just put in a check to make sure that the player isn't dead and now it works. Now i just gotta make a visibility check lol. Thx guys.

Do your NULL check on yourself (pMe) before the loop, no point in looping 16 times if your pointer is NULL the whole time. Also, as you're no longer returning a PlayerTable pointer (for whatever reason, I can't really figure out why you'd just return the index...), you don't need the pCurrent variable as you never do anything with the it.

Also, where are xD, yD and zD declared? Surely not as globals?
Quote Originally Posted by Jason View Post



Do your NULL check on yourself (pMe) before the loop, no point in looping 16 times if your pointer is NULL the whole time. Also, as you're no longer returning a PlayerTable pointer (for whatever reason, I can't really figure out why you'd just return the index...), you don't need the pCurrent variable as you never do anything with the it.

Also, where are xD, yD and zD declared? Surely not as globals?
Well for the way i am using the function i use the index not the position. And the floats aren't global it just got erased when i was pasting it on here. Thanks for your help @Jason i appreciate it.
Posts 1–7 of 7 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us