Пример #1
0
        /// <summary>
        ///     Retorna true si hubo interseccion con el terreno y setea el collisionPoint.
        /// </summary>
        /// <param name="ray"></param>
        /// <param name="collisionPoint"></param>
        /// <returns></returns>
        public bool intersectRay(TgcRay ray, out TGCVector3 collisionPoint)
        {
            collisionPoint = TGCVector3.Empty;
            var scaleInv = TGCMatrix.Scaling(new TGCVector3(1 / ScaleXZ, 1 / ScaleY, 1 / ScaleXZ));
            var a        = TGCVector3.TransformCoordinate(ray.Origin, scaleInv) - traslation;
            var r        = TGCVector3.TransformCoordinate(ray.Direction, scaleInv);

            if (a.Y < minIntensity)
            {
                return(false);
            }

            TGCVector3 q;

            //Me fijo si intersecta con el BB del terreno.
            if (!TgcCollisionUtils.intersectRayAABB(new TgcRay(a, r).toStruct(), aabb.toStruct(), out q))
            {
                return(false);
            }

            float minT = 0;

            //Obtengo el T de la interseccion.
            if (q != a)
            {
                if (r.X != 0)
                {
                    minT = (q.X - a.X) / r.X;
                }
                else if (r.Y != 0)
                {
                    minT = (q.Y - a.Y) / r.Y;
                }
                else if (r.Z != 0)
                {
                    minT = (q.Z - a.Z) / r.Z;
                }
            }

            //Me desplazo por el rayo hasta que su altura sea menor a la del terreno en ese punto
            //o me salga del AABB.
            float t    = 0;
            float step = 1;

            for (t = minT; ; t += step)
            {
                collisionPoint = a + t * r;
                float y;

                if (!interpoledIntensity(collisionPoint.X, collisionPoint.Z, out y))
                {
                    return(false);
                }

                if (collisionPoint.Y <= y + float.Epsilon)
                {
                    collisionPoint.Y = y;
                    collisionPoint   = TGCVector3.TransformCoordinate(collisionPoint + traslation,
                                                                      TGCMatrix.Scaling(ScaleXZ, ScaleY, ScaleXZ));
                    return(true);
                }
            }
        }
Пример #2
0
 /// <summary>
 ///     Generar OBB a partir de AABB
 /// </summary>
 /// <param name="aabb">BoundingBox</param>
 /// <returns>OBB generado</returns>
 public static TgcBoundingOrientedBox computeFromAABB(TgcBoundingAxisAlignBox aabb)
 {
     return(computeFromAABB(aabb.toStruct()).toClass());
 }