I don't understand what "col.bounds.max.x" means

Hi there,

I’m following a tutorial at the moment where there is a player game object and a script is attached to it with code inside of it.
I was wondering could someone explain what the fourth line in the following two lines of code in the script mean?

private Collider2D col;
private Vector2 topOfPlayer;
col = GetComponent<Collider2D>();
topOfPlayer = new Vector2(col.bounds.max.x,col.bounds.max.y);

So basically I understand that a Vector2 variable is created called “topOfPlayer” and then if I’m not mistaken it is being equaled to a new Vector2 that involves the outer limits of the player’s collider box in some way?

I’ve tried looking up what does “bounds.max” mean but I can’t really find an easy understandable answer online.

Thanks

Col is collider, bounds is Bounds struct which represents bounds of this collider.
Bounds is box which can be given by two points (Vectors), min bottom corner and max top corner.
If you ever played on minecraft server and used wooden axe to claim territory you probably should understand how it works.

This picture explains what is bounds, the only difference is that size on the picture is called extents, and size in Unity is twice bigger than that:

Also, a small tip, this line:

topOfPlayer = new Vector2(col.bounds.max.x,col.bounds.max.y);

Can be replaced with:

topOfPlayer = col.bounds.max;
6 Likes

The docs tell you exactly what it is:

In this case it’s literally bounds.center + bounds.extents.

It’s important to note that the Bounds of a Collider are not necessarily the actual surface of the Collider. They are an axis-aligned bounding box.

Hi,

Thanks for the helpful response.

I’m trying to understand your what you’re saying. I’ve created a simple diagram of shown here based on your explanation and I was wondering could you verify that it is correct?:Bounds-Diagram hosted at ImgBB — ImgBB

If what I’ve shown in my diagram is right does that mean that “bounds.max.x” would refer to the x co-ordinate of the vector2 that is contained within the top right corner of the 2D player game object shown in my diagram?

Cheers

EDIT: Apologies in the diagram I have miswritten max top corner as min top corner.

Yes, your diagram is right. Max and min are correct.

You said in a previous post that “bounds are Bounds struct which represent bounds of this collider. Bounds is box which can be given by two points (Vectors), min bottom corner and max top corner.”

I’m just wondering why are bounds used in the first place? What can bounds be used for? Is it used for detection purposes? If so why would you use bounds instead of the boundaries of the 2D collider that surrounds a game object?

Can you explain in simple terms what is meant by “axis-aligned bounding box”. I’m having trouble with distinguishing between the boundaries of a 2D Collider that surrounds a game object and the Bounds of a Collider…

Also what do I name the points that are opposite Max Top Corner and Min Bottom Corner as shown in this image I’ve uploaded: https://ibb.co/MCwR6X0

An axis-aligned bounding box is a bounding box is:

  • A box
  • That is aligned to the world axes (x and y - and z but let’s ignore Z for now)

Imagine you have a BoxCollider2D. If the box collider is not rotated at all - the situation is like the image on the left below. The AABB aligns perfectly with the actual collider box. However, if you rotate the box, the edges of the box no longer align with the world axes. Since the Axis-aligned bounding box must always be aligned with the world axes X and Y, it must grow a bit to contain the rotated BoxCollider2D. The situation is now the one in the image on the right. The actual collider is the rotated box inside. The Axis-aligned bounding box is the larger box that contains it. The key point is that it is always aligned with the world axes.

The AABB is the thing that you get when you read Collider.bounds. The reason Unity uses AABBs is because they are extremely efficient to calculate collisions with. It’s pretty much just a few greater than and less than comparisons to check if any point is inside of an AABB. Unity uses AABB collisions as a “first pass” to check if objects are even close to each other. This way, it can discard colliders quickly before performing more expensive types of collision checks.

8 Likes

To make the distinction clearer, here is a Polygon2D collider with some random shape in green, and its axis-aligned bounding box (AABB) in red.
6990524--825500--upload_2021-3-30_10-8-24.png
Notice that as the polygon collider rotates, its bounds do not rotate as well. Instead, the bounds represent a box that perfectly encapsulates all of the points inside the collider with no gaps around the edges. There are an infinite number of potential boxes that fit this limited description, however:
6990524--825509--upload_2021-3-30_10-12-45.png

So what makes the bounding box axis-aligned is because its sides are aligned with the world axes of x, y, and z.

@PraetorBlue beat me to the punch by a couple of minutes but yes, the reason that AABBs are used everywhere in computer graphics and physics is because they are very efficient to do basic collision checking with. In the case of physics, if a collision doesn’t intersect with the bounding box, it can’t possibly have intersected with the collider itself – and checking for AABB collisions is mounds cheaper than checking for the collision with the polygon collider above, for example.

6990524--825503--upload_2021-3-30_10-11-54.png

5 Likes

Thanks for the additional example with the polygon collider @Madgvox I think that makes things a lot clearer than my Box example.

1 Like

Thanks for those diagrams, I’m starting to understand it now.

I’ve created a diagram myself which uses your polygon examples as a reference and I was wondering could you help me understand whether points that are called “Max Top Corner” and “Min Bottom Corner” remain the same in both examples regardless of the rotational position of the polygon? Also I’m wondering what do I call the points that horizontally oppose the “Max Top Corner” and “Min Bottom Corner”? Do I just call them “Min Top Corner” and “Max Bottom Corner”? I don’t really understand why the points are named like this? Does it have something to do with the X and Y axis?

Here’s the diagram: https://ibb.co/VxVvQ4M

I get what you’ve explained but I’m still trying to understand a basic real world example of using Bounds in a simple 2D platformer game.

For example when I started learning about getting a player game object to jump in the air I was taught to create a game object inside the player game object, position it at the feet of the player and by using a Physics2D.OverlapCircle function and other things I could detect whether the player was grounded before being allowed to jump using the spacebar.

It’s sort of the same thing with getting the Player GO to check for walls. You create a GO called “wallCheck” inside of the Player GO, position it in the center and then by using Physics2D.Raycast function you could check for walls when the ray shoots and detects the the 2D BoxCollider of a wall.

I’m wondering why Bounds was not used in these examples? Could you give me a simple example of where it would be advisable for me to use Bounds?

Perhaps seeing it in motion will help.

in2of3

The reason that there’s only a max and a min is because you only need two points to define a 2D AABB. If you look at the video you can intuitively prove this for yourself – the top left and bottom right corner of the bounding box are just made up of components of the min and max – min.x and max.y for the top left, max.x and min.y for the bottom right.

For the same reason that you wouldn’t want to drive around on four wheels and a chassis as opposed to in a car. AABBs are a component of the whole package that makes up physics and collision logic – they are not the whole themselves. They are a useful tool and are able to be utilized in many areas of game development.

3 Likes

Sorry if I’m a bit slow but are the min and max values of the bounding box(top right and bottom left points) vector values? I don’t understand what they are? Are they variables of some kind?

Take your mind out of scripting and into geometry for a moment. Setting aside Unity entirely, min and max are both geometric points that define a bounding box. They both have an x and a y component to meaningfully place them on a 2D coordinate system.

Back in Unity, 2D points are represented as Vector2 values. A 2D AABB is a simple structure that contains two Vector2 values, its min and its max. The min is the “minimum”, or the lower left most extreme point. The max is the “maximum” or the upper right most extreme point.

Okay I understand what you mean.

However I don’t understand what you mean by when you say “the top left and bottom right corner of the bounding box are just made up of components of the min and max”

And then you go on to describe how the top left corner consists of min.x and max.y. I don’t understand what “min.x” actually is? I know that “min” represents the bottom left corner of a Bounding Box but I don’t know what “min.x” is…

“min” represents the bottom left corner of a bounding box, but “min” actually is a geometric point.

Look at this graph: Min/Max Example | Desmos

Notice how there are two points defined, labeled “min” and “max”. They both consist of an x and a y value, which defines where they appear on the X/Y axes of the graph. Also notice how there are two dashed lines extending outwards from each point. Where the lines coming from “min” intersect with the lines coming from “max”, a box is formed. As you drag around one of the points, the invisible box in the middle changes shape and can become any possible box. In other words, even though there are only two points on the grid, any axis-aligned box with four sides and four corners can be inferred from these two points.

When I refer to min.x, I’m referring to the x component of the point labeled “min”. So if “min” is a point defined as (2,3), “min.x” would refer to the x component of 2.

Like I mentioned above, 2D points are represented in Unity as a type called “Vector2”. You can make a new Vector2 and assign values to it, and then access those values:

Vector2 myValue = new Vector2( 2, 3 );
Debug.Log( myValue.x ); // will print "2.0" to the console

The Bounds type has two Vector2 values inside it called “min” and “max”. You access the x and y components of these Vector2 points the same way that you would with a Vector2 you make yourself, so to access the x component of the “min” point inside a bounds object, you would do this:

bounds.min.x

So when I’m saying that you can get the top left corner from min.x and max.y, I’m speaking in code terms. To get the four corners of a bounding box in Unity, you might do the following (this is actual code from the spinning collider example I made above):

Bounds bounds = coll.bounds;

Vector2 bottomLeft = bounds.min;
Vector2 topRight = bounds.max;
Vector2 bottomRight = new Vector2( topRight.x, bottomLeft.y );
Vector2 topLeft = new Vector2( bottomLeft.x, topRight.y );
6 Likes

Okay that’s great I fully understand now. That graph you posted and the code examples you provided explained it very efficiently to me. I just had difficulty in visualizing how to get the co-ordinates of the Vector values that oppose the Vector values of the MAX and MIN points but now I fully understand. Cheers

May be for someone will be useful the next pictures show difference between results of
methods ClosestPoint() & ClosestPointOnBounds()

void OnDrawGizmos()
{
    Gizmos.color = Color.red;
    if (_showColliderInfo)
    {
        Gizmos.DrawWireSphere(_closestPoint, 0.1f);
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(_closestPointOnBound, 0.2f);
        _showColliderInfo = false;
    }
}

public void ShowColliderInfo(Collider resultCollider, Transform soldierTransform)
{
    _currentLocation = soldierTransform.position;
    _closestPoint = resultCollider.ClosestPoint(_currentLocation);
    _closestPointOnBound = resultCollider.ClosestPointOnBounds(_currentLocation);
    _showColliderInfo = true;
}

The difference existent only if collider “was no aligned with Axes”
9380579--1311899--NoRotation.jpg 9380579--1311902--WithRotation.jpg