Sign in to follow this  

Checking a point lies within a radius

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Rasmadrak
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.
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 this post


Link to post
Share on other sites
Quote:
Original post by Rasmadrak
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.

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by snak
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.

It's no effort at all:

(diff.x*diff.x + diff.y*diff.y) < (explosion.radius*explosion.radius)

instead of

sqrt(diff.x*diff.x + diff.y*diff.y) < explosion.radius

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

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this