What Rotation Fan Do I Need for a Sbc

Hey everyone, sorry that it's been a spell. Work has been keeping me a trifle busy. Anyway I've been using a lot of Quaternions recently and as I know information technology's something that a lot of people struggle with I thought I would do a straightaway post with some of the tips and tricks I've picked up.

What is a Quatern?

Firstly off let's quickly cover what a quaternion is, if you clicked on this you belik already have a basic theme of what they are but I want to cover all the bases.

Quaternions are the fashio that most game engines and reckoner programs in generic store 3d rotations, the reason they use these convoluted things instead of Euler angles is because of something called weight lock, I won't go into inside information but lets just say that you decidedly ne'er want to have to deal with angle lock. Spell I'm not whole true how the underlying math works, I believe they get approximately this away victimization unreal/complex numbers. Yes, those things you loathed in algebra, they actually have a use. Something to do with how they eyelet their values. Anyway 3blue1brown has some videos on it that wish explain IT so much better then me if you really want to translate it:

For now all you need to know is that in unity Quaternions are represented aside a 4d vector with the values xyzw, Never touch them, they are necromancy. Unity provides plenty of functions so that we bequeath never have to fles out how they work ourselves.

Order of Operations

In math class we learn that you carry out mathematics trading operations from left-wing to right just the like reading, quaternions are the comparable right? Wrong! Quaternions multiply from right to left field. And to pinnacle it off they're are not commutative, they are associative. What that means is that different multiplication, add-on, or subtraction, the order that you multiply things matter. Basically a * b is the aforesaid Eastern Samoa b * a but rotation1 * rotation2 is different then rotation2 * rotation1. This means that you might get to examine several different combinations before you get the desirable result, at to the lowest degree until you get the hang of it. I give notice't look until that happens to me.

Grassroots Operations

So directly we can finally start getting into some of the encode, first lets go over some basics.

World vs local

An soft mode to keep raceway of the consecrate of trading operations for quaternions is to repute it in damage of world and local rotations. When multiplying a quaternion the world rotation is on the left, and the local gyration is on the right, like this:

                  finalRotation = worldRotation * localRotation;                

For instance if you took the global rotary motion of an object and so increased it by the localRotation of a child of that physical object you would get the global rotation of that child object

                  childRotation = parentTransform.rotation * childTransform.localRotation;                

You can also use this to add an offset to a rotation, for instance if you welcome to rotate an aim around it's local z axis you would just do something like this:

                  tranform.rotation = targetRotation * Quaternion.Euler(0, 0, 90);                

Of course this also applies to global rotations, if you want to rotate an physical object in global space you would precisely perform this:

                  tranform.rotation = Quaternion.Euler(0, 0, 90) * transform.rotation;                

Adding and Subtracting

Image result for this is where the fun begins
This is where the trouble begins

So you don't just wish to add quaternions? You need to subtract them besides? Are you insane? If the answer is yes, welcome to the club and let's continue.

To subtract one rotation from another you are going to need to get IT's inverse like so:

                  inverseRotation = 4.Inverse(rotation);                

Why unity couldn't just minimal brain dysfunction a .inverse to the struct so that you could do rotation.inverse I don't have it off, IT would make things a lot cleaner sounding. (Unity plz fix).

One of the main uses for this is getting the gyration 'tween two rotations. For example if you sought to know the Quaternion that would get you from rotationA to rotationB you would do something like this:

                  FromAtoB = Quaternion.Reverse(rotationA) * rotationB                

You would then get the rotation between them, this is most useful when you wishing to simulate the offset of one thing to another thing, for example maybe you want to make two levers mirror each other and turn away to use local rotations for whatever rationality.

                  offset = Quartet.Inverse(lever1BaseRotation) * lever1Rotation lever2Rotation = lever2BaseRotation * offset                

Multiple rotations straightaway

So maybe you're completely insane and you want to do multiple rotations at erstwhile in the same equation, maybe you'atomic number 75 storing local rotations with an first or something in a scriptable object for and motorcar hand poser or something. The trick is to e'er keep the range of operations. So if you did something like this to storage rotation3:

                  rotation3Offset = Quaternion.Reciprocal(rotation1) * Quaternion.Inverse(rotation2) * rotation3                

to come back to rotation3 you would need to do this:

                  rotation3 = rotation1 * rotation2 * rotation3Offset                

This sack even work with predefined local offsets if you'ray carful:

                  storedRotation = Quaternion.Inverse(parentRotation) * rotationToStore * Quartet.Backward(offset) rotationToStore = parentRotation * storedRotation * offset                

Dismal if none of that ready-made sense, but I hope it helps someone. If I'm just unbalanced please place help.

Quaternions to Euler Angles and Back

Now as awesome every bit it would be to constitute able to understand Quaternions enough to be able to case them into the inspector of our scripts sporty unsatisfactory pure intuition, that's not very realistic and we are going to need to occasionally win over to and from eulers and back.

The first matter we need to understand earlier starting this is Unity's order of trading operations for Leonhard Euler angles, which is first y, then x, then z. Just something to remember.

Converting from Euler to Quaternion is easy, just employment the Quaternion.Euler() function, where it gets case-hardened is when you start wanting Euler angles out of a Little Jo.

The easy way is to use rotation.eulerAngles care this:

                  Vector3 eulerAngles = transorm.rotation.eulerAngles                

And this works just fine if you want to get the rotation of a quaternion around x, y, surgery z. It does get a less harder when you deprivation to get the euler angle around a different axis surgery vector. Luckily for you I have a convoluted function that can figure out the Euler angle of a rotation around an axis vertebra (Rather the likes of projecting a vector on to a shave). I had to get along up with it because DOTS mathematics does not have a function for acquiring the y rotation of a quaternion and I needed that for a movement hand. You can find it somewhere in here. IT will be in the math extensions class, along with more or less others for projecting vectors if you ever wondered about the math for that.

Conclusion

That's all I have for you guys today, If you find some errors please put them in the comments so that others can figure them and so that I stool fix them later. If I fare ahead with anything else I reckon will be useful I'll add it here later. Until next time.

Liked it? Pick out a second to support WireWhiz on Patreon!

What Rotation Fan Do I Need for a Sbc

Source: https://wirewhiz.com/quaternion-tips/

Post a Comment

Previous Post Next Post

Iklan Banner setelah judul