# Characters following my tile, trig in 3D

This topic is 3988 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone. I'm very very new to OpenGL and dealing with anything in 3D, and I'm having a slight problem visualizing how to apply trig in 3D. I didn't think that what I was trying to create is quite isometric, but I was direced here from the OpenGL forum. Anyway, I'm trying to create an isometric tile map that can be rotated on the z axis. I'm using OpenGL, and here's how I'm setting things up:
glMatrixMode(GL_PROJECTION);
glOrtho(-400.0f, 400.0f, -300.0f, 300.0f, -1000.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);

glRotatef(-60.0f, 1.0f, 0.0f, 0.0f);
glRotatef(zrot + 45.0f, 0.0f, 0.0f, 1.0f);


(zrot increases/decreases when I press left/right) So my first rotation should rotate my z axis "upwards", so that it's 30 degrees off of where my y axis was, and my y axis "downwards", 30 degrees off of where the z was. Then I rotate 45 on the z so that the x and y look like this: http://img85.imageshack.us/img85/5406/axesne0.png After doing this I can draw a couple tiles like so:
glColor3f(1.0, 1.0, 0.0);
glVertex3f(-100.0f, -100.0f, 0.0f);
glVertex3f(100.0f, -100.0f, 0.0f);
glVertex3f(100.0f, 100.0f, 0.0f);
glVertex3f(-100.0f, 100.0f, 0.0f);

glVertex3f(101.0f, 101.0f, 0.0f);
glVertex3f(301.0f, 101.0f, 0.0f);
glVertex3f(301.0f, 301.0f, 0.0f);
glVertex3f(101.0f, 301.0f, 0.0f);
glEnd();


This is fine, and what I want so far. The problem arises when I try to draw what will be my unit that sits on the tile. My units will be 2D, so I want them to face the camera at all times, which means rotating on the z axis is not a good idea (I'm not trying to create an isometric game of Paper Mario). So before drawing them I cancel any effect of zrot and then draw:
glRotatef(-zrot, 0.0f, 0.0f, 1.0f);

glColor3f(1.0, 0.0, 0.0);
glVertex3f(151.0f, 251.0f, 0.0f);
glVertex3f(151.0f, 251.0f, 150.0f);
glVertex3f(251.0f, 151.0f, 150.0f);
glVertex3f(251.0f, 151.0f, 0.0f);
glEnd();


The result is this: http://img412.imageshack.us/img412/5618/shapesjd4.png This is what I'm aiming for when the tiles are rotated that way, but when zrot is something other than 0, I want the unit to follow the tile. This I am not quite sure how to handle this translation, but I tried a few things. The most sensible thing I could think of would be to ignore the z axis and figure out x and y displacement. I came up with this:
GLfloat dx = cos(pi*zrot/180)*sin(pi*zrot/180)*201.0f;
GLfloat dy = sin(pi*zrot/180)*sin(pi*zrot/180)*201.0f;


This isn't getting me what I want though, and I can't get a good picture in my mind of what this is doing. Any ideas? I'm guessing the solution is fairly simplistic and something I should be able to figure out easily, which is what I find most frustrating. Thanks in advance.

##### Share on other sites
Don't know if I understand your problem correctly.. but I think I have done what you want, although I don't rotate around Z :P

I got an OpenGL isometric perspective in my project too and I print text that is aligned to the screen. I do this in the following way:

glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-(mWidth/2), (mWidth/2), -(mHeight/2), (mHeight/2), -4000, 4000);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glRotated(-60,1,0,0);glRotated(45,0,0,1);// Draw scene stuff ---

Then when I want to print my text I first translate it in world coords then I rotate the view so the text faces the screen:

glTranslated(-y,-x,z); // I got a strange coordinate systemglRotated(-45,0,0,1);glRotated(60,1,0,0);// Draw text stuff ---

Shouldn't it be possible to offset/add the Z angle in the rotations?

##### Share on other sites
Hi O-san. Thanks for the response.

That's such a simple solution and it worked perfectly! I think I have a ways to go as far as getting used to rotating axes.

Although the only problem I have with this is that I think it might be somewhat inefficient if I have to draw a lot of items at the world view. I imagine for your program you won't need to go back and forth between the isometric and world views after you draw the text once. But if I have 60 tiles and 30 of them have a character or an object on them, that's a fair number of rotations for one frame.

Looks like you have quite an interesting project going on by the way. I'm tracking your journal now :)

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633703
• Total Posts
3013455
×