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:

There is actually a mathimatical formula to calculate this…

You basically take each vertex corner and rotate it.

So lets say that your numbers are like so:

Vector3(0.5, 0, 1)
Vector3(-0.5, 0, 1)
Vector3(0.5, 0, -1)
Vector3(-0.5, 0, -1)

then use this function to calculate each point of rotation:

function DoRotation(vec : Vector3){
var x= Mathf.Cos(transform.localEulerAngles.y * Mathf.Deg2Rad);
var z= Mathf.Sin(transform.localEulerAngles.y * Mathf.Deg2Rad);
return Vector3(x,0,z);
}

Just remember that the initial values should never change. we do all the recalculation based off of the identity rotation. Also it returns a local coordinate value.

1 Like

Another way would be to create empty game and attach them to your collider. They shoot the ray from their trasform positions.

Found another solution:

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.