[gameprogrammer] Re: Rotation, Rotation, Rotation...

  • From: "richard sabbarton" <richard.sabbarton@xxxxxxxxx>
  • To: gameprogrammer@xxxxxxxxxxxxx
  • Date: Thu, 20 Mar 2008 11:26:14 +0000

Hey guys,

I think that I have finally cracked it!  After some foolin' around
with matrices I found that you could use an Inverse of a matrix to
perform the oppisite of what the matrix does.  So you can multiply the
current matrix by its Inverse, perform your rotation and then Multiply
it by the original matrix.  Then it occured to me that, because this
is a camera it is the first operation I am performing on the modelview
matrix.  Which means that I don't need to calculate the inverse or
multiply by it because I know that the result will be equal to the
Identity.

I set up a simple Camera Class that has a float[16] matrix as a member
that initialises as a 4x4 Identity Matrix and then when I want to
rotate my camera I do the following:

1. Load an Identity Matrix into a NEW matrix
2. Rotate the NEW Identity Matrix by my required rotation ammounts
3. Multiply this NEW matrix by my STORED camera matrix member
4. Save the NEW matrix into my STORED matrix for next time I need to
move the camera.

If, at step two, I rotate around X the camera appears to move left and
right on-screen even if the camera is looking straight down.

Richard


On 18/03/2008, Vince <uberneen@xxxxxxxxx> wrote:
> Your method sounds plausible to me, and it immediately
> makes more sense than trying to figure out what is
> actually going on with quaternion math.
>
> I may toy around with it in my test framework as I get
> time.  Good luck!
>
> Vince~
>
> --- richard sabbarton <richard.sabbarton@xxxxxxxxx>
> wrote:
>
> > Hi Vince,
> >
> > This is exactly the issue.  I want x and y movement
> > of the mouse to
> > control rotation in free 360 deg on all axis.
> > Gimbal Lock is an issue
> > and following some additional reading I think I can
> > do it.  Also, I
> > think I can stop short of using Quarternians to do
> > it.
> >
> > My plan is this...
> >
> > Define my camera orientation as two vectors,
> > Direction (vD) and Up
> > (vU), with a 3rd vector at 0,0,0 (vO) for the
> > origin.  I can then use
> > gluLookAt to set the camera.
> >
> > With these 3 vectors I can rotate my camera in any
> > way without
> > suffering from gimbal lock.
> >
> > For Left/Right rotation I can rotate vD around the
> > axis defined by vU.
> >  Roll rotation I can rotate vU around the axis
> > defined by vD.  For
> > up/down rotation I will rotate vD around the Cross
> > Product of the
> > vectors vU vD.
> >
> > I haven't done this yet but I think it works in
> > theory.  What I need
> > to do is put together an efficient routing to rotate
> > a vector around
> > an arbitrary axis.  The glRotatef() function does
> > eactly this but my
> > problem is that I am going to need the vectors so I
> > think I will need
> > to code this myself.
> >
> > Richard
> >
> >
> > On 18/03/2008, Vince <uberneen@xxxxxxxxx> wrote:
> > > --- richard sabbarton
> > <richard.sabbarton@xxxxxxxxx>
> > > wrote:
> > >
> > > > Hi Robbert,
> > > >
> > > > I have been looking into this a bit further and
> > I
> > > > think I can describe
> > > > the problem a little better. (maybe)...
> > > >
> > > > OK.  What I want is to be able to rotate my
> > Player
> > > > view around my
> > > > players axis and not the global axis.  So, if my
> > > > player starts out, as
> > > > is default, looking down the Z Axis and then
> > looks
> > > > down I want my
> > > > players Z axis to point down so that a rotation
> > > > around my players axis
> > > > are relative to the player and not the global
> > axis.
> > > >
> > > > Lets say my control keys are up,down,left and
> > right.
> > > >  If my player
> > > > pushes down by 90 deg and then pushes left by 90
> > deg
> > > > I want my player
> > > > to now be on its side looking 90 deg to the left
> > of
> > > > its original
> > > > position.
> > > >
> > > > Currently, pushing down for 90 deg looks down
> > but
> > > > then the left action
> > > > rotates the view and I am still looking down.
> > > >
> > > > I read the Quaternion Camera tutorial that Vince
> > > > sent through (Thanks
> > > > Vince) but I don't think I really understood it.
> >  It
> > > > has
> > > > copy/paste'able code that I think would do
> > exactly
> > > > what I want but I
> > > > would prefer to do something I understand.
> > Also, I
> > > > will need the same
> > > > functionality for objects in my world as well
> > and I
> > > > am not sure, given
> > > > the calculations involved, that this is going to
> > run
> > > > quick enough.
> > > >
> > > > I have been looking into rotation matrices and
> > > > transforms as well.
> > > >
> > > > One thing has certainly become clear... My Math
> > is
> > > > letting me down.  I
> > > > have a feeling the answer to my problem lies in
> > a
> > > > trip to the bookshop
> > > > for some Math Texts and a large supply of sugary
> > > > snacks and caffeine
> > > > based drinks.
> > > >
> > > > The thing is, there are tons of tutorials out
> > there
> > > > that explain how
> > > > to do this.  The problem is that they all seem
> > to
> > > > assume a certain
> > > > ammount of knowledge on the subject before they
> > > > begin.  Again, back to
> > > > the Math Texts...
> > > >
> > > > Richard
> > >
> > >
> > > This is the FPS camera that I've implemented in my
> > > testing framework.
> > >
> > > I track xpos, ypos, zpos, xrot, yrot, and zrot.
> > > I also have storage for lastx and lasty, but that
> > > applies to mouse movement so it's not absolutely
> > > necessary.
> > >
> > > This is just my sloppy testing framework, so I
> > don't
> > > do things like compute for timescale.  You might
> > > normally replace the multiplication by 0.5 to a
> > value
> > > determined by your frame loop.
> > >
> > > When you press
> > > 'Forward'
> > >
> > > yrotrad = (yrot / 180 * PI);
> > > xrotrad = (xrot / 180 * PI);
> > > xpos += (float) sin(yrotrad) * 0.5;
> > > zpos -= (float) cos(yrotrad) * 0.5;
> > > ypos -= (float) sin(xrotrad) * 0.5;
> > >
> > > 'Backward'
> > >
> > > yrotrad = (yrot / 180 * PI);
> > > xrotrad = (xrot / 180 * PI);
> > > xpos -= (float) sin(yrotrad) * 0.5;
> > > zpos += (float) cos(yrotrad) * 0.5;
> > > ypos += (float) sin(xrotrad) * 0.5;
> > >
> > > 'Left'
> > >
> > > yrotrad = (yrot / 180 * PI);
> > > xpos += (float) cos(yrotrad) * 0.5;
> > > zpos += (float) sin(yrotrad) * 0.5;
> > >
> > > 'Right'
> > >
> > > yrotrad = (yrot / 180 * PI);
> > > xpos -= (float) cos(yrotrad) * 0.5;
> > > zpos -= (float) sin(yrotrad) * 0.5;
> > >
> > > I handle rotation through the mouse, but this
> > could be
> > > handled any way that you want.
> > > My function takes x and y, which represent the
> > current
> > > mouse position.  Since I'm using GLFW, it's not
> > > constrained to the screen size.
> > >
> > > diffx=x-lastx;
> > > diffy=y-lasty;
> > > lastx=x;
> > > lasty=y;
> > > xrot += (float) diffy * 0.2;
> > > yrot += (float) diffx * 0.2;
> > >
> > > if(xrot > 360) { xrot -= 360; }
> > > if(xrot < 0) { xrot += 360; }
> > >
> > > if(yrot > 360) { yrot -= 360; }
> > > if(yrot < 0) { yrot += 360; }
> > >
> > > When I draw, I first translate to the player
> > position
> > > using a camera() function.
> > >
> > > glRotatef(xrot,1.0,0.0,0.0);
> > > glRotatef(yrot,0.0,1.0,0.0);
> > > glTranslated(-xpos,-ypos,-zpos);
> >
> === message truncated ===
>
>
>
>      
> ____________________________________________________________________________________
> Be a better friend, newshound, and
> know-it-all with Yahoo! Mobile.  Try it now.  
> http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
>
>
> ---------------------
> To unsubscribe go to http://gameprogrammer.com/mailinglist.html
>
>
>

---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: