Jump to content
  • Advertisement
Sign in to follow this  
Infinite_Daremo

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.

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
Advertisement
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
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 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!