Beispiel #1
0
        /// <summary>
        /// twice collision check translation
        /// </summary>
        public static void SuperTranslate(ICollisionAgent agent, Vector2 movement, IEnumerator <Box> boxEnum)
        {
            Box   box = agent.box;
            float f;

            //agent is moved on each axis separately
            if (movement.y != 0f)
            {
                f      = movement.y > 0f ? -0.5f : 0.5f;  //set multiplier based on move direction
                box.y += movement.y;                      //apply movement
                while (boxEnum.MoveNext())                //traverse collection of solid boxes
                {
                    if (Intersects(box, boxEnum.Current)) //test applied movement
                    {
                        //upon collision, place agent on appropriate edge of collider
                        box.y = boxEnum.Current.y + f * (boxEnum.Current.height + box.height + padding);
                    }
                }
            }
            //repeat above for x-axis
            if (movement.x != 0f)
            {
                boxEnum.Reset(); //reset enumerator for reuse
                f      = movement.x > 0f ? -0.5f : 0.5f;
                box.x += movement.x;
                while (boxEnum.MoveNext())
                {
                    if (Intersects(box, boxEnum.Current))
                    {
                        box.x = boxEnum.Current.x + f * (boxEnum.Current.width + box.width + padding);
                    }
                }
            }
            agent.SetPosition(box.Center); //apply final position
        }
Beispiel #2
0
 public static void SuperTranslate(ICollisionAgent mover, Vector2 movement, IEnumerable <Box> boxes)
 {
     SuperTranslate(mover, movement, boxes.GetEnumerator());
 }