check if enemy is in range to fire.

Started by
3 comments, last by MrDarkKnight 12 years, 3 months ago
Hello
I'm working on a TD game and I'm trying to write a code to check if the enemy enters the tower range to fire.

The problem is that the tower is shooting at the enemy even though the enemy is out of range (the red circle is the range)

here is a video that shows my problem.
[media]
[/media]

here what I have so far

Bullet::Bullet()
{
Radius = 100.0f;
}

void Bullet::CheckIfInRange()
{
DistanceX = tower.GetXPosition() - Enemy2.EnemyXPosition;
DistanceY = tower.GetYPosition() - Enemy2.EnemyYPosition;
if((DistanceX<= Radius && DistanceY<=Radius) || (DistanceX>= Radius && DistanceY>=Radius))
return true;
else
return false;
}
Advertisement
Why do you think this is the correct way to check if something is in range? Try looking at your if statement and interpret what it actually is that you're checking for.

A hint for the correct way to approach this problem: check if the squared distance to the enemy is smaller or equal to the squared bullet radius.

Why do you think this is the correct way to check if something is in range? Try looking at your if statement and interpret what it actually is that you're checking for.

A hint for the correct way to approach this problem: check if the squared distance to the enemy is smaller or equal to the squared bullet radius.


thanks i solved the problem. here is the code.

if(Enemy.GetPosition().x + Radius> tower.GetPosition().x && Enemy.GetPosition().x < tower.GetPosition().x + Radius && Enemy.GetPosition().y + Radius> tower.GetPosition().y && Enemy.GetPosition().y < tower.GetPosition().y + Radius)
//shoot

if there is a better way to do this please do let me know.

thank you again.
That is not what you're looking for. You want the tower to shoot when the enemy is in range of the tower right? To calculate the distance between two positions you'll have to use Pythagora's theorem. To keep things simple, to calculate the distance from point A to point B you get this:
[source]D = A - B;
distanceSquared = D.x*D.x + D.y*D.y;
distance = sqrt(distanceSquared);[/source]

Now if you want to check if something is in range, you don't need to calculate the actual distance, you only need to compare the squared distances. In your case you only have to check whether distanceSquared <= radius*radius.

If any of that is unclear, let me know and I'll try to explain in more detail.

That is not what you're looking for. You want the tower to shoot when the enemy is in range of the tower right? To calculate the distance between two positions you'll have to use Pythagora's theorem. To keep things simple, to calculate the distance from point A to point B you get this:
[source]D = A - B;
distanceSquared = D.x*D.x + D.y*D.y;
distance = sqrt(distanceSquared);[/source]

Now if you want to check if something is in range, you don't need to calculate the actual distance, you only need to compare the squared distances. In your case you only have to check whether distanceSquared <= radius*radius.

If any of that is unclear, let me know and I'll try to explain in more detail.


thanks this also work, even better.
thank you again.

This topic is closed to new replies.

Advertisement