check if enemy is in range to fire.

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

Recommended Posts

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; } 

Share on other sites
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.

Share on other sites

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.

Share on other sites
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.

Share on other sites

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.

1. 1
2. 2
3. 3
Rutin
20
4. 4
khawk
14
5. 5

• 9
• 11
• 11
• 23
• 12
• Forum Statistics

• Total Topics
633655
• Total Posts
3013186
×