![]() |
|
rotate around own axis - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: Game Programming Fundamentals (/forum-7.html) +--- Thread: rotate around own axis (/thread-2371.html) |
rotate around own axis - wonza - Sep 24, 2008 10:12 AM Hi there, Im trying to create an space shooter style game in OpenGL/objectC. Ive not done much opengl before so you'll have to forgive me. Ive created a virtual camera where i move objects in the universe around the central point to make it appear im moving. ive also got it working so that i move in the direction that im pointing. however, say if i move upwards and then look down and try to rotate around the y axis the object in front of me kinda wobbles. I think this is because its rotating me around the real y axis rather than the y axis of where i am now facing. Is there any easy way to overcome this problem? Many thanks! rotate around own axis - ThemsAllTook - Sep 24, 2008 12:07 PM 3D rotations are a pretty complex subject. The answer will depend on how you're representing/applying your rotations. Are you keeping a quaternion or a matrix that accumulates rotations as you move your camera, or doing something else? These may be helpful: http://www.sacredsoftware.net/tutorials/Quaternions/Quaternions.xhtml http://www.sacredsoftware.net/tutorials/Matrices/Matrices.xhtml rotate around own axis - AnotherJake - Sep 24, 2008 07:04 PM Yes, sounds like you need to use quaternions. If you already are, don't forget to normalize them regularly. rotate around own axis - wonza - Sep 25, 2008 08:15 AM i think thats exactly what i need, thanks! those tutorials were great too. i was using individual float variables for the x, y, z position and the x,y,z angles too. I think I just about understand vectors and quaternions now, which is great. Im just not sure how to apply this to my code. Ie how to do the equivenlent of what im doing now which is: glRotatef(vax, 1.0f, 0.0f, 0.0f); glRotatef(vay, 0.0f, 1.0f, 0.0f); rotate around own axis - AnotherJake - Sep 25, 2008 08:28 AM There are different approaches. Here's one: Code: GLfloat angleAxis[4];You can also get a matrix from your quat and do a glMultMatrix. [EDIT] Here's my AngleAxisFromQuat function: Code: #define RAD_TO_DEG 57.295779513082frotate around own axis - wonza - Sep 27, 2008 07:50 AM thanks for your replies. I think Im still getting pretty confused about this (sorry for being dumb). It might help to show you the code im using: if (thrust > 0.0f) { vsx +=cos((vay+275)/RAD_TO_DEG); vsy +=cos((vax+90)/RAD_TO_DEG); vsz +=cos((vay+180)/RAD_TO_DEG); } viewPosition.x+=vsx; viewPosition.y+=vsy; viewPosition.z+=vsz; glRotatef(vax, 1.0f, 0.0f, 0.0f); glRotatef(vay, 0.0f, 1.0f, 0.0f); glRotatef(vaz, 0.0f, 0.0f, 1.0f); glTranslatef(-viewPosition.x, -viewPosition.y, -viewPosition.z); My angles are changed based on where i click in the screen. From the tutorials, I think I need to create a direction vector about which to rotate and then get the quaternion from this, and then get the x,y,z from this to then pass into the glRotate function. If Im right about any of that, then how should i work out the direction vector to rotate around from the 3 angles i have? Also, what angle should I be passing into the quaternion function? or would that be the difference in angles of where i was pointing before to where im wanting to point now? Thanks for your patience!! rotate around own axis - AnotherJake - Sep 27, 2008 08:25 AM Sounds like you want to do something similar to Apple's Cocoa OpenGL sample. In particular, check out trackball.c. That sample uses quaternions to rotate a cube based on where the user clicks and drags on the screen. BTW, instead of dividing by RAD_TO_DEG, you should multiply by DEG_TO_RAD, so it's less confusing, and possibly a little faster doing a multiply instead of divide (I think). #define DEG_TO_RAD 0.0174532925199f cos((vay+275) * DEG_TO_RAD); rotate around own axis - wonza - Sep 27, 2008 08:46 AM thanks, ive just had a look at that app, and it seems to be using gluLookAt, which would be ideal, but im using opengl es on the iPhone which doesnt have that function.
rotate around own axis - ThemsAllTook - Sep 27, 2008 08:51 AM Ah, it looks like you've fallen into the same pitfall almost everyone does when they're first learning about this stuff. Euler angles (using one rotation angle for X, one for Y, and one for Z) basically don't work no matter what you do. From an e-mail exchange I had a while back trying to explain this to someone: Quote:I understand your confusion. This is exactly the same difficulty I ran into when trying to understand the difference between rotations with quaternions or matrices and rotations with Euler angles. The main difference between doing them one at a time and doing all three at once is the order of operations. Let's see if I can demonstrate with a simple example: As for gluLookAt, you could substitute something like this, where you'd be keeping an orientation quaternion and a position vector (see my tutorial for a Quaternion_toMatrix implementaton): Code: Matrix matrix;rotate around own axis - AnotherJake - Sep 27, 2008 09:18 AM wonza Wrote:thanks, ive just had a look at that app, and it seems to be using gluLookAt, which would be ideal, but im using opengl es on the iPhone which doesnt have that function. It's just a UVN rotate and translate. I constructed (er, borrowed) this from the Mesa source. (I haven't tested it much (definitely not on iPhone yet), but it is known to have worked for me in the past). Code: void normalize(GLfloat *v)rotate around own axis - wonza - Sep 27, 2008 10:04 AM The custom gluLookAt function seemed to work great thanks! Ive realised Im still going to need the quat functionality though, as ill need to rotate other ships around their own axis too. I've tried the Quaternion_toMatrix(orientation) but seem to be getting a strange compilation error: "incompatible types in assignment", Im passing in a Quaternion and that should be what its expecting. Its annoying when silly errors get in the way. rotate around own axis - AnotherJake - Sep 27, 2008 10:36 AM It would be a good idea to completely tear apart trackball.c to fully understand how they're using quaternions there. The reason I mention this is that if you do, they don't use matrices, but rather angle axis conversions and glRotatef to get to and from quaternions. It is a very simple and straight-forward way to use quaternions. I don't know if this will add to your confusion or not, but here's some pseudo-code for one way to implement quaternion rotations. It appears to be similar to how they're doing it in trackball.c: Code: GLfloat myQuat[4];Here are the remaining functions (I already listed AngleAxisFromQuat earlier in this thread): Code: void EulerToQuat(GLfloat pitch, GLfloat yaw, GLfloat roll, GLfloat *quat)rotate around own axis - ThemsAllTook - Sep 27, 2008 10:55 AM wonza Wrote:I've tried the Quaternion_toMatrix(orientation) but seem to be getting a strange compilation error: "incompatible types in assignment", Im passing in a Quaternion and that should be what its expecting. Its annoying when silly errors get in the way. That's odd. Well, since it's saying there are incompatible types in assignment, the problem is more likely what you're doing with the function's return value than its parameter. As defined in my example code, it takes one of these: Code: struct Quaternion {Code: struct Matrix {rotate around own axis - wonza - Sep 27, 2008 11:03 AM its strange, i just moved the Quaternion_toMatrix function before my draw function and it worked. Thanks both again for all your help, I'll let you know how I got on. rotate around own axis - AnotherJake - Sep 27, 2008 11:05 AM Sounds like you didn't list the function declaration. |