0

i am scratching my head for some time now how to do this.

I have two defined vectors in 3d space. Say vector X at (0,0,0) and vector Y at (3,3,3). I will get a random point on a line between those two vectors. And around this point i want to form a circle ( some amount of points ) perpendicular to the line between the X and Y at given radius.

Hopefuly its clear what i am looking for. I have looked through many similar questions, but just cant figure it out based on those. Thanks for any help.

Edit: (Couldnt put everything into comment so adding it here) @WillyWonka

Hi, thanks for your reply, i had some moderate success with implementing your solution, but has some trouble with it. It works most of the time, except for specific scenarios when Y point would be at positions like (20,20,20). If it sits directly on any axis its fine.

But as soon as it gets into diagonal the distance between perpendicular point and origin gets smaller for some reason and at very specific diagonal positions it kinda flips the perpendicular points.

IMAGE

Here is the code for you to look at

    public Vector3 X = new Vector3(0,0,0);
    public Vector3 Y = new Vector3(0,0,20);

    Vector3 A;
    Vector3 B;

    List<Vector3> points = new List<Vector3>();


    void FindPerpendicular(Vector3 x, Vector3 y)
    {

        Vector3 direction = (x-y);
        Vector3 normalized = (x-y).normalized;  
        float dotProduct1 = Vector3.Dot(normalized, Vector3.left);
        float dotProduct2 = Vector3.Dot(normalized, Vector3.forward);
        float dotProduct3 = Vector3.Dot(normalized, Vector3.up);



        Vector3 dotVector = ((1.0f - Mathf.Abs(dotProduct1)) * Vector3.right) +
                            ((1.0f - Mathf.Abs(dotProduct2)) * Vector3.forward) + 
                            ((1.0f - Mathf.Abs(dotProduct3)) * Vector3.up);

        A = Vector3.Cross(normalized, dotVector.normalized);    
        B = Vector3.Cross(A, normalized);

    }
CosmicSeizure
  • 1,375
  • 5
  • 17
  • 35

1 Answers1

2

What you want to do first is to find the two orthogonal basis vectors of the plane perpendicular to the line XY, passing through the point you choose.

You first need to find a vector which is perpendicular to XY. To do this:

  • Normalize the vector XY first
  • Dot XY with the X-axis
  • If this is very small (for numerical stability let's say < 0.1) then it must be parallel/anti-parallel to the X-axis. We choose the Y axis.
  • If not then we choose the X-axis
  • For whichever chosen axis, cross it with XY to get one of the basis vectors; cross this with XY again to get the second vector.
  • Normalize them (not strictly necessary but very useful)

You now have two basis vectors to calculate your circle coordinates, call them A and B. Call the point you chose P.

Then any point on the circle can be parametrically calculated by

Q(r, t) = P + r * (A * cos(t) + B * sin(t))

where t is an angle (between 0 and 2π), and r is the circle's radius.

  • Hi, thanks for your reply, i had some moderate success with implementing your solution, but has some trouble with it. It works most of the time, except for specific scenarios when Y point would be at positions like (20,20,20). If it sits directly on any axis its fine. – CosmicSeizure May 29 '16 at 11:24
  • @CosmicSeizure what about X? is that always at (0, 0, 0)? dont forget to normalize XY when you do the dot check (so in this case its (1/sqrt(3), 1/sqrt(3), 1/sqrt(3)) ) –  May 29 '16 at 13:08
  • Hi, i updated the question with the code and image, please have a look at that. Thanks. X and Y can be at any position. – CosmicSeizure May 29 '16 at 21:13
  • @CosmicSeizure did you _normalize_ the basis vectors correctly? btw you can't really judge whether they are flipped or not because you have supplied a reference basis vector in the first place, so you can't always be certain about the orientation of the basis –  May 29 '16 at 22:00