public
Created

Quaternion and Vector3 classes for Java

  • Download Gist
Quaternion.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
package uk.co.halfninja.math;
 
/**
* Quaternions are data structures built from unicorn horns.
*
* I nabbed this implementation from The Internet.
*/
public final class Quaternion {
private double x;
private double y;
private double z;
private double w;
//private float[] matrixs;
 
public Quaternion(final Quaternion q) {
this(q.x, q.y, q.z, q.w);
}
 
public Quaternion(double x, double y, double z, double w) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
 
public void set(final Quaternion q) {
//matrixs = null;
this.x = q.x;
this.y = q.y;
this.z = q.z;
this.w = q.w;
}
 
public Quaternion(Vector3 axis, double angle) {
set(axis, angle);
}
 
public double norm() {
return Math.sqrt(dot(this));
}
 
public double getW() {
return w;
}
 
public double getX() {
return x;
}
 
public double getY() {
return y;
}
 
public double getZ() {
return z;
}
 
/**
* @param axis
* rotation axis, unit vector
* @param angle
* the rotation angle
* @return this
*/
public Quaternion set(Vector3 axis, double angle) {
//matrixs = null;
double s = (double) Math.sin(angle / 2);
w = (double) Math.cos(angle / 2);
x = axis.getX() * s;
y = axis.getY() * s;
z = axis.getZ() * s;
return this;
}
 
public Quaternion mulThis(Quaternion q) {
//matrixs = null;
double nw = w * q.w - x * q.x - y * q.y - z * q.z;
double nx = w * q.x + x * q.w + y * q.z - z * q.y;
double ny = w * q.y + y * q.w + z * q.x - x * q.z;
z = w * q.z + z * q.w + x * q.y - y * q.x;
w = nw;
x = nx;
y = ny;
return this;
}
 
public Quaternion scaleThis(double scale) {
if (scale != 1) {
//matrixs = null;
w *= scale;
x *= scale;
y *= scale;
z *= scale;
}
return this;
}
 
public Quaternion divThis(double scale) {
if (scale != 1) {
//matrixs = null;
w /= scale;
x /= scale;
y /= scale;
z /= scale;
}
return this;
}
 
public double dot(Quaternion q) {
return x * q.x + y * q.y + z * q.z + w * q.w;
}
 
public boolean equals(Quaternion q) {
return x == q.x && y == q.y && z == q.z && w == q.w;
}
 
public Quaternion interpolateThis(Quaternion q, double t) {
if (!equals(q)) {
double d = dot(q);
double qx, qy, qz, qw;
 
if (d < 0f) {
qx = -q.x;
qy = -q.y;
qz = -q.z;
qw = -q.w;
d = -d;
} else {
qx = q.x;
qy = q.y;
qz = q.z;
qw = q.w;
}
 
double f0, f1;
 
if ((1 - d) > 0.1f) {
double angle = (double) Math.acos(d);
double s = (double) Math.sin(angle);
double tAngle = t * angle;
f0 = (double) Math.sin(angle - tAngle) / s;
f1 = (double) Math.sin(tAngle) / s;
} else {
f0 = 1 - t;
f1 = t;
}
 
x = f0 * x + f1 * qx;
y = f0 * y + f1 * qy;
z = f0 * z + f1 * qz;
w = f0 * w + f1 * qw;
}
 
return this;
}
 
public Quaternion normalizeThis() {
return divThis(norm());
}
 
public Quaternion interpolate(Quaternion q, double t) {
return new Quaternion(this).interpolateThis(q, t);
}
 
/**
* Converts this Quaternion into a matrix, returning it as a float array.
*/
public float[] toMatrix() {
float[] matrixs = new float[16];
toMatrix(matrixs);
return matrixs;
}
 
/**
* Converts this Quaternion into a matrix, placing the values into the given array.
* @param matrixs 16-length float array.
*/
public final void toMatrix(float[] matrixs) {
matrixs[3] = 0.0f;
matrixs[7] = 0.0f;
matrixs[11] = 0.0f;
matrixs[12] = 0.0f;
matrixs[13] = 0.0f;
matrixs[14] = 0.0f;
matrixs[15] = 1.0f;
 
matrixs[0] = (float) (1.0f - (2.0f * ((y * y) + (z * z))));
matrixs[1] = (float) (2.0f * ((x * y) - (z * w)));
matrixs[2] = (float) (2.0f * ((x * z) + (y * w)));
matrixs[4] = (float) (2.0f * ((x * y) + (z * w)));
matrixs[5] = (float) (1.0f - (2.0f * ((x * x) + (z * z))));
matrixs[6] = (float) (2.0f * ((y * z) - (x * w)));
matrixs[8] = (float) (2.0f * ((x * z) - (y * w)));
matrixs[9] = (float) (2.0f * ((y * z) + (x * w)));
matrixs[10] = (float) (1.0f - (2.0f * ((x * x) + (y * y))));
}
 
}
Vector3.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
package uk.co.halfninja.math;
 
public final class Vector3 {
private double x,y,z;
public double getX() {
return x;
}
 
public double getY() {
return y;
}
 
public double getZ() {
return z;
}
 
public Vector3(double ix, double iy, double iz) {
x = ix;
y = iy;
z = iz;
}
public void set(double ix, double iy, double iz) {
x = ix;
y = iy;
z = iz;
}
public double magnitude() {
return Math.sqrt(x*x+y*y+z*z);
}
public void multiply(double f) {
x *= f;
y *= f;
z *= f;
}
public void normalise() {
double mag = magnitude();
x /= mag;
y /= mag;
z /= mag;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.