Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Multiply quaternion with a speed variable

Discussion in 'Scripting' started by saifshk17, May 23, 2023.

  1. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    485
    I am rotating my object in all 3 axis with Quaternion. Is there a possibility to multiply it with a given speed?

    I am using Quaternion.Inverse in my example instead of Quaternion.Euler, would it still be possible to do arithmetic on it?

    Code (CSharp):
    1. private Quaternion initialObjectRotation;
    2. private Quaternion initialControllerRotation;
    3. private bool offset;
    4. public GameObject obj2;
    5.  
    6. public void Update() {
    7.  
    8.         if (offsetSet == false) {
    9.             initialObjectRotation = transform.rotation;
    10.             initialControllerRotation = obj2.transform.rotation;
    11.             offsetSet = true;
    12.         }
    13.  
    14.          Quaternion controllerAngularDifference = initialControllerRotation * Quaternion.Inverse (obj2.transform.rotation);
    15.          transform.rotation = controllerAngularDifference * initialObjectRotation;
    16.     }
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    I'm not sure how a "speed" would play into your existing code. You set the absolute orientation / rotation of your object to the an absolute rotation you get from the difference of your "controller" from its initial orientation. Do you just want to artificially delay the response?

    Multiplying a quaternion by "speed" doesn't make much sense. A quaternion is always a relative rotation around a single axis. When used as an orientation it's simply the relative rotation from the identity orientation.

    So what exactly do you want to achieve?
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    I'm positive your questions have answers if only someone could understand what you're saying.

    a) Quaternions don't rotate anything (by themselves), it's just a set of 4 values.
    b) Quaternions (when applied) do not rotate on 3 axis, just 1.
    c) You want to say you're using a quaternion to represent a 3-axis Euler rotation.

    That's not really a substitute, it makes no sense.

    Depends. What's arithmetic to you? Can you offer some point of comparison?

    Quaternions are unit 4-dimensional vectors (or better versors), speed is usually scalar. You cannot multiply scalar and a quaternion because that would make it non-unit, which isn't a valid versor. And besides you can't rotate a plain number anyway.

    If you mean a velocity vector, that's just a direction vector with a magnitude of speed, and yes you can multiply quaternion with a vector of any length, this would effectively and relatively rotate the velocity vector. So I presume you meant a velocity vector.

    This is done with multiplication operator
    *

    Code (csharp):
    1. var myQuat = Quaternion.Euler(0, 45, 0);
    2. var myVec = Vector3.right;
    3. var myRotatedVec = myQuat * myVec;
    Unlike other use cases for the multiplication operator, this operation is non-commutative, and quaternion must be on the left hand side. This is how quaternion is "applied" to vectors, that's why I say it doesn't rotate anything on its own.

    This is just a convenient syntactic way to represent a simple concept, yet it translates into a rather lengthy matrix multiplication.
     
    Last edited: May 23, 2023