Archived

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

glRotatef Origin

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Howzit Is there a way that I can set the origin for glRotatef. What I want to do is rotate everything around my character. My character''s position constantly changes. Shot

Share this post


Link to post
Share on other sites
My character is always at the origin. Just to clear up some confusion. My character moves similar to the character in Neverwinter Nights. When I move, I move the camera. If I rotate and the camera isn''t at the origin, my character looks like it''s flying.

Basically what I want to do is take the origin that glRotatef uses and place it at the origin of my character.

Share this post


Link to post
Share on other sites

1. glTranslatef(-point_about_which_you_want_to_rotate.x,
-point_about_which_you_want_to_rotate.y,
-point_about_which_you_want_to_rotate.z);

2. glRotatef( angle_you_want_to_rotate_in_degrees,
rotation_axis_about_which_you_want_to_rotate.x,
rotation_axis_about_which_you_want_to_rotate.y
rotation_axis_about_which_you_want_to_rotate.z);

3. glTranslatef(point_about_which_you_want_to_rotate.x,
point_about_which_you_want_to_rotate.y,
point_about_which_you_want_to_rotate.z);


[edited by - benjamin bunny on March 19, 2004 2:58:18 AM]

Share this post


Link to post
Share on other sites
It sounds like what you want is for your character to always be at the origin. That way he and the camera always rotate about the same point. You probably don't want to move your character in space, just move your terrain around him.

-griffon

[edited by - griffon on March 19, 2004 3:00:24 AM]

Share this post


Link to post
Share on other sites
Actually, ignore what I just posted; It was the product of too many hour awake and not enough caffiene . While I did tell you how to rotate about an arbitrary point, I don't think that's what you need.

quote:

My character is always at the origin. Just to clear up some confusion. My character moves similar to the character in Neverwinter Nights. When I move, I move the camera. If I rotate and the camera isn't at the origin, my character looks like it's flying.

Basically what I want to do is take the origin that glRotatef uses and place it at the origin of my character.

First off, glRotate always rotates about the origin.

Secondly, if you've done it properly, your character's position should not be at the origin, at least in world coordinates (world coordinates is the coordinate system before transformation by the modelview matrix).

Bearing that in mind, here's how I setup an orbital camera modelview matrix in my code:

glLoadIdentity();
glTranslatef(0,0,-radius);
glRotatef(pitch,1,0,0);
glRotatef(yaw,0,1,0);
glTranslatef(-centerPos.x,-centerPos.y,-centerPos.z);

centerPos is the point about which the camera rotates (in your program it'll be your character's position in world coordinates). Radius is the distance of the camera from point, and pitch and yaw and are the angles.

[edited by - benjamin bunny on March 19, 2004 3:19:09 AM]

Share this post


Link to post
Share on other sites
I posted this problem yesterday "moving around", but i have worked it out. You use some trigonometry i found in a tutorial. Mine is first person view, but 3rd person should be a similar concept.
Here is some code, though it is not complete as i need to calculate a bearing.


void calcTranslation(input_t *input)
{
static movement_t position; //Assess the need for a structure here

float heading = 45.0; //We need a calculate this value

if (input->bLeft == 1)
{
position.xRot = position.xRot - 0.5;
}
if (input->bRight == 1)
{
position.xRot = position.xRot + 0.5;
}
if (input->bUp == 1) //We need to translate in X and Z

{
position.xPos = position.xPos-((float)sin(heading*(PI/180))*0.05f);
position.zPos = position.zPos-((float)cos(heading*(PI/180))*0.05f);
}
if (input->bDown == 1) //Same deal

{
position.xPos = position.xPos+((float)sin(heading*(PI/180)) * 0.05f);
position.zPos = position.zPos+((float)cos(heading*(PI/180)) * 0.05f);
}
glLoadIdentity(); //Load identity matrix

//glRotate must ceom before translate

glRotatef(position.xRot,0.0,1.0,0.0);
glTranslatef(-position.xPos, 0.0, -position.zPos);
return;
}


What we are doing here is rotating around a fixed origin. This however rotates the local cord system of your world, so when you press forward (translate) you dont move forward...you head in the direction that the world has been rotated. This is why for player walking forwards/backwards we have to calculate what forward is reletive to the X and Z axis...it isnt just along the Z axis once you have rotated.

You really don need to undetstand tranlsations and rotataions in OpenGL, and how things are different from the world coordinate system perspective and the local coord perspective. The order of glRotate and glTranlslate also completely changes things, and again is different depending on how you view your axis (local or world).

Its tricky, but you need to understand what is going on.

As OpenGL is a state machine, nothing needs to be returned from this function

[edited by - Mr Lane on March 19, 2004 6:46:11 AM]

[edited by - Mr Lane on March 19, 2004 6:47:32 AM]

Share this post


Link to post
Share on other sites