Is there any function to convert Euler angles to quaternions in script
#1
Hey Guys,I've been learning the script operator recently, and I have some unity3d programming experience, but I noticed that there doesn't seem to be a function in the typflow API to convert Euler angles to quaternions.Converting from Euler angles to quaternions is a very complicated mathematical process, is there any way to do it? Sad

https://www.3dgep.com/understanding-quaternions/
  Reply
#2
No one seems to be answering this question, I read through the article and wrote this function that outputs quaternions by entering a point3 value, I'm pasting it here in case anyone else has the same trouble.

public Quat EulerToQuaternion(Point3 euler)
{
float yaw = euler.y * Mathf.Deg2Rad;
float pitch = euler.x * Mathf.Deg2Rad;
float roll = euler.z * Mathf.Deg2Rad;

float cy = Mathf.Cos(yaw * 0.5f);
float sy = Mathf.Sin(yaw * 0.5f);
float cp = Mathf.Cos(pitch * 0.5f);
float sp = Mathf.Sin(pitch * 0.5f);
float cr = Mathf.Cos(roll * 0.5f);
float sr = Mathf.Sin(roll * 0.5f);

Quat q = new Quat();
q.w = cy * cp * cr + sy * sp * sr;
q.x = cy * sp * cr + sy * cp * sr;
q.y = sy * cp * cr - cy * sp * sr;
q.z = cy * cp * sr - sy * sp * cr;

return q;
}


This is an example of a call function :

public void simulationStep()
{
for (int i = 0; i < eventParticleCount; i++)
{
int sInx = tf.GetSimIndex(i);
float eventAge = tf.GetEventAge(sInx);
tf.SetSeed(sInx,(int)GetFloat("seed"));
Point3 randRot = new Point3(0,0,tf.GetRandInt(sInx,0,4)*90);
tf.SetRot(sInx,EulerToQuaternion(randRot));
}
}
  Reply
#3
tyFlow doesn't have a built-in euler to quaternion function because the result is not well-defined, due to vectors not containing enough data to fully define a quaternion.

That said, I guess the equivalent would be tyFlow's 'MatrixFromVector' function, which you could then derive your quaternion from (same limitation about ambiguity of result applies).

Code:
for (int i = 0; i < eventParticleCount; i++) //this for-loop iterates through all particles in this event                
{
    var sInx = tf.GetSimIndex(i);
    
    var vec = new Point3(-0.5f, 0.2f, -.75f);
    tf.SetRot(sInx, new Quat(MatrixFromVector(vec.normalized)));    
}
  Reply


Forum Jump: