- Home /
Box Collider Vertexes in World Space
How do I get the vertexes of a box collider in world space? (with correct rotation and scale).
I'm not sure about the collider, var the$$anonymous$$eshCollider = theCube.GetComponent($$anonymous$$eshCollider).shared$$anonymous$$esh;
throws an error for a box collider. But for the mesh try this little mesh reader I just wrote =]
#pragma strict
public var theCube : Transform;
private var theVerts : String = "";
private var theUVs : String = "";
private var theNorms : String = "";
private var theTris : String = "";
function Start () {
var the$$anonymous$$esh : $$anonymous$$esh = theCube.GetComponent($$anonymous$$eshFilter).mesh as $$anonymous$$esh;
theVerts = theVerts + "(" + the$$anonymous$$esh.vertices.length.ToString() + ") :";
theUVs = theUVs + "(" + the$$anonymous$$esh.uv.length.ToString() + ") :";
theNorms = theNorms + "(" + the$$anonymous$$esh.normals.length.ToString() + ") :";
theTris = theTris + "(" + the$$anonymous$$esh.triangles.length.ToString() + ") :";
for (var i:int = 0; i < the$$anonymous$$esh.vertices.length; i ++) {
theVerts = theVerts + " " + i + "=" + the$$anonymous$$esh.vertices[i].ToString();
theUVs = theUVs + " " + i + "=" + the$$anonymous$$esh.uv[i].ToString();
theNorms = theNorms + " " + i + "=" + the$$anonymous$$esh.normals[i].ToString();
}
for (i = 0; i < the$$anonymous$$esh.triangles.length; i ++) {
theTris = theTris + " " + i + "=" + the$$anonymous$$esh.triangles[i].ToString();
}
}
function OnGUI () {
GUI.Label( Rect( 10, 10, Screen.width - 20, 60), "Verts " + theVerts );
GUI.Label( Rect( 10, 70, Screen.width - 20, 60), "UVs " + theUVs );
GUI.Label( Rect( 10, 130, Screen.width - 20, 60), "Normals " + theNorms );
GUI.Label( Rect( 10, 190, Screen.width - 20, 60), "Tris " + theTris );
}
Can any of the pro's help with a hint/nudge? =]
Answer by ScroodgeM · Jul 20, 2012 at 06:41 PM
using UnityEngine; using System.Collections; public class BoxColliderVerticesLogger : MonoBehaviour { //object with box collider must be assigned here public BoxCollider BoxColliderObject; private Transform[] markers; void Start() { //creating 8 spheres to mark vertices markers = new Transform[8]; for (int i = 0; i < 8; i++) { markers[i] = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform; } } void Update() { if (BoxColliderObject) { //get local collider center Vector3 boxColliderCenter = BoxColliderObject.center; //get local collider extents Vector3 boxColliderExtents = BoxColliderObject.extents; for (int i = 0; i < 8; i++) { //get one of vertice offset from center Vector3 ext = boxColliderExtents; ext.Scale(new Vector3((i & 1) == 0 ? 1 : -1, (i & 2) == 0 ? 1 : -1, (i & 4) == 0 ? 1 : -1)); //calc local vertice position Vector3 vertPositionLocal = boxColliderCenter + ext; //move sphere to global vertice position markers[i].position = BoxColliderObject.transform.TransformPoint(vertPositionLocal); } } } }
Just wanted to say thanks for this! Tried some other methods but this is working absolutely perfectly. Thanks :)
It's quite handy that BoxColliders store their size and extents in local coordinates. I was trying to get the positions of the collider vertices using the values from the Collider.bounds bounding box and perfor$$anonymous$$g a number of transformations, but this method is clearly superior.
Thank you very much! I've tried several ways to achieve this. While all of them worked when the object with the collider had no rotation, this one works in every situation!
Answer by ScroodgeM · Jul 20, 2012 at 10:30 AM
primitive colliders (sphere, capsule, box, etc) are calculated by unique very cheap way and it can have no any vertices at all
for example, to detect collision between two spheres you just need to (not a code! just for understand method!)
currentDistanceSquared = (collider1.position - collider2.position).sqrMagnitude; collisionDistanceSquared = Mathf.sqr(collider1.radius + collider2.radius); if (collisionDistanceSquared < distanceSquared) { [COLLISION HAPPENED] }
so vertices you can get only from mesh collider
for box collider anyway you can get "vertices" by calculating them. all you need is found them in local space of transform that have collider and then translate it to world space.
I need to know the vertices of the box collider, how do I calculate them? Just doing local -> world on the extents does not work.
I need to get the vertices of the box collider. When I try to do local-world on them it does not work. It does not properly account for scale.
for future post your code example, try to complete your task yourself, don't wait somebody else do it for you. 8)
take a script - it creates (and dynamically updates) 8 spheres in vertices of boxCollider
Answer by dpolyakov · Mar 07, 2016 at 11:11 AM
for Unity5 you just need to replace
Vector3 boxColliderExtents = BoxColliderObject.extents;
with this:
Vector3 boxColliderExtents = b.size / 2.0f;
That's only valid if your scales are all 1 in the hierarchy. AND aligned to world axes. Sorry but this answer is just wrong on many levels.
No, it's not wrong. It's just an extension of @Scroodge$$anonymous$$'s answer. The scale, offset and rotation is applied afterwards by "TransformPoint". He just posted this answer (which probably should be a comment) to inform people that the "extents" property of the BoxCollider component has been marked as "obsolete" so it shouldn't be used anymore as it might get removed in the future. The extents property is actually declared like this:
[Obsolete("use BoxCollider.size ins$$anonymous$$d.")]
public Vector3 extents
{
get { return this.size * 0.5f; }
set { this.size = value * 2f; }
}