Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I translate first or rotate first?

I'm trying to create a simple scene where I can walk around, with the criteria of being able to pan around and walk around with the keys. However, in my draw scene function, when I translate my scene than rotate, the panning around doesn't work properly as the entire scene just rotates around me, causing objects to go through me. When I rotate than translate my scene, I can pan around properly, however, I can move only in a certain direction, so if I pan around to my right 90 degrees, I'll move left instead of going forward. Is there anyway where I can put these 2 effects together?

This is the code that I use to draw my view:


glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();

glTranslated(xposition, 0, zposition); //This is where I translate my views
glRotated(yrot, 0, 1, 0); //

glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER_ARB, quadVBO);

glNormalPointer(GL_FLOAT, 0, (void*)sizeof(sideArray));
glColorPointer(3, GL_FLOAT, 0, (void*)sizeof(sideArray)+sizeof(normals));
glVertexPointer(3, GL_FLOAT, 0, 0);

glDrawArrays(GL_QUADS, 0, sizeof(sideArray)/sizeof(GLfloat)/3);

glPopMatrix();
glFlush();
glDisableClientState(GL_VERTEX_ARRAY);  
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);

Here are some pictures that illustrate my problem right now:

Rotate then translate:

Pic1

http://dl.dropbox.com/u/2747708/Screen%20Shot%202012-04-03%20at%2010.17.39%20PM.PNG

Pic2

I can imitate the turn of the camera

http://dl.dropbox.com/u/2747708/Screen%20Shot%202012-04-03%20at%2010.17.48%20PM.PNG

Pic3

However, when I walk forward, it only walks in one direction, and not the direction I'm looking at.

http://dl.dropbox.com/u/2747708/Screen%20Shot%202012-04-03%20at%2010.18.30%20PM.PNG

http://dl.dropbox.com/u/2747708/Screen%20Shot%202012-04-03%20at%2010.18.39%20PM.PNG

Translate then Rotate:

Pic1

http://dl.dropbox.com/u/2747708/Screen%20Shot%202012-04-03%20at%2010.19.44%20PM.PNG

Pic2

I can move around freely, walking straight to any direction I'm looking at.

http://dl.dropbox.com/u/2747708/Screen%20Shot%202012-04-03%20at%2010.19.52%20PM.PNG

Pic3

However, when I rotate the scene, the entire thing rotates, which causes objects to clip through me and doesn't "pan" through the view anymore like when I rotate then translate my view.

http://dl.dropbox.com/u/2747708/Screen%20Shot%202012-04-03%20at%2010.20.01%20PM.PNG

like image 625
TheAmateurProgrammer Avatar asked Oct 19 '25 12:10

TheAmateurProgrammer


1 Answers

Your problem isn't with the order of transformations. You should be rotating then translating.

The problem is that you aren't taking rotation into account when moving. The formula for movement:

movementX = sin(direction);
movementY = cos(direction);

where direction is the number of radians turned clockwise from north, and positive X is east, and positive Y is north.

like image 191
Kendall Frey Avatar answered Oct 21 '25 04:10

Kendall Frey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!