# Checking a point lies within a radius

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

## Recommended Posts

Basically i want to check if an entity lies within a grenades explosion radius. I have the origin of the grenade and the explosion radius and the list of possible entities it collides with. What is the possible check to see if the entity lies within the explosion radius?

##### Share on other sites
You need to find the distance between the two points (using Pythagorus' theorem), and then compare that distance to the explosion radius:
for e in entities:	diff = e.position - grenade.position	if sqrt(diff.x*diff.x + diff.y*diff.y) < explosion.radius:		# we are inside the eplosion radius

##### Share on other sites
I'd prefer to check the squared distance with the square of the explosion radius in order to avoid a call to sqrt for each entity.

##### Share on other sites
It all boils down to ye' old "speed or accuracy" comparision.
Depends on how critical the comparision is.

In the grenade-example presented by the OP, I would use a squared check.

##### Share on other sites
Quote:
 Original post by RasmadrakIt all boils down to ye' old "speed or accuracy" comparision.Depends on how critical the comparision is.In the grenade-example presented by the OP, I would use a squared check.
A distance squared check is just as accurate as a distance check. The only time distance would matter is if the OP wants to apply damage based on the distance to the explosion, then he could do a distance squared check to see if the player is within range, then use the real distance to compute damage.

##### Share on other sites
Quote:
 Original post by RasmadrakIt all boils down to ye' old "speed or accuracy" comparision.Depends on how critical the comparision is.In the grenade-example presented by the OP, I would use a squared check.

If all you need is a binary test of whether a point is within a distance/radius (which is what the OP asked for) then checking the squared distances results in exactly the same results. Speed vs. accuracy doesn't come into it.

Edit: too slow. [grin] Man gd.net is running sluggishly today.

##### Share on other sites
How many grenades per second, and how many entities to check against (realistically)? If this is for a FPS, I'd say that level of optimization (simple as it is) is probably wasted effort.

##### Share on other sites
Quote:
 Original post by snakHow many grenades per second, and how many entities to check against (realistically)? If this is for a FPS, I'd say that level of optimization (simple as it is) is probably wasted effort.

It's no effort at all:

This kind of optimization is so fast and easy there's no reason you should avoid it.

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631758
• Total Posts
3002137
×