Skip to content Skip to sidebar Skip to footer

Android : Opengl 2.0 Rotation/moving The Camera Using Matrix.setlookatm

So this is my second question today, I might be pushing my luck In short making a 3D first Person, where you can move about and look around. In My OnDrawFrame I am using Matrix.set

Solution 1:

First of all I would suggest not to trace the look vector but rather forward vector, then in lookAt method use eye+forward to generate look vector. This way you can loose the update on the look completely when moving, and you don't need to compute the v vector (mRenderer.eyeX += forward.x * SPEED_MOVE;...)

To make things more simple I suggest that you normalize the vectors forward and up whenever you change them (and I will consider as you did in following methods).

Now as for rotation there are 2 ways. Either use right and up vectors to move the forward (and up) which is great for small turning (I'd say about up to 10 degrees and is capped at 90 degrees) or compute the current angle, add any angle you want and recreate the vectors.

The first mentioned method on rotating is quite simple:

vector forward = forward
vector up = up
vector right=cross(forward, up) //this one might be the other way around as up, forward :)
//going leftorright:
forward = normalized(forward +right*rotationSpeedX)
//going up or down:
forward = normalized(forward + up*rotationSpeedY)
vector right=cross(forward, up) //this one might be the other way around
vector up = normalized(cross(forward, right)) //this one might be the other way around
//tilt leftorright:
up = normalized(up +right*rotationZ)

The second method needs a bit trigonometry:

Normally to compute an angle you could just call atan(forward.z/forward.x) and add some if statements since the produced result is only in 180 degrees angle (I am sure you will be able to find some answers on the web to get rotation from vector though). The same goes with up vector for getting the vertical rotation. Then after you get the angles you can easily just add some degrees to the angles and recreate the vectors with sin and cos. There is a catch though, if you rotate the camera in such way, that forward faces straight up(0,1,0) you need to get the first rotation from up vector and the second from forward vector but you can avoid all that if you cap the maximum vertical angle to something like +- 85 degrees (and there are many games that actually do that). The second thing is if you use this approach your environment must support +-infinitive or this atan(forward.z/forward.x) will brake if forward.x == 0.

And some addition about the first approach. Since I see you are trying to move around the 2D space your forward vector to use with movement speed should be normalized(forward.x, 0, forward.z), it is important to normalize it or you will be moving slower if camera tilts up or down more. Second thing is when you rotate left/right you might want to force up vector to (0,1,0) + normalize right vector and lastly recreate the up vector from forward and right. Again you then should cap the vertical rotation (up.z should be larger then some small value like .01)

Solution 2:

It turned out my rotation code was wrong

if (x > mPreviousX )
{
 mRenderer.lookX  = (float) (mRenderer.eyeX +  ((Math.cos(SPEED_TURN / 2) * v[0]) - (Math.sin(SPEED_TURN / 2) * v[2])));
 mRenderer.lookZ  =  (float) (mRenderer.eyeZ +  ((Math.sin(SPEED_TURN / 2) * v[0]) + (Math.cos(SPEED_TURN / 2) * v[2])));
}
else
{
 mRenderer.lookX  =  (float) (mRenderer.eyeX + ((Math.cos(-SPEED_TURN / 2) * v[0]) - (Math.sin(-SPEED_TURN / 2) * v[2])));
 mRenderer.lookZ  =  (float) (mRenderer.eyeZ + ((Math.sin(-SPEED_TURN / 2) * v[0]) + (Math.cos(-SPEED_TURN / 2) * v[2])));
}

Post a Comment for "Android : Opengl 2.0 Rotation/moving The Camera Using Matrix.setlookatm"