perpendicular 2D vector

Hi (sorry for bad english),
I´m trying to calculate 2D Vector, which is perpendicular to a vector between points B and C (see image) and which intersects point A. Problem is that i don´t know position of point D.
How to do it in code?
2248566--150206--vector.png

http://docs.unity3d.com/Manual/ComputingNormalPerpendicularVector.html

the “second” vector would be into/outof the screen (not sure what that is in 2d engine axis :slight_smile: )

Sorry for dumb question, but how to use this function?
Which two vectors should i give as a parameter?
And does it return vector in global or local world space?

The cross product calculates a vector perpendicular to two input vectors. In this case there is only one input vector and a point. What you essentially want in this case is to calculate the point closest to point A on line BC. This point will be D.

So I shouldn´t use Vector3.cross? Maybe i should give as parameters distance between B and C and distance between B and A.

Or you can find vector AB, project it on BC and get BD

@jvo3dc
nonsense, there are still 3 dimensions in a 2d project:

using UnityEngine;
using System.Collections;

public class LineTesting : MonoBehaviour
{
    public LineRenderer lr;

    public GameObject pointA;
    public GameObject pointB;

    [Range(0, 1)]
    public float distanceAlongLine;

    // Use this for initialization
    void Start ()
    {
        // to make it show up
        lr = gameObject.AddComponent<LineRenderer>();
        lr.SetVertexCount(5);
        lr.SetWidth(0.1f, 0.1f);
    }

    void Update()
    {
        // work out the point along the line
        Vector3 pointOnLine = pointA.transform.position + ((pointB.transform.position - pointA.transform.position) * distanceAlongLine);

        // work out the sides
        // vector B-C in your diagram
        Vector3 side1 = pointB.transform.position - pointA.transform.position;
        // into/outof the 2d plane
        Vector3 side2 = Vector3.forward;

        // get the perpendicular direction
        Vector3 normalDirection = Vector3.Cross(side1, side2).normalized;

        // set the line renderer b-d, d-a, a-d, d-c
        lr.SetPosition(0, pointA.transform.position);
        lr.SetPosition(1, pointOnLine);
        lr.SetPosition(2, pointOnLine + normalDirection);
        lr.SetPosition(3, pointOnLine);
        lr.SetPosition(4, pointB.transform.position);
    }
}

(apparently into/outof the 2d plane is Vector3.forward :smile:, if you want the perpendicular to go upwards it should be Vector3.back instead by the looks of it)

1 Like

thank you so much!

I actually assumed that it was a 3D project, because of the triple coordinates. I missed the part about it being a 2D project. You can of course still use a 3rd dimension for a 2D solution, but that is not needed and not the most efficient.

Your solution assumes the position of D to be known. (In the form of the distance along the line.)

As far as I know, only point A, B and C are known. So, the 2D solution as presented on that wikipedia page to find the perpendicular line is:

Vector2 bc = c - b;
Vector2 ab = b - a;
Vector2 ad = ab - Vector2.Dot(ab, bc) * bc;

This solution actually also works in 3D and also allows you to find the position of D.

1 Like

good point, getting my threads mixed up, there was another one about a point along a line today :face_with_spiral_eyes: