# [Vector Distance]Trying to get the distance between two vectors and get the enemy to shoot when i'm in range but I can't get it through. Help please

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

## Recommended Posts

	float CalculateDistance(D3DXVECTOR3 enemyPos, D3DXVECTOR3 playerPos)
{
float length;
float distanceX;
float distanceZ;

distanceX = enemyPos.x - playerPos.x;
distanceZ = enemyPos.z - playerPos.z;

length = sqrt((distanceX*distanceX) + (distanceZ * distanceZ));

return length;
}

//And then in my Update

D3DXVECTOR3 distance = D3DXVECTOR3(10.0f, 0.0f, 10.0f);

if (distance.x || distance.z <= CalculateDistance(D3DXVECTOR3(position.x, 0.0f, position.z), D3DXVECTOR3(CGame::GetPlayer()->GetPos().x, 0.0f, CGame::GetPlayer()->GetPos().z)))
{
int Random = rand() % 50;

if (Random == 0)
{
D3DXVECTOR3 towardsPlayer = CGame::GetPlayer()->GetPos() - position;
Bullet::CreateBullet((D3DXVECTOR3(position.x, position.y - 5.0f, position.z)),
D3DXVECTOR3(10.0f, 10.0f, 0.0f),
25.0f,
towardsPlayer,
30,
BULLET_TYPE_ENEMY);
}
}

I've tried doing this. The enemy continues to shoot even if I'm farther than distance.x and distance.z. Can someone tell me what's wrong?

Edited by Smarkus

##### Share on other sites

When something goes wrong, please include what goes wrong. Save us all some time by not having to guess.

That said, your if check looks very suspect. if (distance.x || ...) -- this will most likely always return true. In this part of the if, you are only checking whether distance.x evaluates to true (which I think is the same as distance.x != 0.0f)

I think the if check can be changed to something like this (you might have to cast things or convert to the correct type...):

if (CalculateDistance(position, CGame::GetPlayer()->GetPos() <= someValue)

##### Share on other sites
14 minutes ago, Lactose said:

When something goes wrong, please include what goes wrong. Save us all some time by not having to guess.

That said, your if check looks very suspect. if (distance.x || ...) -- this will most likely always return true. In this part of the if, you are only checking whether distance.x evaluates to true (which I think is the same as distance.x != 0.0f)

I think the if check can be changed to something like this (you might have to cast things or convert to the correct type...):


if (CalculateDistance(position, CGame::GetPlayer()->GetPos() <= someValue)

Thanks for the reply. What's wrong with my code is that the enemy continues to shoot even if i'm farther away than distance.x and distance.z which is 10.0f each.

So the someValue that you suggested is in my case is the range in which the enemy shoots which in my case is  distance.x and distance.z.

Edited by Smarkus

##### Share on other sites

The likely problem is that you misunderstood what the operator || does. It takes booleans on both sides and returns true if either one is true (it also has short-circuiting semantics, but that's not relevant here).

So distance.x' is converted to a boolean, which means your condition will trigger whenever distance.x is not zero. Probably not what you want.

Edited by alvaro

##### Share on other sites
1 minute ago, alvaro said:

The likely problem is that you misunderstood what the operator || does. It takes booleans on both sides and returns true if either one is true (it also has short-circuiting semantics, but that's not relevant here).

So distance.x' is interpreted as a boolean, which means your condition will trigger whenever distance.x is not zero. Probably not what you want.

Got it. So what I was thinking when I made this vector was that this would be the distance which is the range in which the enemy would shoot and if I'm out of the distance range then the enemy won't shoot at the player. How else do you suggest me to implement that?

##### Share on other sites

if( dist.x  <= YourConditionHere || dist.y <= YourConditionHere)

You are doing the equivalent of:

if(dist.x)

which, if dist.x is != zero...which it always is...it will always enter the conditional.

You might've been able to track this down via debugging as well, or logging.  Those are both good tricks to have in your skillset.

Though bigger picture, distance is a single float and not a vector, so I'm not sure why you're checking .x or .z and not just distance.

Edited by ferrous

##### Share on other sites
1 hour ago, Smarkus said:

Got it. So what I was thinking when I made this vector was that this would be the distance which is the range in which the enemy would shoot and if I'm out of the distance range then the enemy won't shoot at the player. How else do you suggest me to implement that?

You really should be able to figure this one out yourself. Measure the distance (a single number, as ferrous points out), then check against your threshold.

##### Share on other sites

if your max shoot vector is 10,0,10, calculate that as a scalar length of distance value and compare to the distance between the player and enemy.




float maxDistance = sqrt((distance.x*distance.x) + (distance.z*distance.z));
if (maxDistance >= CalculateDistance(D3DXVECTOR3(position.x, 0.0f, position.z), D3DXVECTOR3(CGame::GetPlayer()->GetPos().x, 0.0f, CGame::GetPlayer()->GetPos().z)))

{

}

you can also optimize the whole thing if you don't use sqrt() at all, in this instance it wont' affect the outcome.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633678
• Total Posts
3013290
×