6
\$\begingroup\$

What I am looking for is the most efficient way to get the world position of the 8 vertices of the Box Collider of a freely rotated Game Object. I cannot use collider.bounds since object is rotated, not axis-aligned.

I'm using Unity 5 with C#.

\$\endgroup\$
4
  • \$\begingroup\$ Typically you make a matrix out of all the matrices that transform the game object (hierarchically), and you multiply the vertex by that matrix... Your question is quite vague. \$\endgroup\$
    – Vaillancourt
    Commented Oct 15, 2015 at 4:19
  • \$\begingroup\$ Thanks for your comment. I will try to figure out what you've said... As for my question, I don't get why it is that vague: I thought it was a very specific question to ask how to find out the world position of the 8 external vertexes of a Box Collider (my mention to a rotated Gameobject was only to prevent from receiving suggestions of using collider.bounds) \$\endgroup\$
    – Louis15
    Commented Oct 15, 2015 at 13:33
  • \$\begingroup\$ Given the tags and the text, it's hard to understand you use a framework; we only know you use c# and have a box collider, without actually knowing your implementation. Are you using Unity? This would be a very important tag to add to your question. Although this Q&A site welcomes a lot of Unity questions (and answers), this is not a Unity only stack (Unity or any other framework), so we can't assume anything about the base technology you use. This is why I said your question is vague. You should edit your question (and tags) to give a bit more details, I'm sure you'll get better answers :) \$\endgroup\$
    – Vaillancourt
    Commented Oct 15, 2015 at 14:05
  • \$\begingroup\$ Oh, now I see what you mean. Sorry for not providing such information beforehand. I thought that for this question there would be no difference between frameworks, provided that they employ C# and PhysX. But even though, I did not mention PhysX either. So I will edit it! Many thanks for your feedback \$\endgroup\$
    – Louis15
    Commented Oct 15, 2015 at 14:21

3 Answers 3

7
\$\begingroup\$

Try this:

    BoxCollider b = obj.GetComponent<BoxCollider>(); //retrieves the Box Collider of the GameObject called obj

    Vector3 vertice1 = myobject.transform.TransformPoint(b.center + new Vector3(-b.size.x, -b.size.y, -b.size.z)*0.5f);
    Vector3 vertice2 = myobject.transform.TransformPoint(b.center + new Vector3(b.size.x, -b.size.y, -b.size.z)*0.5f);
    Vector3 vertice3 = myobject.transform.TransformPoint(b.center + new Vector3(b.size.x, -b.size.y, b.size.z)*0.5f);
    Vector3 vertice4 = myobject.transform.TransformPoint(b.center + new Vector3(-b.size.x, -b.size.y, b.size.z)*0.5f);
    Vector3 vertice5 = myobject.transform.TransformPoint(b.center + new Vector3(-b.size.x, b.size.y, -b.size.z)*0.5f);
    Vector3 vertice6 = myobject.transform.TransformPoint(b.center + new Vector3(b.size.x, b.size.y, -b.size.z)*0.5f);
    Vector3 vertice7 = myobject.transform.TransformPoint(b.center + new Vector3(b.size.x, b.size.y, b.size.z)*0.5f);
    Vector3 vertice8 = myobject.transform.TransformPoint(b.center + new Vector3(-b.size.x, b.size.y, b.size.z)*0.5f);
\$\endgroup\$
3
  • \$\begingroup\$ I know this is late, but that looks very repetitive, I'd suggest moving it into an array. \$\endgroup\$
    – mr-matt
    Commented Jul 13, 2017 at 0:03
  • \$\begingroup\$ Can someone improve this answer explaining how this accounts for any rotation of the box? I don't see any orientation information being accounted for here. I'm guessing "size" on the collider has directional context since it's a Vector3, but confirmation from someone with more experience in Unity would be reassuring. \$\endgroup\$
    – Scott Lin
    Commented Feb 8, 2019 at 22:52
  • 1
    \$\begingroup\$ @ScottLin the TransformPoint function applies the orientation, translation, and scale of the object's transform (and its parent transforms) to the input point. \$\endgroup\$
    – DMGregory
    Commented Mar 21, 2023 at 21:22
2
\$\begingroup\$

If you can access a Mesh of the same size and shape, you can use Mesh.bounds, which returns positions in local space.

\$\endgroup\$
0
\$\begingroup\$
public static List<Vector3> GetBoxColliderCornersWorld(BoxCollider boxCollider)
{
    Vector3 size = boxCollider.size;
    List<Vector3> list = new();
    var signs = new List<int> { -1, 1 };
    signs.ForEach(signX => 
        signs.ForEach(signY => 
            signs.ForEach(signZ => {
                var vector = new Vector3(size.x * signX, size.y * signY, size.z * signZ);
                list.Add(boxCollider.transform.TransformPoint(boxCollider.center + vector * 0.5f));
            })));

    return list;
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .