- Home /
Calculating where vertices would be without rotation
I have a rotated object (a box collider in that case) and I want to calculate where its vertices would be, if it weren't rotated.
My failed attempts for converting bounds.min:
The object is rotated 20 degrees in x and y and the non-rotated bounds.min is (1.0, 1.0, 1.0).
Collider collider = transform.GetComponent<Collider>();
Vector3 center = collider.bounds.center;
Vector3 min = collider.bounds.min;
Quaternion rotation;
Vector3 unrotatedMin;
// Using Quaternion.Inverse()
rotation = Quaternion.Inverse(transform.rotation);
unrotatedMin = rotation * (min - center) + center;
Debug.Log(unrotatedMin); // (1.2, 0.1, 0.6) != (1.0, 1.0, 1.0)
Debug.Log(rotation); // (-0.2, -0.2, 0.0, 1.0)
Debug.Log(rotation.eulerAngles); // (341.3, 338.8, 7.1)
// By hand
rotation = new Quaternion();
rotation.eulerAngles = new Vector3(-20.0f, -20.0f, 0.0f);
unrotatedMin = rotation * (min - center) + center;
Debug.Log(unrotatedMin); // (1.1, 0.3, 0.6) != (1.0, 1.0, 1.0)
Debug.Log(rotation); // (-0.2, -0.2, 0.0, 1.0)
Debug.Log(rotation.eulerAngles); // (340.0, 340.0, 0.0)
The same method for rotating vertices works later on in the code, when I rotate from zero rotation back to 20 degrees rotation. I've been trying all day, but I just can't figure it out.
Answer by Oribow · May 22, 2016 at 12:45 AM
Use transform.localToWorldMatrix
. To ignore the scale part of the matrix, use Matrix4x4.MultiplyVector. Bounds are really just that bounds. A cube that contains every vertex of the object. Bounds are never rotated (view left side of picture). You want calc the collider verts. I did something similiar for 2D, but you can easily convert it to 3D. Just assume a box, not rotated and with a scale of 1. Then rotate, scale, translate every vert of that box to your target box local space. (Dont forget to include the collider offset and scale)
private void LoadColliderVerts(Collider2D collider, List<Vector2> verts)
{
verts.Clear();
Type cTyp = collider.GetType();
if (cTyp == typeof(BoxCollider2D))
GetBoxColliderVerts((BoxCollider2D)collider, verts);
else if (cTyp == typeof(CircleCollider2D))
GetCircleColliderVerts((CircleCollider2D)collider, verts, _circleVertCount);
else if (cTyp == typeof(EdgeCollider2D))
verts.AddRange(((EdgeCollider2D)collider).points);
else if (cTyp == typeof(PolygonCollider2D))
verts.AddRange(((PolygonCollider2D)collider).points);
}
private static void GetBoxColliderVerts(BoxCollider2D collider, List<Vector2> verts)
{
Vector2 halfSize = collider.size / 2;
verts.Add(collider.transform.TransformPoint(halfSize + collider.offset));
verts.Add(collider.transform.TransformPoint(new Vector2(halfSize.x, -halfSize.y) + collider.offset));
verts.Add(collider.transform.TransformPoint(-halfSize + collider.offset));
verts.Add(collider.transform.TransformPoint(new Vector2(-halfSize.x, halfSize.y) + collider.offset));
}
private static void GetCircleColliderVerts(CircleCollider2D collider, List<Vector2> verts, int circleVertCount)
{
float anglePerCircleVert = (Mathf.PI * 2) / circleVertCount;
for (int i = 0; i < circleVertCount; i++)
{
verts.Add(collider.transform.TransformPoint(new Vector2(collider.radius * Mathf.Sin(anglePerCircleVert * i), collider.radius * Mathf.Sin(anglePerCircleVert * i))));
}
}
@Oribow: Do you mean unrotated$$anonymous$$in = transform.localToWorld$$anonymous$$atrix.$$anonymous$$ultiplyVector($$anonymous$$);
? That gives me (0.8, 0.5, 0.4) ins$$anonymous$$d of (1.0, 1.0, 1.0). I tried various other combinations and matrices, but nothing works. I'm not sure why I need local to world space transformation since I only have one object and bounds.$$anonymous$$ and bounds.center seem to always be absolute world space coordinates, with or without parent. I'm pretty much just guessing at this point, so I would really appreciate any further help, thanks.
Oh, I found my 3D script for boxColliders. Here you go:
BoxCollider bc = (BoxCollider)mf;
foreach (Vector3 v in generic_cube_mesh.vertices)
{
Vector3 tmpv = v;
tmpv.x *= bc.size.x;
tmpv.y *= bc.size.y;
tmpv.z *= bc.size.z;
tmpv += bc.center;
verticiesList.Add(bc.transform.TransformPoint(tmpv));
}
Generic cube mesh holds the verts of a not rotated, scale = 1 cube. Just so I didnt have to type all 8 verts per hand.
Yes! Thanks so much, the code works and the picture made me realize my mistake to begin with.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081246im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotate a sphere by its vertices... 1 Answer
Rotation problem 1 Answer
Rotate Rigidbody to always face a force 0 Answers