5
\$\begingroup\$

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

As for the framework I am working within, it's Unity 5 with C#.

Would you know how could I achieve that? Thanks in advance.

\$\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
    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
    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
    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
    Oct 15, 2015 at 14:21

2 Answers 2

6
\$\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\$
2
  • \$\begingroup\$ I know this is late, but that looks very repetitive, I'd suggest moving it into an array. \$\endgroup\$
    – mr-matt
    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
    Feb 8, 2019 at 22:52
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\$

You must log in to answer this question.

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