Advertisement
ThinMatrix

Quaternion

Jan 27th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package toolbox;
  2.  
  3. public class Quaternion {
  4.  
  5.     private double w;
  6.     private double x;
  7.     private double y;
  8.     private double z;
  9.  
  10.     public Quaternion(double w, double x, double y, double z) {
  11.         this.w = w;
  12.         this.x = x;
  13.         this.y = y;
  14.         this.z = z;
  15.     }
  16.    
  17.     public static Quaternion convertFromAxisAngle(double angle, double x, double y, double z){
  18.         double angleOverTwo = Math.toRadians(angle)/2d;
  19.         double sinAngleOverTwo = Math.sin(angleOverTwo);
  20.         double newW = Math.cos(angleOverTwo);
  21.         double newX = x * sinAngleOverTwo;
  22.         double newY = y * sinAngleOverTwo;
  23.         double newZ = z * sinAngleOverTwo;
  24.         return new Quaternion(newW, newX, newY, newZ);
  25.     }
  26.  
  27.     public static Quaternion slerp(Quaternion qA, Quaternion qB, double time) {
  28.         double cosHalfTheta = Maths.dotProductOfQuaternions(qA, qB);
  29.         if (Math.abs(cosHalfTheta) >= 1) {
  30.             return new Quaternion(qA.getW(), qA.getX(), qA.getY(), qA.getZ());
  31.         }
  32.         double halfTheta = Math.acos(cosHalfTheta);
  33.         double sinHalfTheta = Math.sqrt(1 - cosHalfTheta * cosHalfTheta);
  34.         double ratioA = 0.5;
  35.         double ratioB = 0.5;
  36.         if (Math.abs((float) sinHalfTheta) >= 0.001) {
  37.             ratioA = Math.sin((1 - time) * halfTheta) / sinHalfTheta;
  38.             ratioB = Math.sin(time * halfTheta) / sinHalfTheta;
  39.         }
  40.         double newW = qA.getW() * ratioA + qB.getW() * ratioB;
  41.         double newX = qA.getX() * ratioA + qB.getX() * ratioB;
  42.         double newY = qA.getY() * ratioA + qB.getY() * ratioB;
  43.         double newZ = qA.getZ() * ratioA + qB.getZ() * ratioB;
  44.         return new Quaternion(newW, newX, newY, newZ);
  45.     }
  46.  
  47.     public static Quaternion multiply(Quaternion left, Quaternion right) {
  48.         double w = (left.getW() * right.getW()) - (left.getX() * right.getX())
  49.                 - (left.getY() * right.getY()) - (left.getZ() * right.getZ());
  50.         double x = (left.getW() * right.getX()) + (left.getX() * right.getW())
  51.                 + (left.getY() * right.getZ()) - (left.getZ() * right.getY());
  52.         double y = (left.getW() * right.getY()) - (left.getX() * right.getZ())
  53.                 + (left.getY() * right.getW()) + (left.getZ() * right.getX());
  54.         double z = (left.getW() * right.getZ()) + (left.getX() * right.getY())
  55.                 - (left.getY() * right.getX()) + (left.getZ() * right.getW());
  56.         return new Quaternion(w, x, y, z);
  57.     }
  58.  
  59.     public Quaternion duplicate() {
  60.         return new Quaternion(w, x, y, z);
  61.     }
  62.    
  63.     public void normalize(){
  64.         double mag = Math.sqrt(w*w + x*x + y*y + z*z);
  65.         w/=mag;
  66.         x/=mag;
  67.         y/=mag;
  68.         z/=mag;
  69.     }
  70.  
  71.     public double getW() {
  72.         return w;
  73.     }
  74.  
  75.     public double getX() {
  76.         return x;
  77.     }
  78.  
  79.     public double getY() {
  80.         return y;
  81.     }
  82.  
  83.     public double getZ() {
  84.         return z;
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement