Search Unity

Question on Unity syntax, meaning and usage

Discussion in 'Scripting' started by Deleted User, Aug 30, 2019.

  1. Deleted User

    Deleted User

    Guest

    I was previously in another forum but was told that it was not suitable / correct. I am in the midst of reviewing the codes from a tutorial. Many are not difficult to understand but I'm still a beginner. I have already checked the internet and it doesn't provide very useful resources. I went to the unity link and it doesn't do a good job explaining things. In my case I don't know what is quarternion even after referring to the link. Can someone tell me what it is? https://docs.unity3d.com/ScriptReference/Quaternion.html - i don't understand what it is telling me. Beware that I might need real life examples to understand it well.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    From the doc "They are based on complex numbers and are not easy to understand intuitively."

    Otherwise, maybe this video would help? (I didn't watch it, it just came up in the Google search and looked relavent)
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Good luck with that
    https://en.wikipedia.org/wiki/Quaternion

    Note that you don't really need to understand how this works and can just treat it as a black box, converting back and forth to Euler as needed.

    Also, in the context of programming the word "code" is already plural. You would use the word "codes" if you were talking about multiple license keys or cheat codes, not C# code.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Here's what you should know about Quaternions:

    Quaternions are one possible way of representing a rotation or orientation in 3D space.

    Quaternions are hard to understand, but they have several mathematical advantages (particularly for interpolation), so Unity uses them "behind the scenes" as the main way of representing rotations.

    In the user interface, you usually use Euler angles instead. Unity provides functions for converting back and forth, but if you convert Euler angles into a Quaternion and then back into Euler angles, you might get something different than you started with, because the same rotation can be represented by more than one combination of Euler angles.

    You can "combine" two rotations represented by Quaternions by multiplying them together. This is not commutative (A * B is different from B * A); basically, the second rotation is applied in the "reference frame" of the first one when making the combination.

    Quaternions have 4 parts, called w, x, y, and z. Never use them for anything. They don't mean what you think they mean. If you need to create a Quaternion, use one of Unity's functions for doing it instead of trying to set these values directly.
     
  5. Deleted User

    Deleted User

    Guest

    Thank you to all of you. I REALLY appreciate this. Give me time to check the video.
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,157
    Good luck with finding any real life examples. Quaternions are most beneficial when you are working with three-dimensional rotations but the way quaternions handle them is by introducing a fourth dimension. Since the real world doesn't exist in four dimensions and our brains think in three dimensions there aren't any real life examples outside of math concepts.

    If the concept happens to click for you that's awesome but if it doesn't don't waste any more time than you have to just getting an understanding of the benefits of quaternions and how to use the helper functions to work with them. I'd be surprised if 99.9% of developers (Unity's own staff included) were able to truly understand them.

    If it helps any here is an online visualizer that shows both the euler and quaternion form of a rotation.

    https://quaternions.online/
     
    Last edited: Aug 31, 2019
  7. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    I'll try painting the picture for why Quaternions are a bit hard to understand by explaining number spaces in math.

    You probably know some of the capital letters to denominate different number spaces in math. For example N, which represents all positive integers. However, using N you'll eventually run into problems like x-y with y>x. Calculating the result requires negative integers numbers Z. Adding division you can run into problems, when the result is not an integer. Now you require fractions. Eventually you end up with all real numbers R.
    With the number space R you can do most things that make sense to our human brains, however it simply cannot solve the equation X² = -1, since we cant calculate the root of a negative number.
    To solve this problem some mathematician defined the imaginary number i = sqrt(-1) and thus created the complex numberspace C.
    Every complex number is a combination of one real part and one imaginary part in the form a + b*i. Just like with any other number space, this allows expressing any number from R as a number in C, by simply chosing b = 0.
    However, keep in mind that i is not a real, but an imaginary number, since all real numbers can already be expressed in R.
    Complex numbers are, for example, good for calculations in 2D space, where one dimension is all real numbers. This makes calculating rotations possible in another dimension, that is not in real number space.

    So why did i mention all of this? Because it helps understanding Quaternions. A Quaternion is a 4-dimensional construct, consisting of 1 real and 3 imaginary parts. This means, for any real number you have another 3 whole dimensions to work with, that are not real. It's the next step after complex numbers C. Similar to how complex numbers are useful for working in 2-dimensional calculations, Quaternions are very good at working with 3-dimensional calculations, especially rotations. One advantage of Quaternions over, for example, Euler Angles, is that a Quaternion cannot run into problems like gimbal lock, while Euler Angles can.
    Now one may ask: if complex numbers are 1r+1i and useful for 2D stuff, and Quaternions are 1r+3i and useful for 3D stuff, then what happened to 1r+2i? In the original approach to adding another dimension a combination of 1r+2i was tried, but it turned out that the dimensions collapse down to 1i again, so a third imaginary part for a total of 4 dimensions was required to stabilize the whole thing.

    This results in a very hard to understand field in math; not only because we are talking about 4 dimensions, but also because only one of these dimensions contains real numbers from R, which we are used to. Before this gets too long, let me just add quickly that Quaternions are not simply a 4-dimensional vector, since specific rules apply to the imaginary numbers i, j and k. When calculating with Quaternions, some math rules you may got used to also do not apply, like xy == yx, which is not true if x and y are Quaternions. While Quaternions found some niche use in some mathematical proofs, to my knowledge rotations in computer graphics are actually their main usage.
    Oh and they are also used in freaking Quantum Mechanics iirc, so yeah..

    There is a joke in math, along the lines of:
    Math: i is as complex as it will get
    Math: jk
    Which is a reference to the 2 additional imaginary parts called j and k in Quaternions, but for most people simply means "joked". However, since Quaternions were introduced in 1840 or something, those crazy mathematicians actually created larger constructs using hypercomplex numbers, like the Sedenion, which is 16-dimensional with only one real part. Gladly, to my knowledge in game development we can fully stop caring after the fourth dimension. And for the most part, it's not even required to understand Quaternions, as you dont need to understand how the tool works in order to use it.

    If you want to get a closer look at Quaternions, there are some videos that try and explain them, like this one
    (which is also the followup of the previously posted video):
     
    Last edited: Aug 31, 2019
    snekmuffin likes this.