Search Unity

Understanding Unity Physics and Vector Maths

Discussion in 'Editor & General Support' started by BsseeJ, Jun 28, 2014.

  1. BsseeJ

    BsseeJ

    Joined:
    Mar 12, 2014
    Posts:
    20
    Hello Unity Forum

    I have recently began making my first real project in Unity and the largest thing I lack is how to use vector maths to get objects to do exactly what I want. Is there any good reads on this besides just the Unity Manual which only helped a little and didn't explain in detail.

    I have just began Pre-Calc in college and I have not been in a physics class yet but plan to be. I would be willing to purchase books on the subject if any are specifically about what I intend on learning. Some of the major issues I deal with is using Quaternions and understanding things like magnitude. Also the effects of arithmetic between vectors and rotations.

    Thank for any help.
     
  2. MakeCodeNow

    MakeCodeNow

    Joined:
    Feb 14, 2014
    Posts:
    1,246
    I'm not aware of any specific tutorials, but here are some pointers:

    Primitives
    Vectors - Represent positions (i.e. location of an object) or directions (like the normal at a vertex) or scale. Vectors are the most intuitive of the 3D types, just keep in mind when you are talking about a position and when you are talking about a direction, as there are some subtle differences.

    Quaternions - Represent rotations. Actually understanding what a quaternion is and how it works is very difficult. They represent a position on a 4D hypersphere. It's complicated. TBH, don't even try to understand it. Just learn the rules for how to use them and move on.

    Matrices - Represent a combination of translation, rotation, and scale. Matrices are awesome for doing a lot of math at once, but generally you want to combine translation, rotation, and scale into a matrix and not try to extract them from a matrix. Think of a matrix as the "baked" version of the transform.

    Spaces
    Whenever doing 3D math, you want to remember that the vectors and quats and matrices are always relative to something, which is referred to as the space or coordinate system you're working in. You need to make sure that whenever you are doing 3D math that the vectors and quats and matrices are all in the same space. You always combine like with like.

    The most basic "space" to be in, is world space, which means that your terms are fixed and absolute. World space is not relative to anything. Most things in Unity like transform.position return world space values. World space is the easiest space to think about, so it's a good place to start.

    However, as you get deeper in, it's helpful to work in coordinate systems other than world space. For example, if one object is a child of another one, and you want to move relative to the parent, it's nice to use local space (see transform.localPosition, etc). If you are dealing with meshes, they will be in model space, where the verts are relative to the origin of the model, and then the object's transform is what actually gives them a place in world space.

    As you do more 3D math, you'll learn move about common spaces. The most important thing is just to know that different spaces exist. A very common mistake when learning 3D math is that you think you are doing everything right but somehow the results are crazy. Often the cause of this is that you are combining data from two different spaces. When in doubt, verify that your inputs are all in the same space.

    Vector Operations

    Vector * float: Scales (stretches) the vector by the float. Values between 0 and 1 shrink it, > 1 stretches it, and values < 0 do that, but flip the direction of the vector.

    Vector / float: Same concept as above.

    Vector magnitude: Tells you the length of the vector.

    Vector normalize: This keeps the direction the same, but ensures that it is unit length (i.e. magnitude = 1). Normalize really just means compute the magnitude and divide the vector by the magnitude.

    Vector + Vector: Sums the vectors. Think of the first vector as a line from the origin (0,0,0) to that position. Then think of the 2nd vector as starting from the tip of the first vector. The final value is the location of the end of the 2nd vector.

    Vector - Vector: Like the opposite of addition. The most common operation is when you want to get a vector between two positions, like "what direction is it from me to my enemy". In that case, the direction from A to B is expressed as B - A.

    Vector dot product: The dot product tells you the angle (technically the facing ratio) between two vectors. The dot product is almost always used to ask "is this guy facing the right way" or to compute the angle between two vectors. The dot product returns values from -1 to 1 where 1 means "totally parallel" 0 means "totally perpendicular" and -1 means "totally anti-parallel" (i.e. parallel by going in opposite directions). If you take the acos of the dot product, you'll get an actual angle measure (in radians). Note that unless you know what you're doing, vectors should always be normalized before you dot them.

    Vector cross product: The cross product takes two vectors and creates a new vector that is perpendicular to them. It sounds crazy, but imagine if you have a X axis and a Z axis. If you cross X and Z, you'll get Y. This is usually used when you want to construct some sort of matrix or coordinate system.

    Quaternion Operations
    Quat Constructors: There are lots of ways to make a quat, each of which are useful in different ways. Just check them out and think about them as you do stuff.

    Quat * Quat: This combines the rotations. Think of it like adding the rotations together.

    Quat * Vector: This rotates the vector by the quat. Pretty self explanatory.

    Matrix Operations
    Matrix * Matrix: Combines the matrices. Like Quat * Quat but can include translation and scale.

    Matrix * Vector: Transforms the vector by the matrix. Like Quat * Vector, but can include translation and scale.

    Matrix Inverse: This makes the matrix "go the other way". If you have a matrix that goes from local to world space, inverting it will make it go from world to local.
     
    Last edited: Jul 18, 2014
    Gametyme, Aurore, BsseeJ and 2 others like this.
  3. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    ^ Well written explanation.
     
  4. John Fallon

    John Fallon

    Unity Technologies

    Joined:
    Jul 1, 2014
    Posts:
    44
  5. BsseeJ

    BsseeJ

    Joined:
    Mar 12, 2014
    Posts:
    20
    Hello John and "MakeCodeNow" thanks for your help, I'm sorry for the late reply, been busy finishing my last tests for my summer pre-calc class. I read over your guide MakeCodeNow and it helped fill in some of the basic blanks I had like what normalize did and especially the spaces, I wasn't sure what localspace methods did so I hadn't really messed with them but they could be the solution to what I'm working on now, using a object as the center point for a another object to spin around. Thanks again.