private void OnTriggerEnter2D(Collider2D collision)
    {
        VectorCollider vc = collision.gameObject.GetComponent <VectorCollider>();

        if (vc != null)
        {
            movementVector = vc.vector;
        }
    }
Ejemplo n.º 2
0
        /// <summary>
        /// Check if vector (line segment) collide with a circunference in space and return the first point of the intersection
        /// </summary>
        /// <param name="vector"></param>
        /// <param name="circle"></param>
        /// <param name="intersecPoint"></param>
        /// <returns></returns>
        public bool CheckCollision(VectorCollider vector, CircleCollider circle, out Vector2 intersecPoint)
        {
            float a;
            float b;
            float c;

            intersecPoint = Vector2.Zero;

            a = Vector2.Dot(vector.Vector, vector.Vector);
            b = Vector2.Dot(vector.Start - circle.center, vector.Vector) * 2;
            c = Vector2.Dot(vector.Start - circle.center, vector.Start - circle.center) - circle.Radius * circle.Radius;

            double sqrtTerm = b * b - 4 * a * c;

            if (sqrtTerm < 0)
            {
                return(false);
            }
            else
            {
                sqrtTerm = Math.Sqrt(sqrtTerm);

                float t1 = (float)(-b - sqrtTerm) / (2 * a);
                float t2 = (float)(-b + sqrtTerm) / (2 * a);

                //t1 has preference, but if the starting point is inside the circle t2 will be the point
                if (t1 >= 0 && t2 <= 1)
                {
                    intersecPoint = vector.Start + t1 * vector.Vector;
                    return(true);
                }
                if (t2 >= 0 && t2 <= 1)
                {
                    intersecPoint = vector.Start + t2 * vector.Vector;
                    return(true);
                }
                return(false);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Check if 2 vectors collide in space and return the point of the intersection
        /// </summary>
        /// <param name="vector1">First VectorCollider to test</param>
        /// <param name="vector2">Second VectorCollider to test</param>
        /// <param name="intersecPoint">Var to store the intersection Point</param>
        /// <returns></returns>
        public bool CheckCollision(VectorCollider vector1, VectorCollider vector2, out Vector2 intersecPoint)
        {
            var div = (vector1.Vector.X * vector2.Vector.Y - vector1.Start.Y * vector2.Vector.X);

            intersecPoint = Vector2.Zero;

            if (div == 0)
            {
                return(false);
            }

            var t1 = (vector2.Vector.Y * (vector2.Start.X - vector1.Start.X) - vector2.Vector.X * (vector2.Start.Y - vector1.Start.Y)) / div;
            var t2 = (vector1.Vector.Y * (vector2.Start.X - vector1.Start.X) - vector1.Vector.X * (vector2.Start.Y - vector1.Start.Y)) / div;

            if (t2 >= 0 && t2 <= 1 && t1 >= 0 && t1 <= 1)
            {
                intersecPoint = vector2.Start + t2 * vector2.Vector;
                return(true);
            }
            else
            {
                return(false);
            }
        }