How move the camera around the world, simulating that the objects are which are moving in OpenGL

Viewed 59

I'm trying to make some cubes move around responding to users interact. First, I realize that's better to move the camera than translate all the cubes. Te results that I want to achieve is the cubes follow my finger around the screen.

I convert the next code:

original source

In java code like this:

switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float[] multy = new float[3];
                float[] suma = new float[3];
                float cameraSpeed = 0.07f;
                float[] cross = normalizar(cross(renderer.getCameraFront(), renderer.getCameraUp()));
                for (int i = 0; i < multy.length; i++) {
                    multy[i] = cameraSpeed * cross[i];
                }
                for (int i = 0; i < suma.length; i++) {
                    suma[i] = renderer.getCameraPos()[i] + (x > getWidth() / 2 ? -multy[i] : multy[i]);
                }
                renderer.setCameraPos(suma);
                requestRender();
                break;
        }
    }
    return true;
}

public float[] normalizar(float[] vector) {
    float[] vectorNormalizado = new float[3];
    double length = Math.sqrt(Math.pow(vector[0], 2) + Math.pow(vector[1], 2) + Math.pow(vector[2], 2));
    if (length != 0) {
        vectorNormalizado[0] = (float) (vector[0] / length);
        vectorNormalizado[1] = (float) (vector[1] / length);
        vectorNormalizado[2] = (float) (vector[2] / length);
    }
    return vectorNormalizado;
}

public float[] cross(float[] v0, float[] v1) {
    float[] crossProduct = new float[3];
    crossProduct[0] = v0[1] * v1[2] - v0[2] * v1[1];
    crossProduct[1] = v0[2] * v1[0] - v0[0] * v1[2];
    crossProduct[2] = v0[0] * v1[1] - v0[1] * v1[0];

    return crossProduct;
}

Now I'm able to move the cubes horizontally. The problem is that the cubes move in a constant velocity and don't take care of the x point of the event, only I use it to move to the right or left relative to the middle of the screen.

How can make that the cubes move following the x point of the event?

Finally, there is any way to make it work vertically too?

0 Answers
Related