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` At the moment I'm grabbing the rotationMatrix from CoreMotion, transposing it and then multiplying it with the modelview matrix. I then rotate 90 degrees around X so that the orientation on screen matches what I expect i.e. the floor I have created is on the floor and is not a wall behind the cameras start position (0, 0, 0). I've played with all sorts of different methods and the only one I can get to work is shown above, where I take the modelview matrix from OpenGL and adjust the cameras position using that. I'm not sure why I have to grab the modelview matrix and not just use the rotationMatrix I got from CoreMotion, as apart from the 90 degree rotation I do around X, the rotationMatrix should match the modelview matrix!!!!! This works fine, but I'm trying to understand why it works and why I have to grab the modelView which has the potential to kill performance. ThemsAllTook, thanks for the tutorials. They are the first ones that have actually made sense to me, especially the one about Quaternions. CoreMotion provides a Quaternion as well as a rotationMatrix, but I've not tried to use that yet. Confused! Mike |