Move in direction of camera - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html) +--- Thread: Move in direction of camera (/thread-7995.html) Move in direction of camera - MikeD - Aug 16, 2010 11:55 AM I've set up a simple camera that has a position x, y, z and a 4x4 rotation matrix that I apply so that I can look around my 3D scene. What I now want to do is move in the direction of the camera. I can't work out how to create a vector from my rotation matrix and then transform the position of the camera along that vector. My Vector and matrix maths is very rusty and I've not been able to make sense of the resources I've been finding on google. I'm hoping that one of the clever guys on the forum can talk me through this. Any sample code on offer would also greatly help my understanding. Thanks MikeD RE: Move in direction of camera - warmi - Aug 16, 2010 12:13 PM You need the initial facing vector of the camera which is where your camera is facing when you start out ( say, if you start out facing -Z then your vector would be Vector3(0,0,-1) -> it needs to be normalized) Once you have that then you need to transform this vector using your matrix4 and use it as your translation vector for the camera. This will only work if your matrix4 is used only for rotations ( and in that case you really only need a 3x3 matrix anyway) RE: Move in direction of camera - ThemsAllTook - Aug 17, 2010 06:27 AM I've written some tutorials that may be of some help to you: http://sacredsoftware.net/tutorials/Vectors/Vectors.xhtml http://sacredsoftware.net/tutorials/Quaternions/Quaternions.xhtml http://sacredsoftware.net/tutorials/Matrices/Matrices.xhtml If your camera has a 4x4 matrix, you could actually do without the separate xyz values and just express them as a translation in the matrix. Alternatively, you could use xyz and a quaternion instead of a matrix for the camera. In either case, as said above, you'd multiply an identity orientation vector by your camera's orientation and offset your camera's position by the result, multiplied by the distance you want to move in that direction. RE: Move in direction of camera - MikeD - Aug 18, 2010 07:33 AM Thanks for the responses guys. I've got the camera working, but I can't say I understand why. My current render method is: Code: ```- (void)render {     glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);          static GLfloat transY = 0;          // Set the clear color and clear the screen     glClearColor(0.7, 0.7, 0.7, 1.0);     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);          // Reset the modelview matrix with the identity matrix     glLoadIdentity();          // Grab the current attitude from core motion     attitude = motionManager.deviceMotion.attitude;     CMRotationMatrix rm = attitude.rotationMatrix;          // Create a 4x4 matrix that rotates by the inverse (=transpose) of attitude.rotationMatrix     GLfloat rotMatrix[] =     {         rm.m11, rm.m21, rm.m31, 0,         rm.m12, rm.m22, rm.m32, 0,         rm.m13, rm.m23, rm.m33, 0,         0,      0,      0,      1     };          // Apply the matrix to the modelview     glMultMatrixf(rotMatrix);          // To correctly orient the view, rotate the view 90 degrees about X     glRotatef(90, 1, 0, 0);     // If the player is moving, grab the current modelview matrix that has been calculated     // based on the rotation matrix applied.     if (movingForward || movingBackwards)     {         glGetFloatv(GL_MODELVIEW_MATRIX, modelViewMatrix);     }          // Using the modelViewMatrix, check the direction being travelled and adjust the position     // of the camera based on the modelview matrix     if(movingForward) {         cameraPos.x += 0.0 * modelViewMatrix[0] + 0.0 * modelViewMatrix[1] + 0.2 * modelViewMatrix[2];         cameraPos.y += 0.0 * modelViewMatrix[4] + 0.0 * modelViewMatrix[5] + 0.2 * modelViewMatrix[6];         cameraPos.z += 0.0 * modelViewMatrix[8] + 0.0 * modelViewMatrix[9] + 0.2 * modelViewMatrix[10];     }     if(movingBackwards) {         cameraPos.x += 0.0 * modelViewMatrix[0] + 0.0 * modelViewMatrix[1] - 0.2 * modelViewMatrix[2];         cameraPos.y += 0.0 * modelViewMatrix[4] + 0.0 * modelViewMatrix[5] - 0.2 * modelViewMatrix[6];         cameraPos.z += 0.0 * modelViewMatrix[8] + 0.0 * modelViewMatrix[9] - 0.2 * modelViewMatrix[10];     }          // Move to the cameras position     glTranslatef(cameraPos.x, cameraPos.y, cameraPos.z);     // Render the scene     int n = 4;          switch (modelMode) {         case 0:             for(int x=-n; x<=n; x+=2)             {                 for(int y=-n; y<=n; y+=2)                 {                     for(int z=-n; z