• Create Account

## Move Object Based on Camera's Direction

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1DJTN  Members

207
Like
0Likes
Like

Posted 03 September 2012 - 11:18 AM

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?

### #2lyzerk  Members

120
Like
0Likes
Like

Posted 03 September 2012 - 11:38 AM

http://www.gamedev.net/topic/630523-strategy-camera/

look here, matWorld is your position

### #3phil_t  Members

7653
Like
0Likes
Like

Posted 03 September 2012 - 11:43 AM

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.

### #4MJP  Moderators

18236
Like
1Likes
Like

Posted 03 September 2012 - 11:44 AM

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.

### #5DJTN  Members

207
Like
0Likes
Like

Posted 03 September 2012 - 11:49 AM

Thanks MJ

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.