• Advertisement
Sign in to follow this  

Move Object Based on Camera's Direction

This topic is 1964 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
http://www.gamedev.net/topic/630523-strategy-camera/

look here, matWorld is your position

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
A view matrix is the [i]inverse[/i] 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement