Exemple #1
0
        public Vector2 Collide(ICollidable obj, out bool hit)
        {
            hit = false;
            floatRectangle objInitRect = obj.GetBoundingBox();
            Vector2 objVel = obj.GetVelocityVector();
            floatRectangle objRect = new floatRectangle(objInitRect.X + objVel.X,
                objInitRect.Y + objVel.Y, objInitRect.Width, objInitRect.Height);

            if (objRect.Top() > m_tileHeight * m_height || objRect.Bottom() < 0
                || objRect.Left() > m_tileWidth * m_width || objRect.Right() < 0)
                return objVel;

            Rectangle tileRect = new Rectangle();
            tileRect.X = (int)objRect.X / m_tileWidth;
            tileRect.Y = (int)objRect.Y / m_tileHeight;
            tileRect.Width = (int)objRect.Width / m_tileWidth;
            tileRect.Height = (int)objRect.Height / m_tileHeight;

            floatRectangle objRectX = new floatRectangle(objInitRect.X + objVel.X, objInitRect.Y + 1,
                objInitRect.Width, objInitRect.Height - 1);
            floatRectangle objRectY = new floatRectangle(objInitRect.X + 1, objInitRect.Y + objVel.Y,
                objInitRect.Width - 1, objInitRect.Height);

            if (tileRect.X < 0) tileRect.X = 0;
            if (tileRect.Y < 0) tileRect.Y = 0;
            if (tileRect.Right >= m_width)
                tileRect.Width = m_width - tileRect.X - 1;
            if (tileRect.Bottom >= m_height)
                tileRect.Height = m_height - tileRect.Y - 1;

            for (int j = tileRect.Top; j <= tileRect.Bottom; ++j)
            {
                for (int i = tileRect.Left; i <= tileRect.Right; ++i)
                {
                    if (m_tiles[m_map[i, j]].m_type == Tile.TileType.NONE
                        || m_tiles[m_map[i, j]].m_type == Tile.TileType.LADDER)
                        continue;

                    floatRectangle currRect = new floatRectangle(i * m_tileWidth, j * m_tileHeight,
                        m_tileWidth, m_tileHeight);

                    if (objVel.X > 0)
                    {
                        if (objRectX.Right() > currRect.Left()
                            && objRectX.Top() < currRect.Bottom() && objRectX.Bottom() > currRect.Top())
                        {
                            hit = true;
                            objVel.X = currRect.Left() - objInitRect.Right();
                            if (objVel.X < 0)
                                objVel.X = 0;
                        }
                    }
                    else
                    {
                        if (objRectX.Left() < currRect.Right()
                            && objRectX.Top() < currRect.Bottom() && objRectX.Bottom() > currRect.Top())
                        {
                            hit = true;
                            objVel.X = currRect.Right() - objInitRect.Left();
                            if (objVel.X > 0)
                                objVel.X = 0;
                        }
                    }
                    if (objVel.Y > 0)
                    {
                        if (objRectY.Bottom() > currRect.Top()
                            && objRectY.Left() < currRect.Right() && objRectY.Right() > currRect.Left())
                        {
                            hit = true;
                            objVel.Y = currRect.Top() - objInitRect.Bottom();
                            if (objVel.Y < 0)
                                objVel.Y = 0;
                        }
                    }
                    //else
                    //{
                    //    hit = true;
                    //    if (objRectY.Top() < currRect.Bottom()
                    //        && objRectY.Left() < currRect.Right() && objRectY.Right() > currRect.Left())
                    //    {
                    //        objVel.Y = currRect.Bottom() - objInitRect.Top();
                    //        if (objVel.Y > 0)
                    //            objVel.Y = 0;
                    //    }
                    //}
                }
            }
            return objVel;
        }
Exemple #2
0
        public bool WillCollide(ICollidable other)
        {
            floatRectangle theirs = other.GetBoundingBox();
            Vector2 vel = other.GetVelocityVector();

            floatRectangle mine = new floatRectangle(m_rect.X + m_vec.X,
                m_rect.Y + m_vec.X, m_rect.Width, m_rect.Height);

            theirs.X += vel.X;
            theirs.Y += vel.Y;

            if (mine.Right() > theirs.Left() && mine.Left() < theirs.Right()
                && mine.Bottom() > theirs.Top() && mine.Top() < theirs.Bottom())
            {
                return true;
            }
            return false;
        }