#### Archived

This topic is now archived and is closed to further replies.

# turning camera

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

## Recommended Posts

im trying to learn OpenGL and 3dimensional theory. and also trying to write my own code for this stuff as much as possible. now i have a function that will rotate the camera so that you can look up and down, but i have a problem with turning left and right rotation. currently my code is this for the turn.
float DEG2RAD(float x) {
return (x*(PI/180));
}

typedef struct {
double x;
double y;
double z;
} vector;

class camera {
public:
camera();
void move(float speed);
void stafe(int speed);
void setpos(int x, int y, int z);
void setlook(int x, int y, int z);
void look(float degree);
void turn(float degree);
void display();
int setmaxup(int degree);
int maxup();
float lookangle;
float clookangle;
float turnangle;
float cturnangle;
private:
vector position;
vector lookat;
int maxlook;
};

void camera::turn(float degree) {
cturnangle+=degree;

if(cturnangle >= 360.0f) {
cturnangle-=360.0f;
}
if(cturnangle <= 360.0f) {
cturnangle+=360.0f;
}

lookat.x=(position.x + xx);
lookat.z=(position.z + zz);
};

can anyone tell me what is wrong with this function. If i rotate it at ANY degree anything that was on the screen is lost, so what can I do about that. the source code included has other functions, i just put the relavent information, or what i thought was relavent. Higher Forces [edited by - hogosha on September 2, 2003 10:23:59 PM]

##### Share on other sites
Lemme see here...
quote:
Original post by hogosha
    if(cturnangle <= 360.0f) {        cturnangle+=360.0f;    }

Shouldn't that be like this:
if (cturnangle < 0.0f)    cturnangle += 360.0f;

Otherwise you're going to have angles greater than 360, which I don't think you want. I don't think this has anything to do with your problem, so lemme keep looking...
quote:
           float xx=cos(DEG2RAD(cturnangle));    float zz=sin(DEG2RAD(cturnangle));    lookat.x=(position.x + xx);    lookat.z=(position.z + zz);};

What's the ; at the end of the function for?

I don't really see anything. What's your call to gluLookAt() look like, if that's what you're using? And is lookat.y initialized to anything? If it's something funky it may be the problem.

Edit: Wow, those source windows are overkill sometimes...

[edited by - BradDaBug on September 2, 2003 10:41:03 PM]

##### Share on other sites
Here's what I use:

position.x = view.x + distance*sinf(rotationY)*sinf(rotationX);position.z = view.z + distance*cosf(rotationY)*sinf(rotationX);position.y = view.y + distance*cosf(rotationX);
It's just spherical polar coordinates. Look it up on google if you want but I haven't found a good explanation that doesn't scare me away with heavy notation. I'm not sure about the exact math, but whatever . I will learn it in the math class I'm taking this year. My up vector is y, so if your's is z you will have to change the two values around. RotationX is your up/down rotation, RotationY is your left/right rotation, and distance is the distance you want the camera to be from the viewpoint. Good luck!

[edited by - Raloth on September 2, 2003 11:02:55 PM]

##### Share on other sites
1.float DEG2RAD(float x) { return (x*(PI/180));}

the Slowest thing i have every Seen!, Kid you wanna be a Carmack and wanna have speed, try this one:

#define PI 3.1415265359
#define PI_OVER_180 0.0174532925f

if you have a working Engine your PC Calculates over 100 Times in Second the Same thing, not really Efficient.

// Could be Faster

if (FreeLook)
{
glRotatef(Xrot , 1.0 , 0.0 , 0.0 );
glRotatef(Yrot , 0.0 , 1.0 , 0.0 );
glRotatef(Zrot , 0.0 , 0.0 , 1.0 );
glTranslatef( -position.x , -position.y , -position.z );
}
else
{
// Camera looking @ Object
Vector3f view = Get3DVector( &LookAt , &position );
Normalize3DVector( &view );

Vector2f XZ;
GLfloat c;
c = (float)1/sqrt( view.x * view.x + view.z * view.z );
XZ.x = (float)view.x * c;
XZ.y = (float)view.z * c;

Xrot = -asin( view.y ) * RADtoDEG;
Yrot = RADtoDEG * atan2( XZ.y , XZ.x ) + 90;
Zrot = 0.0;

glRotatef(Xrot , 1.0 , 0.0 , 0.0 );
glRotatef(Yrot , 0.0 , 1.0 , 0.0 );
glRotatef(Zrot , 0.0 , 0.0 , 1.0 );
glTranslatef( -position.x , -position.y , -position.z );
}

void CCamera::RotateVertical( GLfloat fDegrees )
{
if (!FreeLook)
return;

Yrot -= fDegrees;
// Could be faster if Using Integers, because no fucking
// pig would checkout the Difference
if (Yrot > 360.0)
Yrot -= 360.0;

if (Yrot < 0.0)
Yrot += 360.0;
}

void CCamera::MoveForward( GLfloat speed )
{
if (!FreeLook)
return;

Vector3f Direction;

// slow, really slow... but works fine
Direction.x = -sin(Yrot*PI_OVER_180) * cos (Xrot * PI_OVER_180);
Direction.y = sin(Xrot*PI_OVER_180);
Direction.z = cos(Yrot*PI_OVER_180) * cos (Xrot * PI_OVER_180);

// Speed Up, ...
Multi3DVector( &Direction , speed );

position.x -= Direction.x;
position.y -= Direction.y;
position.z -= Direction.z;
}

=============================

Any Questions?

[edited by - Jankey on September 3, 2003 11:25:37 AM]

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

• 14
• 29
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002290
×