Exemple #1
0
        protected Entity(Vector2 pos)
        {
            Pos = LastPos = pos;

            BoundingBox          = AxisAlignedBB.Null;
            CollisionBoundingBox = AxisAlignedBB.Null;
        }
        public float CalculateYOffset(AxisAlignedBB other, float offset)
        {
            if (other.Max.X > Min.X && other.Min.X < Max.X)
            {
                if (offset > 0.0D && other.Max.Y <= Min.Y)
                {
                    float d1 = Min.Y - other.Max.Y;

                    if (d1 < offset)
                    {
                        offset = d1;
                    }
                }
                else if (offset < 0.0D && other.Min.Y >= Max.Y)
                {
                    float d0 = Max.Y - other.Min.Y;

                    if (d0 > offset)
                    {
                        offset = d0;
                    }
                }
            }

            return(offset);
        }
Exemple #3
0
        public EntityPlayer(float x, float y, float size, Color color) : base(new Vector2(x, y))
        {
            _size = size;

            CollisionBoundingBox = new AxisAlignedBB(size);
            BoundingBox          = CollisionBoundingBox.Offset(Pos - (Vector2.UnitX * CollisionBoundingBox.Size.X / 2 + Vector2.UnitY * CollisionBoundingBox.Size.Y / 2));
        }
        public AxisAlignedBB Union(AxisAlignedBB other)
        {
            var minX = (int)Math.Floor(MathUtil.Min(Min.X, Max.X, other.Min.X, other.Max.X));
            var minY = (int)Math.Floor(MathUtil.Min(Min.Y, Max.Y, other.Min.Y, other.Max.Y));

            var maxX = (int)Math.Ceiling(MathUtil.Max(Min.X, Max.X, other.Min.X, other.Max.X));
            var maxY = (int)Math.Ceiling(MathUtil.Max(Min.Y, Max.Y, other.Min.Y, other.Max.Y));

            return(new AxisAlignedBB(minX, minY, maxX, maxY));
        }
Exemple #5
0
        public virtual void Move()
        {
            var bbO = BoundingBox.Union(BoundingBox.Offset(Motion));

            List <AxisAlignedBB> list = Game.Instance.Map.GetCollidingBoxes(bbO);

            var mOrig = Motion;

            for (int i = 0; i < list.Count; i++)
            {
                var blockBb = list[i];
                Motion.Y = blockBb.CalculateYOffset(BoundingBox, Motion.Y);
            }
            BoundingBox = BoundingBox.Offset(Motion * Vector2.UnitY);

            for (int i = 0; i < list.Count; i++)
            {
                var blockBb = list[i];
                Motion.X = blockBb.CalculateXOffset(BoundingBox, Motion.X);
            }
            BoundingBox = BoundingBox.Offset(Motion * Vector2.UnitX);

            SetPositionToBb();

            var stoppedX = Math.Abs(mOrig.X - Motion.X) > 0.00001f;
            var stoppedY = Math.Abs(mOrig.Y - Motion.Y) > 0.00001f;

            if (stoppedX)
            {
                Motion.X = 0;
            }

            if (stoppedY)
            {
                Motion.Y = 0;
            }
        }
 public bool IntersectsWith(AxisAlignedBB other)
 {
     return(Min.X < other.Max.X && Max.X > other.Min.X &&
            Min.Y < other.Max.Y && Max.Y > other.Min.Y);
 }
Exemple #7
0
        public void TeleportTo(Vector2 pos)
        {
            LastPos = Pos = pos;

            BoundingBox = CollisionBoundingBox.Offset(Pos - Vector2.UnitX * CollisionBoundingBox.Size.X / 2 - Vector2.UnitY * CollisionBoundingBox.Size.Y / 2);
        }
Exemple #8
0
        public List <AxisAlignedBB> GetCollidingBoxes(AxisAlignedBB box)
        {
            var bb = box.Union(box);

            return(_collisionBoxes.Where(cb => cb.IntersectsWith(bb)).ToList());
        }
Exemple #9
0
        public static bool Intersects(AxisAlignedBB box, Vector2 pos, Vector2 dir, out float f)
        {
            float num      = 0f;
            float maxValue = float.MaxValue;

            if (Math.Abs(dir.X) < 1E-06f)
            {
                if (pos.X < box.Min.X || pos.X > box.Max.X)
                {
                    f = 0;
                    return(false);
                }
            }
            else
            {
                float num11 = 1f / dir.X;
                float num8  = (box.Min.X - pos.X) * num11;
                float num7  = (box.Max.X - pos.X) * num11;
                if (num8 > num7)
                {
                    float num14 = num8;
                    num8 = num7;
                    num7 = num14;
                }
                num      = Math.Max(num8, num);
                maxValue = Math.Min(num7, maxValue);
                if (num > maxValue)
                {
                    f = 0;
                    return(false);
                }
            }
            if (Math.Abs(dir.Y) < 1E-06f)
            {
                if (pos.Y < box.Min.Y || pos.Y > box.Max.Y)
                {
                    f = 0;
                    return(false);
                }
            }
            else
            {
                float num10 = 1f / dir.Y;
                float num6  = (box.Min.Y - pos.Y) * num10;
                float num5  = (box.Max.Y - pos.Y) * num10;
                if (num6 > num5)
                {
                    float num13 = num6;
                    num6 = num5;
                    num5 = num13;
                }
                num      = Math.Max(num6, num);
                maxValue = Math.Min(num5, maxValue);
                if (num > maxValue)
                {
                    f = 0;
                    return(false);
                }
            }

            f = num;

            return(true);
        }