简体   繁体   中英

jPCT: Strange camera rotation

I have created Rubik's cube by jPCT and now I need to rotate this whole cube. I have tried to achieve this by rotation matrixes and I have rotated single cube elements but this does not seem to be good way..

So I want to rotate my camera around the cube instead of rotating the cube. It is pretty easy but problem is that jPCT changes the orientation of my camera randomly or I have done some another mistake and I am unable to fix it.

SimpleVector cameraPos = new SimpleVector(-20, 0, 0);
SimpleVector cubeCenter = new SimpleVector(2, 2, 2);

while (!org.lwjgl.opengl.Display.isCloseRequested()) { 
    refreshScene();

    // Camera position is repeatedly rotated
    cameraPos.rotateAxis(new SimpleVector(0, 0, 1), (float) Math.toRadians(1));
    // Here I set camera position
    world.getCamera().setPosition(cameraPos);
    // Camera looks at the center of cube, but unfortunately
    // not with fixed orientation
    world.getCamera().lookAt(cubeCenter);

    try {
        Thread.sleep(50);
    } catch (InterruptedException e) {

    }
}

Above code performs this strange rotation of cube:

当前相机旋转

That is cool but I need rotate my cube like this: 所需的相机旋转

I have tried to set camera orientation by setOrientation method:

SimpleVector upVector = world.getCamera().getUpVector();
upVector.scalarMul(-1.0f);
world.getCamera().setOrientation(world.getCamera().getDirection(), upVector);

The last line in this code should IMHO turn camera orientation upside down, but it just does nothing. I use last version of jPCT.

How can I achieve right camera orientation? Any help is very welcome!

If you want to rotate the cube, which seems to be what you actually want to do, why not simply put a dummy Object3D in its center, make all elements of the cube children of that dummy and only rotate the dummy? That should actually give you the desired results. About your approach: You can be sure that the rotations aren't random. You get what you ask for in your code. Why this results in what you are actually seeing here, is hard to tell without knowing your complete scene setup. Anyway, the easiest way to rotate the camera around some fixed point in space, is to make it look at that point in the initial setup, then do something like this:

cam.moveCamera(Camera.CAMERA_MOVEIN, distance);
cam.rotateAxis(<some axis>, <float>);
cam.moveCamera(Camera.CAMERA_MOVEOUT, distance);

Where distance is the initial distance from the camera to the rotation pivot. You can find an example here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM