🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Find angle of radius at distance

Started by
10 comments, last by _WeirdCat_ 5 years, 10 months ago

Hello,

I'm trying to find the angle (difference) between a sphere's center and it's surface from an external position.

Math is not my strongest skill, and I'm much better reading code than formula. If you can write the solution in code form, my brain will thank you.

The reason I'm trying to calculate this is for a melee attack scan. The attacker scans for targets within a certain radius and angle of the attack origin. But I want to subtract each potential target's size (radius) from the angle test, so larger targets are easier to hit. So currently I just have the direction vector and distance from the attacker and Target origin. I would like to find the angle I need to reduce the angle requirements to be hit.

Thanks for any advice!

Advertisement

google reveals: https://stackoverflow.com/questions/21483999/using-atan2-to-find-angle-between-two-vectors

10 minutes ago, h8CplusplusGuru said:

Thank you for the reference. I will look at it closer soon. But at first glance it looks like I would need 2 vectors for this to be relevant? I only have one - the direction from the origin to the sphere.

Finding the angle between two destinations from one source is simple with dot product and acos. But I'm not sure how to generate a second destination with my one source, one destination, and radius (radius being "the distance between the two destinations").

Maybe I didn't explain this well. I think what I'm wanting is pretty simple. It's probably handled by a single math function. I want to compute the dot product or angle to rotate from one side of a circle to the other from an origin that is a specific distance away from it.

What I have: origin, sphere_center, sphere_radius

I also have the direction vector from the origin to the sphere of course.

Thanks again

I can't guarantee this is correct without checking it further, but off the top of my head I think the angle you're looking for (or half the angle, depending) may be (this is assuming the query position is safely outside the circle):


angle = asin(circleRadius / distanceFromPositionToCircleCenter);

This might also work:


distanceFromPositionToTangentPoint =
    sqrt(square(distanceFromPositionToCircleCenter) - square(radius));
angle = atan2(radius, distanceFromPositionToTangentPoint);

Imagine (or draw) one line passing through the query position and the circle center, one line passing through the query position and tangent to the circle, and one line passing through the tangent point and the circle center. These lines inscribe a right triangle. The radius and the distance from the position to the circle center are known, and other parameters, including the angle you're interested in, can be derived using trig.

Again, I'd want to double-check and maybe test this before committing to it, but you could at least try it out (and maybe work through the math yourself to confirm it).

@ Zakwayda: that is extremely helpful. Thank you for your time. If it doesn't work, it gives me a good place to start.

Zakwadya's first equation is correct.  You form a right triangle:  Point 1 = origin, Point 2 = center of circle, Point 3 = one of the tangent points on the circle.  A tangent point is where a line from the origin touches the circle perpendicular to the center (and therefore is the right angle of the triangle).  sin(angle) = opposite / hypotenuse, so angle = asin(o / h).  The length of the hypotenuse is the distance from Point 1 to Point 2, and the length of the opposite is the radius of the circle.

Thanks for confirming that. Am I right in thinking that the maximum angle of this result will be 90 degrees?

I'm wondering what to do when a potential target gets so close to the attacker that their radius is overlapping the attacker's origin. If I'm giving the attacker extra angle based on the defender's radius, would it make sense to just go ahead and give them a full 90 degree angle bonus when the sphere (defender) center is overlapping the origin (attacker)?

Here are some images to better explain this. The blue sphere is the defender. The white angle is the attack area. One image shows a normal situation with a defender in front. The other shows a defender getting too close, but still being inside the attack area.

attack_close.png

attack_far.png

asin(x) is undefined when x is > 1, which would happen when distance-to-center is less than the radius.  If you're inside the sphere, giving the maximum bonus (90 degrees) seems fine to me.

What about dot(attacker_direction_vec_normalized, vector_from_attacker_to_target normalized);

This gives you full 360 degree scan 

Positive result of dot indicates that attacker faces target 1 means its right in front of him, 0 means in 90. Degree angle

-1 says its behind him

Yeah, that would work pretty well for testing general angle thresholds. My attacks just have a specific min/max angle that's specified by a proxy object in the 3D model scene. So for example, a certain type of knife swing may only hit targets to the right or left.

My main problem was taking the target sizes into account. If you are fighting a very large enemy, part of his body can be at an angle the attack can hit while his origin position is completely outside of it. This is the reason for the radius angle offset.

This topic is closed to new replies.

Advertisement