SOLVED : Help, Please : Changing the origin of Rotation

Started by
4 comments, last by Nathanmini 17 years ago
I currently have a setup very similar to a first person shooter - with the environment rotating around the veiwport. This is not quite the setup i wanted - i am trying to create an RPG style camera with the 'origin of rotation' being a point a point in front of the viewport (where my character would be stood). Sorry if my explanation is not very good - im quite new to openGL, havent quite got the terminology yet. The up/down/left/right strafes the viewport forwards/backwards/left/right respectively. and the L & R buttons rotate the environment around the viewport. I have tried a few methods, i believe this way should work, but it doesnt: (lol) (BEFORE DRAWING EACH FRAME) 1. Translating the environment to the position where i would like the rotation to occur - just 'in front' of the viewport. 2. Rotating the environment to the correct rotation. 3. Translating the environment again to the position of the viewport in the environment - the location on the environment. If you dont understand i will try to explain it better - or maybe it would be better if i posted my code (i am programming for the Nintendo DS) Thanks adzz182 [Edited by - adzz182 on March 26, 2007 4:53:43 AM]
Advertisement
Think this way: Whenever a rotation is performed then the point at (0,0,0) will not change its position while every other point in space will (or at least can) change its position. This can be seen when performing the matrix-vector product of a rotation on paper. I.e. you want to have
R * p == p
where R is any rotation matrix and p a point in space. The formula says: The rotated point and the original point should be the same. This would happen if R is the identity matrix (i.e. _no_ rotation, but this isn't the case you want) or p == (0,0,0).

So you 1st have to translate the world so that the point you want not to rotate is rested at (0,0,0). Assume your point of interest (i.e. 'in front of the view') is given as (x,y,z). Then
glTranslate(x,y,z);glRotate(<whatever>);glTranslate(-x,-y,-z);

would do the trick. Remember that the later a transformation is given to GL, the more local it is applied to the vertices. The last transformation above is glTranslate(-x,-y,-z), so this is applied 1st to a vertex. A vertex that is exactly at that position is hence translated to (0,0,0) and the subsequent rotation doesn't change that. Then glTranslate(x,y,z) translates it back to the original position.


EDIT: Notice please that the set-up above is suitable as a view transformation in GL. If you want to rotate the camera as an object and later derive the view transformation from the camera settings, then you may want to transform your camera inverse compared to the code above.
Thanks for the quick reply haegarr.
Im still having trouble with this bit of code though....ill try to explain a little better. Here is my current code:

while (1) {  heading = (yrotation & 511);         //used for strafing forwards/backwards  heading90 = ((yrotation+128)&511);   //used for strafing left/right  if (keysHeld() & KEY_LEFT)           //if LEFT is Pressed  {    xpos -= SIN[heading90] >> 4;    zpos += COS[heading90] >> 4;  }  if (keysHeld() & KEY_RIGHT)          //if RIGHT is Pressed  {    xpos += SIN[heading90] >> 4;    zpos -= COS[heading90] >> 4;  }  if (keysHeld() & KEY_DOWN)           //if DOWN is pressed  {    xpos -= SIN[heading] >> 4;    zpos += COS[heading] >> 4;  }  if (keysHeld() & KEY_UP)             //if UP is pressed  {    xpos += SIN[heading] >> 4;    zpos -= COS[heading] >> 4;  }  if (keysHeld() & KEY_R)              //if R_TRIGGER is pressed  {    yrot+=ROTSPEED;  }  if(keysHeld() & KEY_L)               //if L_TRIGGER is pressed  {    yrot-=ROTSPEED;  }  int DrawGLScene()  {    glRotatef32i((512-yrot),0,(1<<12),0);    glTranslate3f32(-xpos,0.0f,-zpos);


This code does work, but not how i would like it to. Currently i have:
                 ^          |          |          -90 Degree Rotation->          |                    |          o       ViewPort                                 ViewPort o---------->What i am looking for is:          ^          |          |          -90 Degree Rotation->          o                                   ViewPort----o----->          |             |       ViewPorto = where rotation is taking place
I suspect that your problem may be solved by inserting a translation of (xpos, 0.0, zpos) before your rotation call, to undo the translation that you call for after that rotation, which should bring you to the situation described by haegarr.

I hope that this helps.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

Thanks alot for your replies, they did help. I have solved the problem now (i cant believe it took me so long - its sooooo simple lol). For anyone whos interested about how to do this here is the new source, which is now a sort of RPG style 'camera'.

The only difference if in the glTranslate
OLD:glTranslate3f32(-xpos,0.0f,-zpos);NEW:glTranslate3f32(-xpos+(SIN[heading]<<1),0.0f,-zpos-(COS[heading]<<1));
hey, i seem to be having the same problem as you had so if you could please post your display function code it would be much appreciated

This topic is closed to new replies.

Advertisement