Hello there,
i’m currently trying to implement a rubics cube in Android Studio using Java and OpenGL. I have no problem rendering the cube, but i can’t figure out how to rotate only a specific site of the cube. So far i’ve only figured out how to rotate the whole cube on the z axis. If i rotate the cube on the y or x axis it is not rotating the cube on it’s center x or y axis but on the axis of the “viewpoint”.
I can also use my mouse the rotate around the cube but the only thing is does is rotate the cube around me (viewpoint).
My questions:
- How do i make the camera rotate around the objects and not the objects around me.
- How can i implement side rotation, for example a U-Move is a clockwise up movement.
Sorry for my spelling english is not my first language.
code from my render class:
//relative array
private float[][] points = { //(x,y,z)
{0.0f, 0.0f, z}, //Middle
{2.1f, 0.0f, 0.0f},
{0.0f, 2.1f, 0.0f},
{-2.1f, 0.0f, 0.0f},
{-2.1f, 0.0f, 0.0f},
{0.0f, -2.1f, 0.0f},
{0.0f, -2.1f, 0.0f},
{2.1f, 0.0f, 0.0f},
{2.1f, 0.0f, 0.0f},
{0.0f, 0.0f, -2.1f},
{-2.1f,0.0f,0.0f},
{-2.1f,0.0f,0.0f},
{0.0f,2.1f,0.0f},
{2.1f,0.0f,0.0f},
{2.1f,0.0f,0.0f},
{0.0f,2.1f,0.0f},
{-2.1f,0.0f,0.0f},
{-2.1f,0.0f,0.0f},
{0.0f,0.0f,-2.1f},
{2.1f,0.0f,0.0f},
{2.1f,0.0f,0.0f},
{0.0f,-2.1f,0.0f},
{-2.1f,0.0f,0.0f},
{-2.1f,0.0f,0.0f},
{0.0f,-2.1f,0.0f},
{2.1f,0.0f,0.0f},
{2.1f,0.0f,0.0f}
};
// Call back to draw the current frame.
@Override
public void onDrawFrame(GL10 gl) {
// This method erases the previous state of the object and makes the view clear.
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, z);
gl.glRotatef(angleX, 1.0f, 0.0f, 0.0f);
gl.glRotatef(angleY, 0.0f, 1.0f, 0.0f);
gl.glRotatef(angleCube, 0.0f, 0.0f, 1.0f);
for(int i = 0; i<27; i++){
gl.glTranslatef(points[i][0], points[i][1], points[i][2]);
cube.draw(gl);
}
// Update the rotational angle after each refresh
angleX += speedX; // (NEW)
angleY += speedY; // (NEW)
// On ArrowDown-Key Rotate 90 degrees
if((angleCube > -90.0f) && (rotate == true)) {
angleCube += speedCube;
System.out.println(angleCube +" " +rotate);
} else if((angleCube < 0.0f) && (rotate == false)) {
angleCube -= speedCube;
System.out.println(angleCube +" " +rotate);
}
}
draw method from class Cube:
// Draw the shape
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise orientation
gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display)
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
// Render all the faces
for (int face = 0; face < numFaces; face++) {
// Set the color for each of the faces
gl.glColor4f(colors[face][0], colors[face][1], colors[face][2], colors[face][3]);
// Draw the primitive from the vertex-array directly
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, face*4, 4);
}
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
}
}
onTouchEvent method from SurfaceView
// Handler for touch event
@Override
public boolean onTouchEvent(final MotionEvent evt) {
float currentX = evt.getX();
float currentY = evt.getY();
float deltaX, deltaY;
switch (evt.getAction()) {
case MotionEvent.ACTION_MOVE:
// Modify rotational angles according to movement
deltaX = currentX - previousX;
deltaY = currentY - previousY;
renderer.angleX += deltaY * TOUCH_SCALE_FACTOR;
renderer.angleY += deltaX * TOUCH_SCALE_FACTOR;
}
// Save current x, y
previousX = currentX;
previousY = currentY;
return true; // Event handled
}