Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
general [2020/04/10 19:48]
paul [Quaternions]
— (current)
Line 1: Line 1:
-====== General ====== 
  
-===== Rotations ===== 
-Unity uses 3 rotation systems. [[https://www.youtube.com/watch?v=kYOtk5a6_x4|Intro to Quaternion Rotations (in Unity)]] 
- 
-  - Euler Angers - rotation about x, y, z axis 
-  - Angle Axis - A normalized Vector3 representing the axis about which to rotate by float angle amount 
-  - Quaternions - Angle Axis rotation representation that have been scaled to improve performance. 
- 
- 
-==== Calculations ==== 
- 
-=== Quaternion.FromToRotation() == 
- 
-This code snippet calculates the rotation required to rotate an object from a given starting direction (in this case the back of the object given Vector3.back) to a final direction (in this case direction to camera) using the ''Quaternion.FromToRotation()'' function. 
-<code c#> 
-// Get the direction to a camera 
-Vector3 directionToCamera = Camera.main.transform.position - transform.position; 
- 
-transform.rotation = Quaternion.FromToRotation(Vector3.back, directionToCamera); 
-</code> 
- 
-=== Quaternion.LookRotation() === 
-''Quaternion.LookRotation()'' gives us more control on our rotation than ''Quaternion.FromToRotation()'', as it gives us a control of our final rotation when we finish the primary rotation (as in ending up looking at what you need to look at but being upside down). It takes in two rotations, a first priority look to direction, and then a second priority look direction. 
-<code c#> 
-// Get the direction to a camera 
-Vector3 directionToCamera = Camera.main.transform.position - transform.position; 
- 
-Quaternion targetRotation = Quaternion.LookRotation(-directionToCamera, Vector3.up); 
- 
-transform.rotation = Quaternion.FromToRotation(Vector3.back, directionToCamera); 
-</code> 
- 
-==== Quaternions ==== 
- 
-Number primer: [[https://www.youtube.com/watch?v=3BR8tK-LuB0|Numberphile video]]. 
- 
-They are in the form of ''x, y, z, w''. They are always normalized, so ''x² + y² + z² + w² = 0'' 
- 
-=== Creating Quaternions === 
-<code c#> 
-// This has to be normalized. We generally never use this 
-Quaternion rotation = new Quaternion(0,0,0,1); 
- 
-// You usually create a rotation in euler and then convert to quaternion. 
- 
-==== Convertions ==== 
-<code c#> 
-// Convert from Quaternion to Euler: 
-Vector3 inEuler = quaternionRotation.eulerAngles; 
- 
-// Convert Euler to Quaternion 
-Quaternion inQuaternion = Quaternion.Euler(inEuler); 
- 
-// To Angle-Axis 
-Quaternion randomQuaternion = Random.rotation; 
-float angle; 
-Vector3 axis; 
- 
-randomQuaternion.ToAngleAxis(out angle, out axis); 
- 
-// And back 
-Quaternion rotation = Quaternion.AngleAxis(angle,axis); 
-</code> 
- 
-===== Interpolation ===== 
-Modify one rotation value to another rotation value. 
- 
-SLERP - Spherical Linear Interpolation. Two ways of doing this, with a constant velocity or with a smooth variable velocity. 
- 
-LERP 
- 
-You can use both. 
  • general.1586548111.txt.gz
  • Last modified: 2020/04/10 19:48
  • by paul