- Home /
Quaternion multiplication
I was studying this script from an example project and after comparing it to another tutorial, did some research on Quaternion multiplication. Now I am by no means a math whiz (only learning what I need to when necessary), But what I found interesting was this line right here
direction.Normalize ();
I was reading this post http://answers.unity3d.com/questions/372371/multiply-quaternion-by-vector3-how-is-done.html and from what I could gather, Multiplying a quaternion by a vector3 or (Vector2) gives the same rotation. Because I thought the Horizontal and Vertical Input both have a magnitude of 1 anyways is using Vector3.normalize necessary?
using UnityEngine;
using System.Collections;
/// <summary>
/// Utilities to convert Joystiq input to worldspace ( based in main camera)
/// and to convert worldspace to Speed and Direction
/// </summary>
public class JoystickToWorld
{
public static Vector3 ConvertJoystickToWorldSpace ()
{
Vector3 direction;
float horizontal = Input.GetAxis ("Horizontal");
float vertical = Input.GetAxis ("Vertical");
Vector3 stickDirection = new Vector3 (horizontal, 0, vertical);
direction = Camera.main.transform.rotation * stickDirection; // Converts joystick input in Worldspace coordinates //Quaterntion multiplication
direction.y = 0; // Kill Z
//direction.Normalize (); <-------------
return direction;
}
public static void ComputeSpeedDirection (Transform root, ref float speed, ref float direction)
{
Vector3 worldDirection = ConvertJoystickToWorldSpace ();
speed = Mathf.Clamp (worldDirection.magnitude, 0, 1);
if (speed > 0.01f) { // dead zone
Vector3 axis = Vector3.Cross (root.forward, worldDirection);
direction = Vector3.Angle (root.forward, worldDirection) / 180.0f * (axis.y < 0 ? -1 : 1);
} else {
direction = 0.0f;
}
}
}
Answer by robertbu · Sep 06, 2014 at 02:18 AM
is using Vector3.normalize necessary?
Yes. Take a look at these three possible vectors from your code above: (1,0,0), (0,0,1), and (1,0,1). The magnitude of the first two is 1.0, but the magnitude of the last is sqrt(2). Without the Normalize(), your character would move faster when going in a diagonal direction. But you are right in thinking that the Quaternon multiplication does not change the magnitude of the vector.
right that makes sense. I got a little confused because in the other script the author doesn't normalize the direction Vector. He pretty much does the same thing except that. He does use vector3.sqrmagnitude for the speed though.