Example #1
0
        /// <summary>
        /// Crear Collider a partir de BoundingBox.
        /// Crea el BoundingSphere del Collider.
        /// </summary>
        /// <param name="mesh">BoundingBox</param>
        /// <returns>Collider creado</returns>
        public static BoundingBoxCollider fromBoundingBox(TgcBoundingBox aabb)
        {
            BoundingBoxCollider collider = new BoundingBoxCollider();

            collider.aabb           = aabb;
            collider.BoundingSphere = TgcBoundingSphere.computeFromPoints(aabb.computeCorners()).toClass();
            return(collider);
        }
Example #2
0
        /*
         * /// <summary>
         * /// Proyecta un BoundingBox a un rectangulo 2D de screen space
         * /// </summary>
         * public static Rectangle projectAABB(TgcBoundingBox aabb)
         * {
         *  return aabb.projectToScreen();
         * }
         */

        /// <summary>
        /// Proyectar AABB a 2D
        /// </summary>
        /// <param name="box3d">BoundingBox 3D</param>
        /// <param name="box2D">Rectangulo 2D proyectado</param>
        /// <returns>False si es un caso degenerado de proyeccion y no debe considerarse</returns>
        public static bool projectBoundingBox(TgcBoundingBox box3d, out Rectangle box2D)
        {
            //Datos de viewport
            Device   d3dDevice = GuiController.Instance.D3dDevice;
            Viewport viewport  = d3dDevice.Viewport;
            Matrix   view      = d3dDevice.Transform.View;
            Matrix   proj      = d3dDevice.Transform.Projection;
            int      width     = viewport.Width;
            int      height    = viewport.Height;

            box2D = new Rectangle();

            //Proyectar los 8 puntos, sin dividir aun por W
            Vector3[] corners = box3d.computeCorners();
            Matrix    m       = view * proj;

            Vector3[] projVertices = new Vector3[corners.Length];
            for (int i = 0; i < corners.Length; i++)
            {
                Vector4 pOut = Vector3.Transform(corners[i], m);
                if (pOut.W < 0)
                {
                    return(false);
                }
                projVertices[i] = MeshCreatorUtils.toScreenSpace(pOut, width, height);
            }

            //Buscar los puntos extremos
            Vector2 min      = new Vector2(float.MaxValue, float.MaxValue);
            Vector2 max      = new Vector2(float.MinValue, float.MinValue);
            float   minDepth = float.MaxValue;

            foreach (Vector3 v in projVertices)
            {
                if (v.X < min.X)
                {
                    min.X = v.X;
                }
                if (v.Y < min.Y)
                {
                    min.Y = v.Y;
                }
                if (v.X > max.X)
                {
                    max.X = v.X;
                }
                if (v.Y > max.Y)
                {
                    max.Y = v.Y;
                }

                if (v.Z < minDepth)
                {
                    minDepth = v.Z;
                }
            }


            //Clamp
            if (min.X < 0f)
            {
                min.X = 0f;
            }
            if (min.Y < 0f)
            {
                min.Y = 0f;
            }
            if (max.X >= width)
            {
                max.X = width - 1;
            }
            if (max.Y >= height)
            {
                max.Y = height - 1;
            }

            //Control de tamaƱo minimo
            if (max.X - min.X < 1f)
            {
                return(false);
            }
            if (max.Y - min.Y < 1f)
            {
                return(false);
            }

            //Cargar valores de box2D
            box2D.Location = new Point((int)min.X, (int)min.Y);
            box2D.Size     = new Size((int)(max.X - min.X), (int)(max.Y - min.Y));
            return(true);
        }