4
votes

How to create coordinate system given only a vector? (left-handed, right-handed, local or different space)

enter image description here

I'm searching calculate a local coordiante system algorithm given only one vector:

Reason:

1) Maybe Z is surface normal, then X and Y vector would be tangent and bitangent vector

2) Another reason is for ray tracing: sampling a light source. Given a intersection point construct local coordinate system from that point then trace ray to light shape.

Vector3 Z = {0.0f, 0.0f, 1.0f}; // given
// fill x and y
CreateXHandedCoordinateSystem(Vector3* X, Vector3* Y, const Vector3& Z) {
   // implementation goes here..
   // evaluate algorithm
   // calculate X
   // Y = cross(X, Z) or something ..
}
3
I sincerly have no idea what you want to know...Simon Kraemer
@NathanOliver I love how "than" and "then" are appropriate words in your sentence :)YSC
I'm voting to close this question as off-topic because there does not appear to be a specific programming question.NathanOliver
@NathanOliver: This is exactly tag related question. I'm edited my question and added some info.user5044221

3 Answers

-1
votes

Assume that the smallest component (in absolute value) of the given vector is Zz. Then define X to be (Zy, -Zx, 0), and Y = (-Zx.Zz, -Zy.Zz, Zx²+Zy²).

If the smallest component isn't Zz, adapt the formulas accordingly.

Notice that these vectors are unnormalized and I didn't look at handedness.

4
votes

You can use double cross product trick.

  1. Take your vector (lets call it local X) take arbitrary up vector (usually world up vector ) and calculate cross product. This will be your local Z.
  2. Take your local X and local Z and calculate cross product this will be your local Y
-1
votes

Just use coordinate rotation concept.

Initially, you have one vector (suppose in x direction)

Now rotate that vector by 90 degree and you will get the second axis (y axis if rotated 90 degree in XY plane)

Similarly get the third axis by rotating in appropriate plane.