- Home /
How to project the position of a point relative to a rotated collider using its bounds?
Hello, let's say I have a point point
and a rotated box collider collider
. What I'd like to do is be able to project point
such that if I generate a OBB bounds of the collider, I can calculate things like OBBBounds.SqrDistance(point)
or OBBBounds.IntersectRay(ray)
.
I've tried using something like
Vector3 localPoint = colliderTransform.worldToLocalMatrix.MultiplyPoint(point);
OBBBounds.center = Vector3.zero;
sqrDistance = OBBBounds.sqrDistance(localPoint);
However, this doesn't seem to quite work, and I think it's because colliderTransform.worldToLocalMatrix.MultiplyPoint(point)
is not the right transformation. However, I can't figure out exactly what is the right transformation. I think it's because OBBBounds.extent
that is world space, while localPoint
is in local space.
Would you be able to help my identify what the right transformation is?
As an example, let's say I have a collider at (41.7 ,7.1 ,42.1)
, with rotation (0, 180, 90)
in degrees, and my point is at (46.0, 5.7, 47.0)
. When I apply the above code, localPoint = (-1.4, 4.3, -4.9)
, and I would expect that it should actually be at (-4.3, 1.4, -4.9)
.
Answer by Bunny83 · Aug 21, 2021 at 07:27 PM
I think you have the wrong idea about Collider.bounds
. It returns the worldspace AABB (axis aligned bounding box)- The collider class does not provide you a localspace bounding box. What you're currently doing is projecting your worldspace point into the local space of the collider and then you test the local space point against a worldspace bounds which of course doesn't make much sense.
It's not quite clear what your exact usecase is. However the Collider class does have a Raycast method as well as a ClosestPoint method that directly takes a worldspace ray / worldspace point
Of course if, for some reasons, you really need a bounds instance that represents the localspace AABB of the box collider, you have to construct this yourself by using the box collider parameters like that:
Boxcollider boxColl;
Bounds localSpaceBounds = new Bounds(boxColl.center, boxColl.size);
Of course this bounds would only work when everything is in the same local space. So all positions or direction vectors need to be in the same local space.
Hi @Bunny83 ah yes you are correct - what I meant to say was that I had the localSpaceBounds of the collider as you mentioned, and I was trying to transform my point into that local space. I recognize that there is a ClosestPoint and Raycast method of the collider but I actually don't have access to the collider in the script I'm trying to run this from (all the position, rotation, and bounds information are extracted elsewhere).
How would I transform this point into the local space of the localSpaceBounds?
Well, just like you did already. However this line makes no sense:
OBBBounds.center = Vector3.zero;
The center of the bounds should also be given in local space coordinates. The center of a box collider is also in local space. So setting it to zero doesn't make any sense.
Though it should be obvious that all results you get from Bounds.ClosestPoint is also in local space since everything involved would be in local space.
Your answer
Follow this Question
Related Questions
Size of collider bounding box regardless of rotation 1 Answer
best way to check if a Vector3 point is within any of multiple colliders 1 Answer
Why would a collider.Raycast not register a hit when collider.bounds.IntersectRay does? 1 Answer
Non-rotating collider fixed to a rolling ball 1 Answer
Get closest point on collider on at a certain z depth 0 Answers