2d - Check if point is within bounds, rotation
Hello, I need to detect when one object is within/inside another,
it works by checking bounds.Contains method (I check if bounds of green box, contains min and max bounds of yellow one)
However in my scenario, both boxes can be rotated, and it basically means following approach will be useless, since unity`s documentation says, bounds are:
> An axis-aligned bounding box, or AABB for short
is there any other method to calculate and detect that? I could use 2d colliders and detect that on collision enter, but I am looking for simpler way to calculate it.
Thank you.
Answer by Statement · Oct 31, 2015 at 07:25 PM
If you just need to test a point vs a bound, transform the point into the same transformation space as the bound. Obviously you can't use the renderer bounds since it keeps resizing. Perhaps a Rect would suffice.
If you need to to test two 2d boxes, you could do a simple line test for intersection, based from the corners of the box and if any line intersect, it overlaps. You can do a containment test such that if all 4 corners of box is inside the other box, it's contained (no intersection occur in containment). Note that either of the boxes could be contained within the other so you need to make the test for both (is A contained in B or is B contained in A?).
These are general problems that isn't Unity specific and you can find lots of examples online.
A simpler way might just be to use a BoxCollider2D, and use the provided functions IsTouching or OverlapPoint.
Thank you, isTouching using box collider 2d, turned out to be the easiest way, cheers.
Your answer
Follow this Question
Related Questions
Keep Top Down 2D camera in bounds of background sprite 3 Answers
Advanced placing position from the same angle of target position 0 Answers
How to calculate vector perpendicular to a direction vector? 2 Answers
List.Contains not working as intended 0 Answers
How do you determine the x,y co-ordinates of a sprite's boundaries? 1 Answer