method to check if point is inside the rect? (moving objects in some boundary)

What I want to do is to move object, box for example on top of another box but not to go over the boundaries. Think moving picture on the wall. So if I am dragging smaller object when I get to the edge object stops moving in that direction but in others it can move (for example can’t go over left edge but can move up and down). I am trying to figure out how to determine if object edge, point on the object edge or center point with half of the size of the object in that axis is inside the rect, not over the boundary of the object in that axis.

The problem here that smaller object has a little offset from the bigger one, they are not on the same plane. Another problem is that planes are not aligned with world axis and the same method should work on other planes as well like the plane at some angles with points R2,R5,R6,R4

I have something already that is working for some cases but not for all, it’s more complicated that I want it to be, there might be a lot easier solution so I am looking for advice here.

You probably want to compare the two rects on some common plane. Use Overlaps for the comparison itself.
To find a common plane, take any 3 points of the rect A, then produce a Plane (via 3rd overload).

If your two planes are parallel, you can use the plane normal to find the direction of the offset of rect B points from plane A. The magnitude of this offset is equal to the distance between the planes → this is the same as dot product of the difference between two plane origins* and a normal vector.

  • You can also use rect centroids for this vector, because Unity’s plane won’t reveal its origin (or inPoint as they call it in the ctor), so you’d have to compute one via ClosestPointOnPlane. (You can also use GetDistanceToPoint and pick a centroid from the other rect.)

If your two planes aren’t parallel, you need to research some other way of finding the common plane. But whatever it is, you just project the other plane’s points onto it. Obviously if you involve some rotation, the projection will be cast distorted, like a shadow.

edit:
Forgot to add this. Rects are 2D, and you want this to work in 3D. So your intermediate results should be in 2D, so that you can compare 2 rects on 1 plane.

There is also a problem of how exactly you keep track of your Rects in the 3D scene. You may also want to use Bounds for a better effect.

thanks for explanation, i am actually using box collider bounds but using vertices that are faced forward of the bigger object (wall) and the same for smaller object (painting), just the ones that are facing -forward direction. so basically i am not dealing with rects. so basically the question would be how to check if any of the corners of the smaller object is inside the area that is made by corners of the bigger object.

  • get the plane of the wall
  • get points of the painting on the plane of the wall (because of the offset)
  • somehow check if points are inside the area?

The area can be established through box collider bounds by checking if it’s less than our position subtract or add our vector3 bounds extent.

Read carefully this paper
Unity - Scripting API: Bounds.extents.
The artefact in question the first line states nicely

What are bounds extents?

They are the extents of the Bounding Box. This is always half of the size of the Bounds.

therefor our position subtract our bounds extent is the edge. In any direction. X Y and Z. Bounds extent is a vector3. Likewise our position subtract or add our (bounds / 2) will be the edge of our bounds. Bounds may exceed mesh on an oddly shaped object. so work carefully.

if the point checking is less than our position + bounds extent and greater than our position - bounds extent then we have intersected the object with one or more of our corners.