1

I'm having difficulties with checking if a gameobject is within a specific radius compared to the player.

My gameobject contains a radius and i want to use that for the checking yet i fail.

This is what i had so far:

public bool IsInRange(Vector2 currentTarget)
{

    if ((currentTarget.X >= PosX - BRadius && currentTarget.Y >= PosY - BRadius) || // left up
        (currentTarget.X >= PosX - BRadius && currentTarget.Y <= PosY + BRadius) || // left down
        (currentTarget.X >= PosX + BRadius && currentTarget.Y >= PosY + BRadius) || //right up
        (currentTarget.X >= PosX + BRadius && currentTarget.Y <= PosY - BRadius)) //right down
    {
        return true;
    }
    return false;
}

I'm trying to do this within C# using the XNA framework.

PosX and PosY are from the current gameObject his position. And the currentTarget is for now only the player's position.

csharpwinphonexaml
  • 3,659
  • 10
  • 32
  • 63
Baklap4
  • 3,914
  • 2
  • 29
  • 56

3 Answers3

2

I've used Vector2.Distance() in the past to find out if a game object is near the player object. We can use this to return a float, and from this we can check if the game object is within a certain radius.

If you are specifically looking to see if a game object is top-right, bottom-left, etc of the player then this will not help much unfortunately.

In the question case:

public bool IsInRange(Vector2 currentTarget) {
    Vector2 yourPos = new Vector2(PosX, PosY);
    return BRadius >= Vector2.Distance(yourPos, currentTarget);
}
Lukas Ansteeg
  • 332
  • 2
  • 13
  • 1
    This or the answer by srm should be the accepted answer. I've added a code example. The currently accepted answer checks for a square around the target, not a circle as radius would imply. – Lukas Ansteeg May 29 '20 at 20:47
1

You are mixing your x's and y's.

Check the currentTarget.X is between the PosX extremes left & right. Not left and top. It makes my head hurt thinking that way!

I'll flesh this out a little in pseudo-code.

if(
  (target.x > my.x -radius && target.x < my.x +radius) &&
  (target.y > my.y -radius && target.y < my.y +radius)
){ ... }

Note this checks for a bounding box, not strictly speaking a radius. But that is sufficient in many cases.

So in the end it should be:

if (currentTarget.X > PosX - BRadius && currentTarget.X < PosX + BRadius) &&
   (currentTarget.Y > PosY - BRadius && currentTarget.Y < PosY + BRadius)
        { return true; }
Matt Stevens
  • 1,104
  • 1
  • 12
  • 24
  • I used this, yet this doesn't solve the problem for example: if you have the player at 400,250 and the AI at 200,200. And you move slightly up with the player it's within the Y range so it's in range. But it should also check if it's in range together with the Y – Baklap4 May 02 '14 at 14:29
  • I've replaced the OR with an AND in the pseudo-code, so now it fires when you are within both the x AND y bounds. – Matt Stevens May 02 '14 at 22:36
0

According to circle-circle collision (which is a lengthy discussion on hitdetection on spheres) you should just check the distance of x from the center of your cirlce. If the distance is greater than the radius, you are fine. Just solve (x2-x1)^2 + (y1-y2)^2 <= (r1+r2)^2, given that both objects have spherical hitboxes.

Community
  • 1
  • 1
srm
  • 128
  • 9