3d Vector math, Please help!

I know this isnt a math forum, but since Its dealing with 3d vectors from unity. I was hoping someone may be able to help me on this.

See the screenshot below, Given the current information, I need to find the coordinants of points 3,4,5 and 6 in respect to the forward vector.

From 2011-08-01

Assuming your relation b = d*tan(theta) is correct (the drawing indicates b = d*tan(theta/2), since it shows theta as the angle between two triangle sides, not between d and one triangle side) and that right and up are P1->P2 and P2->P3 directions, you will have:

  b = Mathf.Tan(theta); // theta or theta/2 - see note above
  // this way is more compact:
  P3 = P1 + b * (Vector3.right + Vector3.up)
  P4 = P1 + b * (Vector3.right - Vector3.up)
  P5 = P1 - b * (Vector3.right + Vector3.up)
  P6 = P1 - b * (Vector3.right - Vector3.up)
  // this way is more intuitive:
  P2 = P1 + b * Vector3.right;
  P3 = P2 + b * Vector3.up;
  P4 = P2 - b * Vector3.up
  P5 = P4 - 2 * b * Vector3.right;
  P6 = P3 - 2 * b * Vector3.right;

alt text

If i get that right the given information is:

Vector3 P1;   // point
Vector3 P2;   // point
Vector3 D;    // direction
float b;      // distance between P1 and P2

I’ll assume that the big arrow shows the direction of “D”

To get the vector in x direction (let’s call it Dx) just subtract P1 from P2 To get the vector from the center to P2. To get the second vector in y direction (Dy) you just need to calculate the cross product between D and Dx. Keep in mind that Unity uses a left-handed-system and not the mathematical right-handed-system. The difference is that the calculated vector will point in the opposite direction.

Vector3 Dx = P2-P1;
Vector3 Dy = Vector3.Cross(D,Dx).normalized * b; // normalize and scale it to the same length as Dx which is b

// now just calculate the 4 points
Vector3 P3 = P1 + Dx + Dy;
Vector3 P4 = P1 + Dx - Dy;
Vector3 P5 = P1 - Dx - Dy;
Vector3 P6 = P1 - Dx + Dy;

If P2 is not given than you need more information since the square can be rotated around D. The “normal” way is to calculate the cross product with a world axis to get the first vector. Usually you would use the up-vector as second parameter but there are two cases the calculations would fail: the forward vector points exactly up or down. That would make the cross-product (0,0,0)

If (Mathf.Abs(Vector3.Dot(D,Vector3.up)) < 0.99f)
{
    Dx = Vector3.Cross(D,Vector3.up).normalized * b;
}
else
{
    Dx = Vector3.Cross(D,Vector3.forward).normalized * b;
}
// Now continue with the code above.
// ...