Move Object Based on Camera's Direction

Started by
3 comments, last by DJTN 11 years, 7 months ago
I have an object that I would usually move by adding to its position vector. If I wanted to move it forward, I’d add to its position vector’s Z value. If I want to move it backward, I’d “subtract” from its Z value and so on. I'm in a situation now were I need to move the object forward based on the camera’s looking direction. If the camera is looking left then I need to subtract from the objects position vector’s X value, etc...

To calculate the position, I thought I’d simply put the position of the object into a new vector3, add to it’s z value like before, and then transform that vector by the view matrix and then set the object's position vector to the new vector3- however, this is not working as expected.


How can I transform my move (position) vector based off the camera’s looking direction?
Advertisement
http://www.gamedev.net/topic/630523-strategy-camera/

look here, matWorld is your position
Subtract the position of the camera from the position of the object. Then normalize that vector.

This is the direction in which you should move your object (backward - negate to move forward).

Just multiply the vector by the world distance you want to move the object, then add it to the object's position.
A view matrix is the inverse of your camera's transformation matrix. A transformation matrix takes you from an object's local space to world space, while a view matrix takes you from world space to the camera's local space. If you invert the view matrix to get the transformation matrix, the camera's forward direction will be the third row of the matrix (the Z basis). For a view matrix you can also just transpose instead of inverting and grab the third row, which is equivalent to grabbing the _13, _23, and _33 components from the view matrix. Once you have the forward direction, you can just do position += forwardDir * moveAmt.
Thanks MJ

This topic is closed to new replies.

Advertisement