c# quaternion eular calculator
C#
public static Vector3 QuaternionToEuler(Quaternion q)
{
Vector3 euler;
// if the input quaternion is normalized, this is exactly one. Otherwise, this acts as a correction factor for the quaternion's not-normalizedness
float unit = (q.x * q.x) + (q.y * q.y) + (q.z * q.z) + (q.w * q.w);
// this will have a magnitude of 0.5 or greater if and only if this is a singularity case
float test = q.x * q.w - q.y * q.z;
if (test > 0.4995f * unit) // singularity at north pole
{
euler.x = Mathf.PI / 2;
euler.y = 2f * Mathf.Atan2(q.y, q.x);
euler.z = 0;
}
else if (test < -0.4995f * unit) // singularity at south pole
{
euler.x = -Mathf.PI / 2;
euler.y = -2f * Mathf.Atan2(q.y, q.x);
euler.z = 0;
}
else // no singularity - this is the majority of cases
{
euler.x = Mathf.Asin(2f * (q.w * q.x - q.y * q.z));
euler.y = Mathf.Atan2(2f * q.w * q.y + 2f * q.z * q.x, 1 - 2f * (q.x * q.x + q.y * q.y));
euler.z = Mathf.Atan2(2f * q.w * q.z + 2f * q.x * q.y, 1 - 2f * (q.z * q.z + q.x * q.x));
}
// all the math so far has been done in radians. Before returning, we convert to degrees...
euler *= Mathf.Rad2Deg;
//...and then ensure the degree values are between 0 and 360
euler.x %= 360;
euler.y %= 360;
euler.z %= 360;
return euler;
}
public static Quaternion EulerToQuaternion(Vector3 euler)
{
float xOver2 = euler.x * Mathf.Deg2Rad * 0.5f;
float yOver2 = euler.y * Mathf.Deg2Rad * 0.5f;
float zOver2 = euler.z * Mathf.Deg2Rad * 0.5f;
float sinXOver2 = Mathf.Sin(xOver2);
float cosXOver2 = Mathf.Cos(xOver2);
float sinYOver2 = Mathf.Sin(yOver2);
float cosYOver2 = Mathf.Cos(yOver2);
float sinZOver2 = Mathf.Sin(zOver2);
float cosZOver2 = Mathf.Cos(zOver2);
Quaternion result;
result.x = cosYOver2 * sinXOver2 * cosZOver2 + sinYOver2 * cosXOver2 * sinZOver2;
result.y = sinYOver2 * cosXOver2 * cosZOver2 - cosYOver2 * sinXOver2 * sinZOver2;
result.z = cosYOver2 * cosXOver2 * sinZOver2 - sinYOver2 * sinXOver2 * cosZOver2;
result.w = cosYOver2 * cosXOver2 * cosZOver2 + sinYOver2 * sinXOver2 * sinZOver2;
return result;
}
Also in C#:
- Title
- move character unity
- Category
- C#
- Title
- 2d character controller unity
- Category
- C#
- Title
- linq c# object except two lists
- Category
- C#
- Title
- making a list of chars in c#
- Category
- C#
- Title
- create a file in the directory of the exe and write to it c#
- Category
- C#
- Title
- c# get enum in list
- Category
- C#
- Title
- unity c# change color of gameobject
- Category
- C#
- Title
- How to make a function in C#
- Category
- C#
- Title
- csharp attributes as generics constraints
- Category
- C#
- Title
- c# check if string is only letters and numbers
- Category
- C#
- Title
- c# index in select
- Category
- C#
- Title
- c# byte array to bitmap
- Category
- C#
- Title
- asp textarea
- Category
- C#
- Title
- convert string to datetime c#
- Category
- C#
- Title
- c# md5 string
- Category
- C#
- Title
- c# mailmessage set sender name
- Category
- C#
- Title
- unity add component
- Category
- C#
- Title
- c# abstract class
- Category
- C#
- Title
- c# empty char
- Category
- C#
- Title
- how to put double quotes in a string c#
- Category
- C#
- Title
- check connection c#
- Category
- C#
- Title
- get query string parameter from string value c#
- Category
- C#
- Title
- c# is not equal to
- Category
- C#
- Title
- c# tab character
- Category
- C#
- Title
- c# bitmap to array byte
- Category
- C#
- Title
- mongodb c# batch find
- Category
- C#
- Title
- c# to vb.net
- Category
- C#
- Title
- c# error "The name 'ViewBag' does not exist in the current context"
- Category
- C#
- Title
- c# map
- Category
- C#
- Title
- dictionary c#
- Category
- C#
- Title
- c# windows application get current path
- Category
- C#
- Title
- c sharp create dictionary
- Category
- C#
- Title
- new command - latex
- Category
- C#
- Title
- c# scene manager
- Category
- C#
- Title
- unity up arrow input
- Category
- C#
- Title
- how to find the type of a object c#
- Category
- C#
- Title
- c# file exist
- Category
- C#
- Title
- c# start process
- Category
- C#
- Title
- unity how to tell when a gameobject is colliding
- Category
- C#
- Title
- c# replace foreach with lambda
- Category
- C#
- Title
- use newtonsoft json to clone object
- Category
- C#
- Title
- how to initiate a varaible in cs
- Category
- C#
- Title
- c# repeat string x times
- Category
- C#
- Title
- c# update control from another thread
- Category
- C#
- Title
- loop gridcontrol devexpress c#
- Category
- C#
- Title
- c# foreach namevaluecollection
- Category
- C#
- Title
- c# stop loop
- Category
- C#
- Title
- no entity framework provider found for the ado.net provider with invariant name
- Category
- C#
- Title
- What is a class in c#
- Category
- C#
- Title
- c# linq to get most recent item from IList
- Category
- C#
- Title
- remove first object from list c#
- Category
- C#
- Title
- c# edit element in list
- Category
- C#
- Title
- transform object according to its parent unity
- Category
- C#
- Title
- unity how to get transform scale
- Category
- C#
- Title
- Assets/Scripts/Snake.cs(177,25): error CS1061: Type `Snake.SnakeBodyPart' does not contain a definition for `SetGridPostion' and no extension method `SetGridPostion' of type `Snake.SnakeBodyPart' could be found. Are you missing an assembly reference?
- Category
- C#
- Title
- .net loop through dictionary
- Category
- C#
- Title
- unity print to console
- Category
- C#
- Title
- c# regex replace
- Category
- C#
- Title
- list clone - C#
- Category
- C#
- Title
- how to make rb.addforce 2d
- Category
- C#
- Title
- double tryparse dot comma
- Category
- C#
- Title
- unity get list length
- Category
- C#
- Title
- where keyword in c#
- Category
- C#
- Title
- c# get the last item in a list
- Category
- C#
- Title
- copy a list in c# unity
- Category
- C#
- Title
- c# contextswitchdeadlock
- Category
- C#
- Title
- C# webclient submit form
- Category
- C#
- Title
- strtorime online
- Category
- C#
- Title
- bold caption latex
- Category
- C#
- Title
- c# get hwid
- Category
- C#
- Title
- asp.netcore: develop on win10 run on ubuntu
- Category
- C#
- Title
- comment envoyer un socket C#
- Category
- C#
- Title
- datatable return column names
- Category
- C#
- Title
- c# error CS0120
- Category
- C#
- Title
- how to add a gameobject
- Category
- C#
- Title
- c# uppercase string
- Category
- C#
- Title
- c# else if
- Category
- C#
- Title
- usermanager find based on role
- Category
- C#
- Title
- c# get gridview DataKeyNames
- Category
- C#
- Title
- what is a protected int c#
- Category
- C#
- Title
- godot c# move towards
- Category
- C#
- Title
- eager loading vs lazy loading c#
- Category
- C#
- Title
- check which activity in focus in android
- Category
- C#
- Title
- list of vectors c#
- Category
- C#
- Title
- Category
- C#
- Title
- c# append array
- Category
- C#
- Title
- unity rigidbody constraints
- Category
- C#
- Title
- c# string contains space
- Category
- C#
- Title
- c# get all class properties
- Category
- C#
- Title
- loop datagridview c#
- Category
- C#
- Title
- unity set position
- Category
- C#
- Title
- C# Unknown column 'FundAllocation' in 'field list
- Category
- C#
- Title
- parsing string to int without format exception c#
- Category
- C#
- Title
- unity c# foreach
- Category
- C#
- Title
- unity getkey keycode
- Category
- C#
- Title
- asp.net model display name
- Category
- C#
- Title
- ASP select box all states
- Category
- C#
- Title
- c# class declaration
- Category
- C#
- Title
- sqlite connection c#
- Category
- C#
- Title
- how to check if textbox is empty in c#
- Category
- C#