Esempio n. 1
0
        //Generate a bounding box from a mesh in world space
        //Is similar to AABB but takes orientation into account so is sometimes smaller
        //which is useful for collision detection
        public Box(Mesh mesh, Transform meshTrans)
        {
            Bounds bounds = mesh.bounds;

            Vector3 halfSize = bounds.extents;

            Vector3 xVec = Vector3.right * halfSize.x;
            Vector3 yVec = Vector3.up * halfSize.y;
            Vector3 zVec = Vector3.forward * halfSize.z;

            Vector3 top    = bounds.center + yVec;
            Vector3 bottom = bounds.center - yVec;

            Vector3 topFR = top + zVec + xVec;
            Vector3 topFL = top + zVec - xVec;
            Vector3 topBR = top - zVec + xVec;
            Vector3 topBL = top - zVec - xVec;

            Vector3 bottomFR = bottom + zVec + xVec;
            Vector3 bottomFL = bottom + zVec - xVec;
            Vector3 bottomBR = bottom - zVec + xVec;
            Vector3 bottomBL = bottom - zVec - xVec;


            //Local to world space
            topFR = meshTrans.TransformPoint(topFR);
            topFL = meshTrans.TransformPoint(topFL);
            topBR = meshTrans.TransformPoint(topBR);
            topBL = meshTrans.TransformPoint(topBL);

            bottomFR = meshTrans.TransformPoint(bottomFR);
            bottomFL = meshTrans.TransformPoint(bottomFL);
            bottomBR = meshTrans.TransformPoint(bottomBR);
            bottomBL = meshTrans.TransformPoint(bottomBL);

            this.topFR = topFR.ToMyVector3();
            this.topFL = topFL.ToMyVector3();
            this.topBR = topBR.ToMyVector3();
            this.topBL = topBL.ToMyVector3();

            this.bottomFR = bottomFR.ToMyVector3();
            this.bottomFL = bottomFL.ToMyVector3();
            this.bottomBR = bottomBR.ToMyVector3();
            this.bottomBL = bottomBL.ToMyVector3();
        }