1
\$\begingroup\$

I'm working on a 2D RPG game and now I need to detect if players ability collides with enemy rectangle hitbox. Currently I have this code that detects if a point is in cone, but I need to modify it to find if rectangle is colliding with a cone like in a picture below

float skillAngleTotalWidth = 90f;
int radius = 8, x = 3, y = 4;
float startAngle = skillUsedAtAngle - skillAngleTotalWidth / 2;
float endAngle = skillUsedAtAngle + skillAngleTotalWidth / 2;
// Calculate polar co-ordinates 
float polarradius = (float)Math.Sqrt(x * x + y * y);
float Angle = Helpers.RadianToDegree((float)Math.Atan(y / x));
// Check whether polarradius is less then radius of circle or not and Angle is between startAngle and endAngle or not 
if (Angle >= startAngle && Angle <= endAngle && polarradius < radius)
{
   Console.WriteLine("Point ({0}, {1}) exist in " + "the circle sector", x, y);
}

enter image description here

Need to find if cone and rectangle collide

\$\endgroup\$
  • \$\begingroup\$ The complicated cases will be those when they collide despite none of the rectangle's corners being inside the cone: When the tip of the cone dips inside, when the rectangle is wider than the cone, etc. But I notice that in all situations except (easily detected) complete inclusion, at least one edge of the cone will cross at least one edge of the rectangle... \$\endgroup\$ – Medinoc Feb 13 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.