Пример #1
0
        public Vector2 GetReflection(Vector2 velocity, Collider other)
        {
            if (other.GetType() == typeof(BoxCollider))
            {
                BoxCollider box       = (BoxCollider)other;
                Vector2     middleDis = new Vector2(Math.Abs(Transform.Position.x - box.Transform.Position.x),
                                                    Math.Abs(Transform.Position.y - box.Transform.Position.y));

                if (Math.Abs(middleDis.x - box.Size.x / 2) <= Math.Abs(middleDis.y - box.Size.y / 2))
                {
                    return(new Vector2(velocity.x * (-1), velocity.y));
                }

                return(new Vector2(velocity.x, velocity.y * (-1)));
            }

            if (other.GetType() == typeof(CircleCollider))
            {
                CircleCollider circle    = (CircleCollider)other;
                Vector2        middleDis = new Vector2(Transform.Position.x - circle.Transform.Position.x,
                                                       Transform.Position.y - circle.Transform.Position.y);
                //siehe OneNote CCB > CCB > Reflection für grafische Darstellung der Herleitung der Gleichung
                Vector2 reflection = Vector2.Dot(middleDis, velocity) * 2 * middleDis - velocity;
                return(reflection);
            }
            Server.Instance.Log("This is a Bug in GetReflection.. ur welcome :3");
            Server.Instance.Log("velocity:" + velocity);
            Server.Instance.Log("object of collision" + other);
            return(Vector2.down);
        }
Пример #2
0
        public static bool CollideCircleAndBoxColliders(BoxCollider box, CircleCollider circle)
        {
            float distX = Math.Abs(circle.Transform.Position.x - box.Transform.Position.x);
            float distY = Math.Abs(circle.Transform.Position.y - box.Transform.Position.y);

            //If the distance is greater than halfCircle + halfRect, then they are too far apart to be colliding
            if (distX > (box.Size.x / 2 + circle.Radius) || distY > (box.Size.y / 2 + circle.Radius))
            {
                return(false);
            }

            if (distX <= (box.Size.x / 2) || distY <= (box.Size.y / 2))
            {
                return(true);
            }

            double cornerdistanceSqr = Math.Pow((distX - box.Size.x / 2), 2) + Math.Pow((distY - box.Size.y / 2), 2);

            return(cornerdistanceSqr <= Math.Pow((circle.Radius), 2));
        }