3
\$\begingroup\$

I attached a Box Collider to a object, which has not the center as its pivot. Now I need the 8 corner points.

Right now I can only get the points in a unrotated state. But whenever I rotate the Object (which rotates in the center of the boxCollider) - everything gets messed up, because the rotation alters the gameObject-Position.

EDIT2: Solution

public Vector3[] GetColliderVertexPositions (GameObject obj)  
{
    BoxCollider b = obj.GetComponent<BoxCollider>(); //retrieves the Box Collider of the GameObject called obj
    Vector3[] vertices = new Vector3[8];
    vertices[0] = obj.transform.TransformPoint(b.center + new Vector3(-b.size.x, -b.size.y, -b.size.z) * 0.5f);
    vertices[1] = obj.transform.TransformPoint(b.center + new Vector3(b.size.x, -b.size.y, -b.size.z) * 0.5f);
    vertices[2] = obj.transform.TransformPoint(b.center + new Vector3(b.size.x, -b.size.y, b.size.z) * 0.5f);
    vertices[3] = obj.transform.TransformPoint(b.center + new Vector3(-b.size.x, -b.size.y, b.size.z) * 0.5f);
    vertices[4] = obj.transform.TransformPoint(b.center + new Vector3(-b.size.x, b.size.y, -b.size.z) * 0.5f);
    vertices[5] = obj.transform.TransformPoint(b.center + new Vector3(b.size.x, b.size.y, -b.size.z) * 0.5f);
    vertices[6] = obj.transform.TransformPoint(b.center + new Vector3(b.size.x, b.size.y, b.size.z) * 0.5f);
    vertices[7] = obj.transform.TransformPoint(b.center + new Vector3(-b.size.x, b.size.y, b.size.z) * 0.5f);

    return vertices;
}

Found that:

Most efficient way to get the world position of the 8 vertexes of a Box Collider (C#)

\$\endgroup\$
3
\$\begingroup\$

Collider.bounds is an AABB (Axis-Aligned Bounding Box). An AABB is always parallel to the world axes. This figure that I just drew may help with the concept: enter image description here

Therefore, it does not have the corners of your box collider in local coordinates. Rotating it creates a weird situation.

If you are happy with AABB's, then use its coordinates without rotating. If you really need the corners of your box collider, you're going to have to use BoxCollider.offset and BoxCollider.size to calculate the local coordinates of the corners and then do Transform.TransformPoint() to get their world coordinates.

Please note: When trying to find coordinates within a BoxCollider2D, one must now use BoxCollider2D.offset instead of BoxCollider2D.center, as the latter is deprecated. The answer is updated to reflect this change.

\$\endgroup\$
  • \$\begingroup\$ thank you for your answer. BoxCollider.center and BoxCollider.size only gets me to the unrotated positions. I cant reach the Corners with them alone \$\endgroup\$ – OC_RaizW Aug 24 '16 at 9:52
  • \$\begingroup\$ did you try doing Vector3 rotatedCorner = transform.rotate * unrotatedCorner;? \$\endgroup\$ – Gazihan Alankus Aug 24 '16 at 9:55
  • \$\begingroup\$ Like I mentioned above, Transform.TransformPoint(unRotatedCorner) should give you the corners in world coordinates. \$\endgroup\$ – Gazihan Alankus Aug 24 '16 at 10:35
  • \$\begingroup\$ ok.. so: i tried "colVerts[i] = boxCol.transform.TransformPoint(colVerts[i]);" "colVerts[i] = transform.rotation * colVerts[i];" None of them works. \$\endgroup\$ – OC_RaizW Aug 24 '16 at 10:50
  • \$\begingroup\$ Well, you must be doing something wrong, then. \$\endgroup\$ – Gazihan Alankus Aug 24 '16 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.