private static CollisionDetails doOnlyYMovement(ref Vector2 velocity, CollisionDetails ret, ref Vector2 slidingYPosition)
 {
     velocity.X *= -0.5f; //bounce from wall
     ret.m_speedAfterCollision    = velocity;
     ret.m_positionAfterCollision = slidingYPosition;
     return(ret);
 }
        private static CollisionDetails doStandStill(CollisionDetails ret, Vector2 velocity)
        {
            if (velocity.Y > 0)
            {
                ret.m_hasCollidedWithGround = true;
            }

            ret.m_speedAfterCollision = new Vector2(0, 0);

            return(ret);
        }
        private static CollisionDetails doOnlyXMovement(ref Vector2 velocity, CollisionDetails ret, ref Vector2 slidingXPosition)
        {
            ret.m_positionAfterCollision = slidingXPosition;
            //did we slide on ground?
            if (velocity.Y > 0)
            {
                ret.m_hasCollidedWithGround = true;
            }

            ret.m_speedAfterCollision = doSetSpeedOnVerticalCollision(velocity);
            return(ret);
        }
Beispiel #4
0
        private void UpdatePlayer(float totalElapsedSeconds)
        {
            Vector2 oldPosition = m_player.Position;

            m_player.Update(totalElapsedSeconds);
            Vector2 newPosition = m_player.Position;

            m_hasCollidedWithGround = false;
            Vector2 velocity = m_player.Velocity;

            if (CollisionHandler.didCollide(newPosition, m_player.Size, m_level))
            {
                CollisionDetails details = CollisionHandler.getCollisionDetails(oldPosition, newPosition, m_player.Size, velocity, m_level);
                m_hasCollidedWithGround = details.m_hasCollidedWithGround;

                m_player.Position = details.m_positionAfterCollision;
                m_player.Velocity = details.m_speedAfterCollision;
            }
        }
        public static CollisionDetails getCollisionDetails(Vector2 oldPos, Vector2 newPosition, Vector2 size, Vector2 velocity, Level level)
        {
            CollisionDetails ret = new CollisionDetails(oldPos, velocity);

            Vector2 slidingXPosition = new Vector2(newPosition.X, oldPos.Y); //Y movement ignored
            Vector2 slidingYPosition = new Vector2(oldPos.X, newPosition.Y); //X movement ignored

            if (didCollide(slidingXPosition, size, level) == false)
            {
                return(doOnlyXMovement(ref velocity, ret, ref slidingXPosition));
            }

            else if (didCollide(slidingYPosition, size, level) == false)
            {
                return(doOnlyYMovement(ref velocity, ret, ref slidingYPosition));
            }

            else
            {
                return(doStandStill(ret, velocity));
            }
        }