Пример #1
0
        public static GTABoundingBox2 ComputeBoundingBox(Entity e, Vector3 offset, float scale = 0.5f)
        {
            var m  = e.Model;
            var rv = new GTABoundingBox2
            {
                Min = new GTAVector2(float.PositiveInfinity, float.PositiveInfinity),
                Max = new GTAVector2(float.NegativeInfinity, float.NegativeInfinity)
            };
            Vector3 gmin;
            Vector3 gmax;

            m.GetDimensions(out gmin, out gmax);
            var bbox = new SharpDX.BoundingBox((SharpDX.Vector3) new GTAVector(gmin), (SharpDX.Vector3) new GTAVector(gmax));
            //Console.WriteLine(bbox.GetCorners()[0]);

            /*
             * for (int i = 0; i < bbox.GetCorners().Length; ++i) {
             *  for (int j = 0; j < bbox.GetCorners().Length; ++j) {
             *      if (j == i) continue;
             *      var c1 = bbox.GetCorners()[i];
             *      var c2 = bbox.GetCorners()[j];
             *      HashFunctions.Draw3DLine(e.GetOffsetInWorldCoords(new Vector3(c1.X, c1.Y, c1.Z)), e.GetOffsetInWorldCoords(new Vector3(c2.X, c2.Y, c2.Z)), 0,0);
             *  }
             * }
             */
            /*
             * for (int i = 0; i < bbox.GetCorners().Length; ++i)
             * {
             *  var corner = bbox.GetCorners()[i];
             *  var cornerinworld = e.GetOffsetInWorldCoords(new Vector3(corner.X, corner.Y, corner.Z));
             *
             *
             * }*/
            //UI.Notify(e.HeightAboveGround.ToString());
            var sp = HashFunctions.Convert3dTo2d(e.GetOffsetInWorldCoords(e.Position));

            foreach (var corner in bbox.GetCorners())
            {
                var c = new Vector3(corner.X, corner.Y, corner.Z);

                c = e.GetOffsetInWorldCoords(c);
                var s = HashFunctions.Convert3dTo2d(c);
                if (s.X == -1f || s.Y == -1f)
                {
                    rv.Min.X = float.PositiveInfinity;
                    rv.Max.X = float.NegativeInfinity;
                    rv.Min.Y = float.PositiveInfinity;
                    rv.Max.Y = float.NegativeInfinity;
                    return(rv);
                }

                /*
                 * if(s.X == -1) {
                 *  if (sp.X < 0.5) s.X = 0f;
                 *  if (sp.X >= 0.5) s.X = 1f;
                 * }
                 * if(s.Y == -1) {
                 *  if (sp.Y < 0.5) s.Y = 0f;
                 *  if (sp.Y >= 0.5) s.Y = 1f;
                 * }
                 */
                rv.Min.X = Math.Min(rv.Min.X, s.X);
                rv.Min.Y = Math.Min(rv.Min.Y, s.Y);
                rv.Max.X = Math.Max(rv.Max.X, s.X);
                rv.Max.Y = Math.Max(rv.Max.Y, s.Y);
            }

//            int width = 1280;
//            int height = 960;
//            int x = (int)(rv.Min.X * width);
//            int y = (int)(rv.Min.Y * height);
//            int x2 = (int)(rv.Max.X * width);
//            int y2 = (int)(rv.Max.Y * height);
//            float w = rv.Max.X - rv.Min.X;
//            float h = rv.Max.Y - rv.Min.Y;
//            HashFunctions.DrawRect(rv.Min.X + w/2, rv.Min.Y + h/2, rv.Max.X - rv.Min.X, rv.Max.Y - rv.Min.Y, 255, 255, 255, 100);
//            new UIRectangle(new Point((int)(rv.Min.X * 1920), (int)(rv.Min.Y * 1080)), rv.)
            return(rv);
        }
Пример #2
0
        public static GTABoundingBox2 ComputeBoundingBox(Entity e, Vector3 offset, float scale = 0.5f)
        {
            var m  = e.Model;
            var rv = new GTABoundingBox2
            {
                Min = new GTAVector2(float.PositiveInfinity, float.PositiveInfinity),
                Max = new GTAVector2(float.NegativeInfinity, float.NegativeInfinity)
            };
            Vector3 gmin;
            Vector3 gmax;

            m.GetDimensions(out gmin, out gmax);
            var bbox = new SharpDX.BoundingBox((SharpDX.Vector3) new GTAVector(gmin), (SharpDX.Vector3) new GTAVector(gmax));
            //Console.WriteLine(bbox.GetCorners()[0]);

            /*
             * for (int i = 0; i < bbox.GetCorners().Length; ++i) {
             *  for (int j = 0; j < bbox.GetCorners().Length; ++j) {
             *      if (j == i) continue;
             *      var c1 = bbox.GetCorners()[i];
             *      var c2 = bbox.GetCorners()[j];
             *      HashFunctions.Draw3DLine(e.GetOffsetInWorldCoords(new Vector3(c1.X, c1.Y, c1.Z)), e.GetOffsetInWorldCoords(new Vector3(c2.X, c2.Y, c2.Z)), 0,0);
             *  }
             * }
             */
            /*
             * for (int i = 0; i < bbox.GetCorners().Length; ++i)
             * {
             *  var corner = bbox.GetCorners()[i];
             *  var cornerinworld = e.GetOffsetInWorldCoords(new Vector3(corner.X, corner.Y, corner.Z));
             *
             *
             * }*/
            //UI.Notify(e.HeightAboveGround.ToString());
            var sp = HashFunctions.Convert3dTo2d(e.GetOffsetInWorldCoords(e.Position));

            foreach (var corner in bbox.GetCorners())
            {
                var c = new Vector3(corner.X, corner.Y, corner.Z);

                c = e.GetOffsetInWorldCoords(c);
                var s = HashFunctions.Convert3dTo2d(c);
                if (s.X == -1f || s.Y == -1f)
                {
                    rv.Min.X = float.PositiveInfinity;
                    rv.Max.X = float.NegativeInfinity;
                    rv.Min.Y = float.PositiveInfinity;
                    rv.Max.Y = float.NegativeInfinity;
                    return(rv);
                }

                /*
                 * if(s.X == -1) {
                 *  if (sp.X < 0.5) s.X = 0f;
                 *  if (sp.X >= 0.5) s.X = 1f;
                 * }
                 * if(s.Y == -1) {
                 *  if (sp.Y < 0.5) s.Y = 0f;
                 *  if (sp.Y >= 0.5) s.Y = 1f;
                 * }
                 */
                rv.Min.X = Math.Min(rv.Min.X, s.X);
                rv.Min.Y = Math.Min(rv.Min.Y, s.Y);
                rv.Max.X = Math.Max(rv.Max.X, s.X);
                rv.Max.Y = Math.Max(rv.Max.Y, s.Y);
            }

            float w = rv.Max.X - rv.Min.X;
            float h = rv.Max.Y - rv.Min.Y;

//            just for debug purposes, show visible and not visible entities in other color
//            if (CheckVisible(e))
//            {
//                HashFunctions.DrawRect(rv.Min.X + w / 2, rv.Min.Y + h / 2, rv.Max.X - rv.Min.X, rv.Max.Y - rv.Min.Y,
//                    Color.White, 100);
//            }
//            else
//            {
//                HashFunctions.DrawRect(rv.Min.X + w / 2, rv.Min.Y + h / 2, rv.Max.X - rv.Min.X, rv.Max.Y - rv.Min.Y,
//                    Color.Red, 100);
//            }
//            HashFunctions.DrawRect(rv.Min.X + w/2, rv.Min.Y + h/2, rv.Max.X - rv.Min.X, rv.Max.Y - rv.Min.Y, 255, 255, 255, 100);
            return(rv);
        }