Rotating Character In 3D World
I have created a 3D World based on tutorial 10. I have drawn a cube for my character and have his Zpos 1 unit infront of me (so i can see him, later will chance to be on my coords) The char moves forward when i press the up arrows and back with the back arrow. They use my positions Z and X coordinates for this. The only problem i have is when i turn the character doesnt turn with me. I know i need to use the glRrotatef(); the only thing i dont know is how to make him turn with me. I tried putting the yrot but i got weird results. Is there anyone who can help me out? If you need to know anything else about my source let me know.
This is how i show my cube...
//draw character
glBindTexture(GL_TEXTURE_2D, texture[6]);
glBegin(GL_QUADS);
//front
glTexCoord2f(0.0f,1.0f); glVertex3f(xpos+0.3f, 0.5f,zpos-1.0f);
glTexCoord2f(1.0f,1.0f); glVertex3f(xpos-0.3f, 0.5f,zpos-1.0f);
glTexCoord2f(1.0f,0.0f); glVertex3f(xpos-0.3f, -0.5f,zpos-1.0f);
glTexCoord2f(0.0f,0.0f); glVertex3f(xpos+0.3f, -0.5f,zpos-1.0f);
//right
glTexCoord2f(0.0f,1.0f); glVertex3f(xpos+0.3f, 0.5f,zpos-1.0f);
glTexCoord2f(1.0f,1.0f); glVertex3f(xpos+0.3f, 0.5f,zpos-1.3f);
glTexCoord2f(1.0f,0.0f); glVertex3f(xpos+0.3f, -0.5f,zpos-1.3f);
glTexCoord2f(0.0f,0.0f); glVertex3f(xpos+0.3f, -0.5f,zpos-1.0f);
//left
glTexCoord2f(0.0f,1.0f); glVertex3f(xpos-0.3f, 0.5f,zpos-1.0f);
glTexCoord2f(1.0f,1.0f); glVertex3f(xpos-0.3f, 0.5f,zpos-1.3f);
glTexCoord2f(1.0f,0.0f); glVertex3f(xpos-0.3f, -0.5f,zpos-1.3f);
glTexCoord2f(0.0f,0.0f); glVertex3f(xpos-0.3f, -0.5f,zpos-1.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[5]);
glBegin(GL_QUADS);
//back
glTexCoord2f(0.0f,1.0f); glVertex3f(xpos+0.3f, 0.5f,zpos-1.3f);
glTexCoord2f(1.0f,1.0f); glVertex3f(xpos-0.3f, 0.5f,zpos-1.3f);
glTexCoord2f(1.0f,0.0f); glVertex3f(xpos-0.3f, -0.5f,zpos-1.3f);
glTexCoord2f(0.0f,0.0f); glVertex3f(xpos+0.3f, -0.5f,zpos-1.3f);
glEnd();
[edited by - Eric_003 on February 22, 2003 2:10:11 AM]
since your drawing a cube, then locally the cube will NEVER change, it will always just be a cube, so having the lieks of:
glVertex3f(xpos+0.3f, 0.5f,zpos-1.0f);
is very inefficient and will only cause problems.
This is the purpous of the modelview matrix, to make these standard translations/rotations/scales/etc for you, for free.
simply call glTranslatef(xpos,0,zpos) after you call glRotatef(..)
push the matrix onto the stack before drawing the cube though to preserve the camera matrix. obviously pop it afterwards
| - Project-X - my mega project.. yup, still cracking along
- | - adDeath - an ad blocker I made - | - email me - |
glVertex3f(xpos+0.3f, 0.5f,zpos-1.0f);
is very inefficient and will only cause problems.
This is the purpous of the modelview matrix, to make these standard translations/rotations/scales/etc for you, for free.
simply call glTranslatef(xpos,0,zpos) after you call glRotatef(..)
push the matrix onto the stack before drawing the cube though to preserve the camera matrix. obviously pop it afterwards
| - Project-X - my mega project.. yup, still cracking along

I changed all my vertext lines like this one...
glVertex3f(xpos+0.3f, 0.5f,zpos-1.0f);
by taking out the xpos and zpos like you said to do and used a translate instead. It still works how it did before and it is alot easier and mor effcient then the way i had it.
Im not sure what you mean about the model view maxtrix though, with pushing and poping. Are those asm commands? i hope not since i dont know asm very well at all, I''m using VC++.
Well any more help would be great
glVertex3f(xpos+0.3f, 0.5f,zpos-1.0f);
by taking out the xpos and zpos like you said to do and used a translate instead. It still works how it did before and it is alot easier and mor effcient then the way i had it.
Im not sure what you mean about the model view maxtrix though, with pushing and poping. Are those asm commands? i hope not since i dont know asm very well at all, I''m using VC++.
Well any more help would be great

glpushmatrix/glpopmatrix are opengl commands for temporary storing curent matrix. a simpe use would be like this. you ahve to draw N cubes at diferent positions in space. just push matrix, translate to first cube draw cube. now pop matrix and you are in same position whre you were before pushing. now the same for next...
You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
I just have one other minor problem. When i move my character it seems to speed up and slow down movement at differnt times. I noticed that tutorial 10 also does this when you run it. Is there something i can do about this so its a consistant pace for movement?
I have tried to base my movement on time but am not having any luck doing this. I always get errors when trying to add it in. I have used NeHe tutorial 21, but i must be missing some code or not done something right since i get errors when running it. I also tried the one from gametutorials.com but the progrma is setup totally different from the way they hadve your code since they have it devided up into 3 separate files and is a bit hard to follow since I''m used to the way NeHe tutorials are written. So if anyone could help me implement time based movement into my program, and also briefly explain how it works. If you require any or all of my code let me know.
To use time based movement, you first need to know how far your character moves per a frame. A vector is good for doing this.
Before you add the vector to the current position, you check how much time past since the last frame. Then if a lot of time past since the last frame, you multiply by a large number to increase distance moved.
For example, if 20 milliseconds past, and you like how it moves at 15 milliseconds per a frame, you multiply the vector by 20/15 to increase the distance moved. If the frame past quickly, lets say 10 milliseconds, you would have multiplied by 10/15 thus decreasing distance moved.
Before you add the vector to the current position, you check how much time past since the last frame. Then if a lot of time past since the last frame, you multiply by a large number to increase distance moved.
For example, if 20 milliseconds past, and you like how it moves at 15 milliseconds per a frame, you multiply the vector by 20/15 to increase the distance moved. If the frame past quickly, lets say 10 milliseconds, you would have multiplied by 10/15 thus decreasing distance moved.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement