Quaternion rotation does not work as excepted

后端 未结 2 1366
野的像风
野的像风 2020-11-27 08:43

In OpenGL ES 1 for android, I have a Rubic cube that consists of 27 smaller cubes. i want rotations which cause particular small cube becoming exactly in front of the viewpo

2条回答
  •  -上瘾入骨i
    2020-11-27 08:53

    What happens is that when you apply this transform to your model (rotation in your case) you also rotate It's base vectors. Think of it as if you would also rotate your coordinate system or as if you were looking from the first person view of your model. Every transform you make will effect the next one.

    Since you generally want to keep your own coordinate system you might want to consider moving your camera around the cube rather then rotate the cube. I am sure you can find a "lookAt" method either in your API or on web. It should take 3 vectors: cameraPosition, lookAtPoint, upVector. With this approach you could position the cube to (0,0,0) which is also your "lookAtPoint", first cameraPosition should be something like (0,0,-1) and first upVector to (0,1,0). Now for the movement (You probably only use left/right and up/down as input): To go up/down (your rotation around X) you next to do the following:

    originalDistance = (cameraPosition-objectPosition).lenght
    leftVector = normalizedVector(crossProduct(camearPosition, upVector))//generaly cameraPosition-objectPosition
    camearPosition = cameraPosition + upVector*inputScalar //inputScalar should be a small floating value
    cameraPosition = normalizedVector(cameraPosition)*originalDistance //put camera to original distance from object
    upVector = normalizedVector(crossProduct(cameraPosition, leftVector))//generaly cameraPosition-objectPosition
    

    To go left/right (your rotation around X) you next to do the following:

    originalDistance = (cameraPosition-objectPosition).lenght
    leftVector = normalizedVector(crossProduct(camearPosition, upVector))//generaly cameraPosition-objectPosition
    camearPosition = cameraPosition + leftVector*inputScalar //inputScalar should be a small floating value
    cameraPosition = normalizedVector(cameraPosition)*originalDistance //put camera to original distance from object
    leftVector = normalizedVector(crossProduct(cameraPosition, upVector))//generaly cameraPosition-objectPosition
    upVector = normalizedVector(crossProduct(cameraPosition, leftVector))//generaly cameraPosition-objectPosition
    

    This should generally solve the problem.. (pleas tell me if I made a mistake as I am writing this by hard)

    As for your approach of rotating the object itself, you should find out what is your quaternion in object's own coordinate system and rotate it around that one. It is also quite easy if you have some math skills. Other then that you could also just define 2 angles (X,Y) and change them directly through input and use quaternions of (1,0,0,X) and (0,1,0,Y) but there might be problems with this approach when Y is 90 degrees..

    I hope this helps.

提交回复
热议问题