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

  • From: Vince <uberneen@xxxxxxxxx>
  • To: gameprogrammer@xxxxxxxxxxxxx
  • Date: Tue, 18 Mar 2008 10:20:11 -0700 (PDT)

--- 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);

So my display function is abstractly

init();
camera();
draw();

From what I've read, this is a pretty common FPS
camera, except when moving forward, you move along the
vector that you're looking down, instead of moving
forward along terrain while looking up.

If your range of movement is FPS style, along some
kind of terrain, this should do what you want, but if
it's more like a flight or space simulator, you'll run
into Gimbal lock, and that's where you could
potentially use quaternions.

My knowledge of quaternions isn't that great, which is
why I haven't implemented them yet.  Like you, I want
to know more about what I'm doing with them rather
than copy and paste, but I figure dissecting an
existing class is a good place to begin.

Vince~


      
____________________________________________________________________________________
Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs

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


Other related posts: