PDA

View Full Version : working with rotational movement



ilKhanKerensky
03-12-2003, 11:22 AM
I am currently working on a project in OpenGL and was having a little trouble with rotation of my map. The way my map is setup is a hexagonal map, so each spot on the map is a hexagon. The base of the map is the lower left corner hexagon. In the entire matrix of hexagons, it is [0,0]. I then set the base hexagon to start out at coordinate (0, -1, 0) on the screen. The reason I am starting the y-coordinate at -1 is because if I started it at 0, the map would be in the center of the screen and would not be visible, because it is a flat map, but that is not the issue here.

Anyways, just like in several computer games, I use the left and right arrow keys to turn right and left, or in other words, to pivot on the y-axis. However, I am having a problem with the pivoting. The problem is that the base of the map stays at (0, -1, 0) at all times. But that is not what I want to happen. What I need to happen is for the map to conform to the user, in other words, the place in which my personage would be standing on the map if i were a character in the game always needs to be at (0, -1, 0), therefore making the map pivot around me.

So in other words, I want to BE the y-axis, so everything will rotate around me as I turn. But for some reason it's not working out that way.

Just in case you need to look at some of my code, I have included the two functions that would be of the most importance in this scenario, the function in which I set up OpenGL, and the function in which I do the drawing. Also notice that I am using OpenGL inside of SDL. I love SDL.

Be advised, you might not see some variables declared inside of these two functions, but you will see them used inside of these two functions. They are declared globally elsewhere.





void setup_opengl( int width, int height )
{
float ratio = (float) width / (float) height;

/* Our shading model--Gouraud (smooth). */
glShadeModel( GL_SMOOTH );

/* Set the clear color. */
glClearColor( 0, 0, 0, 0 );

/* Setup our viewport. */
glViewport( 0, 0, width, height );

/*
* Change to the projection matrix and set
* our viewing volume.
*/
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );

gluPerspective( 60.0, ratio, 1.0, 1024.0 );

myMap = new HEXMAP_DTP( 5, 5, 1.0f ); //create a 5x5 hex map with each hex having a 1 unit radius
myMap->SetBase ( 0.0f, -1.0f, 0.0f ); //base coords of map
}

static void draw_screen( void )
{
/* Clear the color and depth buffers. */
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

/* We don't want to modify the projection matrix. */
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );

/* Move down the z-axis. */
glTranslatef( 0.0, 0.0, -5.0 );
glTranslatef( 0, movy, movz );

/* Rotate. */
glRotatef( angle, 0.0f, 1.0f, 0.0f );

myMap->DrawMap();


/*
* Swap the buffers. This this tells the driver to
* render the next frame from the contents of the
* back-buffer, and to set all rendering operations
* to occur on what was the front-buffer.
*
* Double buffering prevents nasty visual tearing
* from the application drawing on areas of the
* screen that are being updated at the same time.
*/
SDL_GL_SwapBuffers( );
}


Thanx for any help you can provide. If you are confused about my explanation, instant message me or reply to this and ask me to try and explain further, and I will try to. If you want to see any more of my code I can do that also.

[This message has been edited by ilKhanKerensky (edited 03-12-2003).]

[This message has been edited by ilKhanKerensky (edited 03-12-2003).]

[This message has been edited by ilKhanKerensky (edited 03-12-2003).]

nexusone
03-12-2003, 12:30 PM
I am thinking you are making a top down view game like starcraft?

Remeber you openGL default view is -Z look down into the screen.

Now the problem is that you are back to thinking 2D, when you still need to keep in mind you are working really in 3D.

Now you are rotating your map on the Y-axis, which is a right to left rotation from on the screen.
Rotating the X-axis will give you a top to bottom rotation.

Which leaves the Z-axis which gives you a turning rotation.

I think that were you use the y-axis rotation, changed it to the z-axis.

/* Rotate. */
glRotatef( angle, 0.0f, 0.0f, 1.0f ); // myMap->DrawMap();


[This message has been edited by nexusone (edited 03-12-2003).]

ilKhanKerensky
03-12-2003, 01:23 PM
no no...I am not making a 2d game, and its not like Starcraft. It is a 3d game, and it is FPS...kind of like Doom, etc.

So I am rotating on the correct axis, that is not the problem. Let's see if I can describe it a bit better, I might have described the problem poorly.

My map uses hexes for each space on the map. I know some of you think that might be insane, because hexes are harder to work with than simple things like squares, but for the purpose of this game, which I will not go into, the map is composed of hexes. I think map is the wrong word, however. If you knew what Battletech was, then you would know that map is an okay word for the job, but not everyone here knows battletech, so map is the wrong word. That is what probably threw you off. Essentially it is the floor of the level that I am talking about. So if I was walking in a hallway, it is the floor of the hallway. It is composed of hexagons.

Now, the base of the world, which is hexagon [0,0] (I have a matrix of hexagons to represent the world), and every other hexagon is drawn on the screen depending on where that base hexagon lies.

For my purposes, the base hexagon is lieing at the coordinate (0, -1, 0). When I give that coordinate, I am talking about the center of the hexagon. From that point, more hexagons are drawn. The reason the y-value is at -1 is because it is slightly below our eyes. Just like if you were walking in a hallway, the floor of the hall is below your eyes, the map is slightly below our eyes.

So when I rotate the map, it rotates around the y-axis. Seems fine, right? Well, it's not quite right...It rotates around the axis fine, but thats where the problem lies.

Lets say I am standing on the base hex of the map, which lies on (0, -1, 0). If we turned left, it would look like we were turning left. It we turned right, it would look like we were turning right. Works fine. Then move to a different hex on the map.

Lets take, for example, the middle hex of the map. Stand on the middle hex and turn left and right...well...it doesnt quite work out. Remember how the map rotates around the y-axis? Well the middle of the map is not sitting right at the y-axis, is it? So in order to descibe its movement, I will use the solar system.

Lets say the center of the solar system is the origin of the y-axis. The sun rotates directly around it. Rather, the earch REVOLVES around it. That is kind of what the middle of the map does. Therefore, when you try and do a simple turn on the map, whenever you are not at the base point in the map, you find yourself in another spot on the map, not the same spot you were in, relatively.

Do you get my drift? If you dont I can post some pics up on my my website and give you the webaddress.

shinpaughp
03-12-2003, 02:17 PM
You might have your glRotate in wrong place.

In other words:

glMatrixMode(GL_MODELVIEW);
glRotatef(angle1, 0.0, 1.0, 0.0);//rotation about world coord (0.0,0.0,0.0)
glTranslatef(somex, somey, somez);//move to current pos
glRotatef(angle2, 0.0, 1.0, 0.0);//rotation about yourself

You probably have your rotate before the translate.