Sign in to follow this  
Wavesonics

Camera Roation

Recommended Posts

Wavesonics    330
Ok I'm having some trouble w\ my camera rotation. I know there are camera classes out there but I want to learn this. This cam will have no lateral roation or movement, just side to side rotation and forward and back translation. So this is what I got: For Input: if (keys[VK_UP]) // Is The Up Arrow Being Pressed? { ztranslate += sin((float)yangle) * .005; xtranslate += cos((float)yangle) * .005; } if (keys[VK_DOWN]) // Is The Down Arrow Being Pressed? { ztranslate -= sin((float)yangle) * .005; xtranslate -= cos((float)yangle) * .005; } if (keys[VK_RIGHT]) // Is The Right Arrow Being Pressed? { yangle += .01; } if (keys[VK_Left]) // Is The Left Arrow Being Pressed? { yangle -= .01; } This is what I have for the actual movement and rotation: // Move Camera glRotatef(yangle, 0.0f, 1.0f, 0.0f); glTranslatef(xtranslate, 0.0f, ztranslate); As you can see I leave the Y alone. Now the rotation is working great. The translation is the problem. The idea I have is I know the angle (yangle) of a right triangle. So I can use sin to get the Opp side which should b my Z cord and I can use Cos to get my Adj side which should b my X cord. then I multiplay by .005 so that I control the "speed" or distance of the translation. This just give me the point I want to move to right? So then I just plug that point into my glTranslate function and it just go, but it doesnt... Here take a look at the unpreditable responses: http://www.talentopoly.com/adam/UPLOADS/cpp/WinMain.exe it's not a virus, i'm not competent enough to program a virus, so dont worry.

Share this post


Link to post
Share on other sites
Wavesonics    330
Well... the world. Everything, those come before i draw anything.
Take a look @ that sample prog I linked to, it almost works. It's just the direction of the translation is screwed up some times.

Share this post


Link to post
Share on other sites
catch    192
Okay, maybe the camera idea is a little vague. As far as I know there is no actual camera in OpenGL. It's a concept, nothing more.

Basically, according to your code, you aren't switching your matrix mode to projection. Therefore just calling rotate and translate out in the middle of nowhere will simply rotate and translate the last thing in the matrix stack, which could be anything at all, and not even the projection matrix.

Then again your code sample looks cut-n-pasted, so I can't tell if you're doing that or not.

What have you got against gluLookAt, anyway? ;)

Share this post


Link to post
Share on other sites
jakem3s90    437
Hey there,

Try this

if (keys[VK_UP]) // Is The Up Arrow Being Pressed?
{
ztranslate -= sin((float)yangle) * .005;
xtranslate += cos((float)yangle) * .005;
}
if (keys[VK_DOWN]) // Is The Down Arrow Being Pressed?
{
ztranslate += sin((float)yangle) * .005;
xtranslate -= cos((float)yangle) * .005;
}


The do your


glRotatef(yangle, 0.0f, 1.0f, 0.0f);
glTranslatef(xtranslate, 0.0f, ztranslate);


If you walk the wrong way, then switch all of your += and -= around.

If you check out the law of sines, you'll see what you're doing wrong. Let me know if this helps!

PS: This is a great math tutorial to get this stuff sorted out.
Clicky

Share this post


Link to post
Share on other sites
Wavesonics    330
hehe i dont have anything against gluLookAt what I can't figure out is how to set the direction to look in correctly every time.

Ur right I didnt post the full code here, i coppied in snippets, but it's all my code if thats what u mean.

I'm not rotating the Projection Matrix. I'm rotating the modle view matrix as far as I know... I guess thats a problem huh...

Share this post


Link to post
Share on other sites
jakem3s90    437
Quote:
Original post by Wavesonics
hehe i dont have anything against gluLookAt what I can't figure out is how to set the direction to look in correctly every time.

Ur right I didnt post the full code here, i coppied in snippets, but it's all my code if thats what u mean.

I'm not rotating the Projection Matrix. I'm rotating the modle view matrix as far as I know... I guess thats a problem huh...


No, you're going to want to use the ModelView matrix.

Share this post


Link to post
Share on other sites
Wavesonics    330
Ok I just tried your code and it seems to have about the same effect look:
http://www.talentopoly.com/adam/UPLOADS/cpp/WinMain2.exe

It work for some directions (read: angles) but gets all screwy when u rotate to other angles.

Also could U explain the logic behind the changes u made 2 the math code there? I really wanna learn this.

Share this post


Link to post
Share on other sites
Wavesonics    330
Yes I'm loading the ident matrix.

Ya sure I'll post the code, its incredibly dirty right now b/c i've been commenting out stuff and working around things and u'll see... once i get this working and i know what i need i'll clean it up.

WinMain (win32 entry point):
Here you'll want to look at line 183 for my render calls to the OpenGLOps Object called OGLO:
http://www.talentopoly.com/adam/UPLOADS/cpp/WinMain.cpp

My OpenGL Operations Included into WinMain:
Here you'll want to look at SetUpFrame() on line 107:
http://www.talentopoly.com/adam/UPLOADS/cpp/OpenGLOps.cpp

OpenGL Operations header:
Just my class declaration...
http://www.talentopoly.com/adam/UPLOADS/cpp/OpenGLOps.h

[Edited by - Wavesonics on August 4, 2004 1:42:30 PM]

Share this post


Link to post
Share on other sites
Wavesonics    330
Ok I must be close b/c I found this math code in a tutorial with a working camera:

if (keys[VK_UP])
{
xpos -= (float)sin(heading*piover180) * 0.005f;
zpos -= (float)cos(heading*piover180) * 0.005f;
}

Now it looks like hes converting degrees to radians by multiplying degrees (heading) by pi / 180. Besides that my math and his math is exactly the same.

Why would converting it to radians matter?

Then he takes the xpos and zpos and inverts them for the final translation:

GLfloat xtrans = -xpos;
GLfloat ztrans = -zpos;
glTranslatef(xtrans, ytrans, ztrans);

Share this post


Link to post
Share on other sites
jakem3s90    437
Just looking through your code, it looks like you're not converting your angles into radians. C++'s sin and cos functions expect radians. Just multiply your angle by pi*180 to do that.

EDIT: WOOPS! Should have refreshed the page before posting... BTW, which compiler are you using?

Share this post


Link to post
Share on other sites
Wavesonics    330
haha, um Dev C++ is my IDE, MinGW compiler.

Well that explains why hes converting to radians, thank you.

Now I plugged his code into my code and it still doesnt work... And I'm pretty sure I transfered it perfectly.

Aw 1 small thing, he stores yrot (rotation) and heading seperately. But I cant see why, as far as I can tell yrot is set = to heading, and is never changed. But heading is used for the translation calculations not yrot.

Share this post


Link to post
Share on other sites
Wavesonics    330
jakem3s90 do u have icq or aim or something so maybe we could talk in real time?
ICQ: 39886687
AIM: Jebradic
YAHOO: Jebradic
MSN: wavesonics@hotmail.com

Share this post


Link to post
Share on other sites
jakem3s90    437
Ok.. Here's a couple things to try

(I'm at work, so I can't really check it myself)


Restructure your render loop as such:

First, pull the glMatrixMode(GL_MODELVIEW) and the glLoadIdentity() calls from your render loop, stick them just before you start that loop.

Also, call glPushMatrix() before you call OGLO.RenderFrame() and a glPopMatrix() after.

Other than that, you're code looks good, it's hard to tell without really digging through it though.

Share this post


Link to post
Share on other sites
jakem3s90    437
Quote:
Original post by Wavesonics
jakem3s90 do u have icq or aim or something so maybe we could talk in real time?
ICQ: 39886687
AIM: Jebradic
YAHOO: Jebradic
MSN: wavesonics@hotmail.com



Just PM'd you my info...

Share this post


Link to post
Share on other sites
Wavesonics    330
haha ok, so removing the ident matrix from the loop, its job is essentialy replaced by the push and pop of the matrix. Other wise things go all scewy.

Share this post


Link to post
Share on other sites
chawk    122
I only looked through the code briefly but it seems that you're not clearing the modelview matrix each time you call renderframe()? Additionally, the only thing I thought was wrong with your math is that you're not translating by the negation of your camera position; i.e., do glTranslatef(-x, 0, -z);

Share this post


Link to post
Share on other sites
Wavesonics    330
Wouldn't clearing the modleview just be loading the ident matrix since the modelview matrix is already selected? And I just added in the - to the translation, just figured out why u want that hehe.

I gtg right now but I'll b working on this l8er today, thx for ur help everyone!

Share this post


Link to post
Share on other sites
chawk    122
Yea that's what I meant, glLoadIdentity() with it. You don't want transformations from last frame affecting this frame's matrix and compounding together into a mess.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this