Hello.
I need help with quanterion. I have spent on this all saturday and couple hour today and still I don’t know how to do it properly (I feel so dumb).
I want to rotate cube (around on own x,y or z axis) in fact on the beginning only two wall front and right.
//front wall
-0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
//right wall
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, -0.5f, -1.0f,
0.5f, 0.5f, -1.0f
So if I want to make a rotation on Z axis (roll) I must to do something with 8 vectors and I did something like this
private final static float DEGTORAD = (float)(Math.PI/180);
private static float rollAngle = 4;
private static Quaternion roll = new Quaternion();
private Quaternion change;
private float[] array = new float[]{
-0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, -0.5f, -1.0f,
0.5f, 0.5f, -1.0f};
private FloatBuffer vertexData;
private void update(){
roll();
for(int i = 0;i<array.length;i+=3) {
change = new Quaternion(array[i], array[i+1], array[i+2], 0.0f);
Quaternion.mul(roll, change, change);
array[i] = change.x;
array[i+1] = change.y;
array[i+2] = change.z;
}
vertexData.put(array);
vertexData.flip();
}
public static void roll() {
roll.setFromAxisAngle(new Vector4f(0, 0, 1, rollAngle * DEGTORAD));
roll.normalise();
}
public void gameLoop(){
...
while(running) {
update();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();//Matrix reseting.
GL11.glTranslatef(0.0f, 0.0f, -6.0f); // Move Right And Into The Screen
GL11.glRotatef(80, 0.5f, 0.5f, -0.5f); // Rotate The Cube On X, Y & Z
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(vertexSize, 0, vertexData);
glColorPointer(colorSize, 0, colorData);
glDrawArrays(GL_QUADS, 0, amountOfVertices);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
Display.sync(60);
Display.update();
}
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
Display.destroy();
}
and I must admit that the front wall rotate properly also right but unfrtunateli something bad happening with right wall namely
is getting smaller and smaller. The same happens with front wall if I change rotation e.g.
roll.setFromAxisAngle(new Vector4f(1, 0, 0, rollAngle * DEGTORAD));
http://img859.imageshack.us/img859/2908/picqq.png
What’s wrong how to do this properly?
I think i should create this topic in ‘LWJGL Development’ it’s more appropriate place. Can someone move it.