Calculation of corner points of a rotated rectangle

Hi.

Hope you can help me with a little confusing math problem.
I have a rectangle with length 2 and height 1 and want to make a raycast from each corner of it. When the rectangle has no rotation I can calculate the points of the corners by:

leftBottom.x = -(rigidbody.collider.bounds.extents.x - 0.1f);
leftBottom.y = -(rigidbody.collider.bounds.extents.y - 0.1f);
leftBottom = transform.TransformPoint(leftBottom);
	
leftTop.x = - (rigidbody.collider.bounds.extents.x - 0.1f);
leftTop.y = (rigidbody.collider.bounds.extents.y - 0.1f);
leftTop = transform.TransformPoint(leftTop);

rightBottom.x = (rigidbody.collider.bounds.extents.x - 0.1f);
rightBottom.y = - (rigidbody.collider.bounds.extents.y - 0.1f);
rightBottom = transform.TransformPoint(rightBottom);

rightTop.x = (rigidbody.collider.bounds.extents.x - 0.1f);
rightTop.y =  (rigidbody.collider.bounds.extents.y - 0.1f);
rightTop = transform.TransformPoint(rightTop);

This works well. But if my rectangle rotates (e.g. 90°) then the calculated points are the same as no rotation would be given.

How do I have to add the rotation to this to get the correct rotated corner points?

Any help is appreciated :slight_smile:

I would say make 4 Empty Objects and place them on the exact spots of your rectangle corners(Where you want the rays to come out from), then attach them to your rectangle as children. Then make the rays come out of them instead of the rectangle itself. This way you wont need to calculate the rotation or position of the rectangle.

But if you insist on calculating it, i would suggest defining your rectangle as a Transform and then using transform.rotation inside your coordinate calculation script.

Also since you know the coordinates, you can substract them manually instead of making the system calculate them and use those coordinates with a transform.rotation too.

But i would suggest my first suggestion of making 4 new empty objects.

Ok, found the problem.

collider.bounds is axis alligned so rotating them is not possible.

An alternative is to use Rigidbody.SweepTest which is what I have searched :slight_smile:

Thx for help anyway guys.