- Home /
Question by
NovHak · Nov 19, 2018 at 03:58 PM ·
randomquaternionsrotations
My interpretation of Random.rotation and Random.rotationUniform. Right or wrong ?
Dear forum readers,
For modding reasons, I'm rewriting Unity's UnityEngine.Random class. I think I got everything, except maybe the Random.rotation and Random.rotationUniform properties.
Do these rewrites seem legit to you ? The only difference between the two being in the do-while condition.
public sealed partial class Random
{
public static Quaternion rotation
{
get
{
float norm, w, x, y, z;
do
{
w = Range(-1f, 1f);
x = Range(-1f, 1f);
y = Range(-1f, 1f);
z = Range(-1f, 1f);
norm = w * w + x * x + y * y + z * z;
}
while (norm == 0f);
norm = (float)System.Math.Sqrt(norm);
Quaternion quat;
quat.w = w / norm;
quat.x = x / norm;
quat.y = y / norm;
quat.z = z / norm;
return quat;
}
}
public static Quaternion rotationUniform
{
get
{
float norm, w, x, y, z;
do
{
w = Range(-1f, 1f);
x = Range(-1f, 1f);
y = Range(-1f, 1f);
z = Range(-1f, 1f);
norm = w * w + x * x + y * y + z * z;
}
while ((norm > 1f) || (norm == 0f));
norm = (float)System.Math.Sqrt(norm);
Quaternion quat;
quat.w = w / norm;
quat.x = x / norm;
quat.y = y / norm;
quat.z = z / norm;
return quat;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Can I use Quaternions to get from one point to another? 1 Answer
Quaternions rotating left and right, not forward and back 1 Answer
4 rotations WTF 1 Answer
Relative Rotations and Position calculation 2 Answers
How to use quaternions to apply an offset to a rotation to calibrate a controller 1 Answer