glRotatef Origin

Started by
8 comments, last by Mr_Ridd 20 years, 1 month ago
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
The ability to succeed is the ability to adapt
Advertisement
1. translate your character to the origin (translate(-characterPos))
2. rotate
3. reverse 1.

You might find it useful to read up on transformations before you venture further into this stuff.

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

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.
The ability to succeed is the ability to adapt
Substitute the point about which you want to rotate for characterPos then.

[edited by - benjamin bunny on March 19, 2004 2:34:00 AM]

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

How do I do that?

I''m using glRotatef.
The ability to succeed is the ability to adapt
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]

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

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]
Spot on griffon!
The ability to succeed is the ability to adapt
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]

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

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]

This topic is closed to new replies.

Advertisement