Moving to the side with gluLookAt (); C ++

I am trying to accomplish a simple task using "gluLookAt ()" in openGl C ++: "camera-based movement". I want the cam to move along the keys in the following way:

Directional Keys:

← = Turn left

β†’ = Turn right

↑ = Look up

↓ = Look down

W, S, A, D

W = move forward

S = move back

A = move left (side)

D = move to the right (side)

Q, E

Q = Left rotation

E = right spin

So far I have managed to work with all types of movements that I need, except for the side ones, using the keys "A" and "D". No matter how hard I try, when I press one of these keys, the camera never moves the way I want it to move. Someone told me that I needed to do some kind of β€œcross product,” but despite some testing, I really lost.

//variables float x1 = 0.0f, x2 = 0.0f, x3 = 0.0f, y1 = 1.0f, y2 = 1.0f, y3 = 1.0f, z1 = 5.0f, z2 = -1.0f, z3 = 0.0f; //the camera gluLookAt( x1, y1, z1, x1+x2, y2, z1+z2, x3, y3, z3); //methods void special(int key, int xx, int yy) { switch (key) { case GLUT_KEY_LEFT : camAngle -= 0.09f; x2 = sin(camAngle); z2 = -cos(camAngle); break; case GLUT_KEY_RIGHT : camAngle += 0.09f; x2 = sin(camAngle); z2 = -cos(camAngle); break; /////////////////////////////////////////////////////// void normal(unsigned char key, int x, int y) { float part = 0.9f; switch (key) { case 'w' : x1 += x2 * part ; z1 += z2 * part ; break; case 's' : x1 -= x2 * part ; z1 -= z2 * part ; break; case 'a' : ?????????????????? break; case 'd' : ?????????????????? 
+4
source share
2 answers

It is unclear where all the things that you have happen are happening since you did not publish all your code and it was disabled in strange places. But if you want to move the camera left and right, this should be pretty easy. Definition of gluLookAt() :

 gluLookAt (cameraX, cameraY, cameraZ, lookAtX, lookAtY, lookAtZ, upX, upY, upZ); 

Moving the camera left and right without changing the angle is called a β€œtruck”, and sometimes the arrows of the 1st person refer to it as a symbol striking left or right. To do this, you need to move the camera and the search point by the same amount as this:

 gluLookAt (cameraX + deltaX, cameraY + deltaY, cameraZ + deltaZ, lookAtX + deltaX, lookAtY + deltaY, lookAtZ + deltaZ, upX, upY, upZ); 

For left and right, deltaX will look like + or - some amount, while deltaY and deltaZ are likely to be 0.

+2
source

For what you want to do, you will need the camera position (pos), up the vector (up) and the camera viewing direction (dir). When you have these vectors, the rest is pretty simple. First you need to calculate the right vector, so that you can hide to the right or left, this is achieved with:

 strafe = Cross(up,dir); //Being cross the cross product of two vectors 

Once you have strafe, you just need to add it to the position:

 pos += strafe * strafeSpeed; //Strafe to one side. pos -= strafe * strafeSpeed; //Strafe to the other. 

user11 ... post will not work if you do not always look in the same direction with the same vector up.

0
source

Source: https://habr.com/ru/post/1496352/


All Articles