i guess i might as well show my entire quaternion class... here it goes:
I might as well send my entire quaternion class... here it goes...
package spacesim;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
public class Quaternion {
float x,y,z,w;
public Quaternion(){
set(1,0,0,0);
}
void set(float W, float X, float Y, float Z){
x=X;
y=Y;
z=Z;
w=W;
}
void setRotation(float angle, float xAngle, float yAngle, float zAngle){
this.w = (float)Math.cos( angle/2);
this.x = (float)(xAngle * Math.sin( angle/2 ));
this.y = (float)(yAngle * Math.sin( angle/2 ));
this.z = (float)(zAngle * Math.sin( angle/2 ));
}
void applyRotation(float angle, float xAngle, float yAngle, float zAngle){
Quaternion c = new Quaternion();
c.setRotation(angle,xAngle,yAngle,zAngle);
c = multiply(this, c);
set(c.w, c.x, c.y, c.z);
}
void normalize(){
float mag = (float)Math.sqrt(Math.pow(x,2) + Math.pow(y,2) + Math.pow(z,2));
x/=mag;
y/=mag;
z/=mag;
w/=mag;
}
void getMatrix(){
float[] matrix = new float[16];
matrix[0] = 1 - (float)(2*Math.pow(y, 2)) - (float)(2*Math.pow(z,2));
matrix[1] = (2 * x * y) - (2 * w * z);
matrix[2] = (2 * x * z) + (2 * w * y);
matrix[3] = 0;
matrix[4] = (2 * x * y) - (2 * w * z);
matrix[5] = 1 - (float)(2*Math.pow(x, 2)) - (float)(2*Math.pow(z,2));
matrix[6] = (2 * y * z) + (2 * w * z);
matrix[7] = 0;
matrix[8] = (2 * x * z) - (2 * w * y);
matrix[9] = (2 * y * z) - (2 * w * z);
matrix[10] = 1 - (float)(2*Math.pow(x, 2)) - (float)(2*Math.pow(y,2));
matrix[11] = 0;
matrix[12] = 0;
matrix[13] = 0;
matrix[14] = 0;
matrix[15] = 1;
FloatBuffer m2 = bb.asFloatBuffer();
for(int a =0; a < 16; a++){
m2.put(a,matrix[a]);
}
System.out.println("MATRIX");
GL11.glLoadMatrix(m2);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, m2);
for(int a =0; a < 16; a++){
System.out.println(m2.get(a));
}
FloatBuffer fb = BufferUtils.createFloatBuffer(16);
for(int a=0; a<16; a++){
fb.put(matrix[a]);
}
// now that we have stuffed it with data, we need to flip it so that position is placed at the beginning of data and end limit is set to current position
fb.flip();
GL11.glLoadMatrix(fb);
//return matrix;
}
Quaternion multiply(Quaternion a, Quaternion b){
Quaternion c = new Quaternion();
c.w = (a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z);
c.x = (a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y);
c.y = (a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x);
c.z = (a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w);
return c;
}
}