SDK Question aligning a positional vector with a directional one

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
11,318
Reaction score
2,786
Points
203
Location
between the planets
I need to align a position with a normalised directional vector, and I was wondering if there is a simpler method than transforming the direction vector into Euler angles, building a matrix and rotating the position? Seems kind of a lot of work just to get an engine exhaust to the right coordinates...
 
I need to align a position with a normalised directional vector, and I was wondering if there is a simpler method than transforming the direction vector into Euler angles, building a matrix and rotating the position? Seems kind of a lot of work just to get an engine exhaust to the right coordinates...

Well, not really simpler, but no need for Euler angles. Let's see if I can remember that...

[math]\vec d[/math].. normalized direction
[math]\vec u[/math].. roughly up-direction

Lets say the normalized direction is the z-axis, then the 3 base vectors are as follows:

[math]\vec x = \vec u \times \vec d[/math]

[math]\vec x = \frac{\vec x}{\lvert \vec x \rvert}[/math]

[math]\vec y = \vec d \times \vec x[/math]

[math]\vec y = \frac{\vec y}{\lvert \vec y \rvert}[/math]

[math]\vec z = \vec d[/math]

Then the rotation matrix is defined by:

[math]\mathcal{R} = \begin{bmatrix} x_1 & y_1 & z_1\\ x_2 & y_2 & z_2 \\ x_3 & y_3 & z_3 \end{bmatrix}[/math]

Given that I don't know your exact use-case, I hope that helps,
Face
 
Use AC3D, it does it for you.
 
Use AC3D, it does it for you.

That would be nice, but unfortunately I have to do it on runtime... ;)

Given that I don't know your exact use-case, I hope that helps,

I'm having trouble reading mathematical notation as always, but I think I'll get over it...

The exact use-case is that I have a thruster mesh with a defined exhaust point. That mesh then gets integrated into a superstructure in a position and alignement that I can't predict (user defined). I have the new mesh position and alignment, all I have to do is to get my exhaust in the right place...
 
I'm having trouble reading mathematical notation as always, but I think I'll get over it...

I'll try to come up with equivalent Orbiter API C++ statements.

The exact use-case is that I have a thruster mesh with a defined exhaust point. That mesh then gets integrated into a superstructure in a position and alignement that I can't predict (user defined). I have the new mesh position and alignment, all I have to do is to get my exhaust in the right place...

Understood. If you have the new mesh position, you should have the rotation matrix already, no? Did you transform it by means of animation sequences?

---------- Post added at 14:21 ---------- Previous post was at 14:04 ----------

[math]\vec d[/math].. normalized direction
Code:
VECTOR3 d=<whateveryouhave>;
[math]\vec u[/math].. roughly up-direction
Code:
VECTOR3 u=_V(0,1,0);
[math]\vec x = \vec u \times \vec d[/math]
[math]\vec x = \frac{\vec x}{\lvert \vec x \rvert}[/math]
Code:
VECTOR3 x=unit(crossp(u, d));
[math]\vec y = \vec d \times \vec x[/math]
[math]\vec y = \frac{\vec y}{\lvert \vec y \rvert}[/math]
Code:
VECTOR3 y=unit(crossp(d, x));
We skip this in code: [math]\vec z = \vec d[/math]

[math]\mathcal{R} = \begin{bmatrix} x_1 & y_1 & z_1\\ x_2 & y_2 & z_2 \\ x_3 & y_3 & z_3 \end{bmatrix}[/math]
Code:
 MATRIX3 R=_M(x.x, y.x, d.x, x.y, y.y, d.y, x.z, y.z, d.z);
or, if you prefer a function:

Code:
MATRIX3 GetRotationFromVector(VECTOR3 d)
{
  normalise(d); //just to be sure
  VECTOR3 x=unit(crossp(_V(0,1,0), d));
  VECTOR3 y=unit(crossp(d, x));
  return _M(x.x, y.x, d.x, x.y, y.y, d.y, x.z, y.z, d.z);
}
 
If you have the new mesh position, you should have the rotation matrix already, no? Did you transform it by means of animation sequences?

Unfortunately I didn't, since the mesh got there by attachment and not by animation. But your function works like a charm, thanks a real lot!! :cheers:
 
Face, there seems to be a wee problem with that function... when d is 0 1 0 or 0 -1 0 (aligned to the y axis), it returns lots of -1.#IND. Any idea what's going on?

EDIT: Never mind, to my own astonishement I figured it out after reading your instructions again and taking another look at the function. It assumed y axis as up-vector, so all I have to do is pass the respective rotation along with the direction, is that correct? at least it seems to work...
 
Last edited:
Back
Top