示例#1
0
        /// <summary>
        ///     Generar OBB a partir de AABB
        /// </summary>
        /// <param name="aabb">BoundingBox</param>
        /// <returns>OBB generado</returns>
        public static OBBStruct computeFromAABB(TgcBoundingAxisAlignBox.AABBStruct aabb)
        {
            var obb = new OBBStruct();

            obb.extents = (aabb.max - aabb.min) * 0.5f;
            obb.center  = aabb.min + obb.extents;

            obb.orientation = new[] { new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1) };
            return(obb);
        }
示例#2
0
        /// <summary>
        ///     Interseccion entre un Ray y un AABB
        /// </summary>
        /// <param name="ray">Ray</param>
        /// <param name="aabb">AABB</param>
        /// <param name="tmin">Instante minimo de colision</param>
        /// <param name="q">Punto minimo de colision</param>
        /// <returns>True si hay colision</returns>
        private bool intersectRayAABB(TgcRay.RayStruct ray, TgcBoundingAxisAlignBox.AABBStruct aabb, out float tmin,
                                      out TGCVector3 q)
        {
            var aabbMin = TgcCollisionUtils.toArray(aabb.min);
            var aabbMax = TgcCollisionUtils.toArray(aabb.max);
            var p       = TgcCollisionUtils.toArray(ray.origin);
            var d       = TgcCollisionUtils.toArray(ray.direction);

            tmin = 0.0f;               // set to -FLT_MAX to get first hit on line
            var tmax = float.MaxValue; // set to max distance ray can travel (for segment)

            q = TGCVector3.Empty;

            // For all three slabs
            for (var i = 0; i < 3; i++)
            {
                if (FastMath.Abs(d[i]) < float.Epsilon)
                {
                    // Ray is parallel to slab. No hit if origin not within slab
                    if (p[i] < aabbMin[i] || p[i] > aabbMax[i])
                    {
                        return(false);
                    }
                }
                else
                {
                    // Compute intersection t value of ray with near and far plane of slab
                    var ood = 1.0f / d[i];
                    var t1  = (aabbMin[i] - p[i]) * ood;
                    var t2  = (aabbMax[i] - p[i]) * ood;
                    // Make t1 be intersection with near plane, t2 with far plane
                    if (t1 > t2)
                    {
                        TgcCollisionUtils.swap(ref t1, ref t2);
                    }
                    // Compute the intersection of slab intersection intervals
                    tmin = TgcCollisionUtils.max(tmin, t1);
                    tmax = TgcCollisionUtils.min(tmax, t2);
                    // Exit with no collision as soon as slab intersection becomes empty
                    if (tmin > tmax)
                    {
                        return(false);
                    }
                }
            }
            // Ray intersects all 3 slabs. Return point (q) and intersection t value (tmin)
            q = ray.origin + ray.direction * tmin;
            return(true);
        }