# Players x, y, z based on camera angle

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

## Recommended Posts

I have my model sitting there and I can move it on the x y and z axis, but than if I move my camera, the model still moves on the x y z axis, but not in context of the camera view. Sorry if that doesn't make sense, basically i want my camera to realign the x y z axis so that no matter where my camera is, hitting forward will move the object away from the camera, hitting right will move the object right ect... I thought there was a D3DX function that will do some math on my view matrix that does this but I cant seem to find it. Im sorry if I didn't describe my issue very well. Thanks!

##### Share on other sites
The camera matrix consists of 3 axis and a position. You can just take those axis (left, up, and forward ) and scale then add them to your objects position.

##### Share on other sites
Im sorry I don't fully understand what you mean. What does scaling have to do with anything? Also, by camera matrix do you mean my view matrix? I have a camera position vector, camera look vector, and up vector, than my view matrix is calculated uses those correct? What do you mean by left up and forward? Sorry If im missing something obvious =(.

##### Share on other sites
The camera matrix, is probably your view matrix, yes. The matrix, generated from the camera's forward and camera up will contain one more column (up cross forward) that is your left vector. So, you end up with the matrix:
[ left up forward pos ]
Just extract the columns and scale them by the speed you want to move the object by in each direction, and add those vectors to the object's position.

##### Share on other sites

The camera matrix, is probably your view matrix, yes. The matrix, generated from the camera's forward and camera up will contain one more column (up cross forward) that is your left vector. So, you end up with the matrix:
[ left up forward pos ]
Just extract the columns and scale them by the speed you want to move the object by in each direction, and add those vectors to the object's position.

@Kulseran: D3D would suggest a left handed system so I would use forward up right in this instance.

@OP: The view matrix is the inverse of the cameras world transform.

 Matrix4 cameraworld(x.x, x.y, x.z, 0, // Xaxis in world space y.x, y.y, y.z, 0 // Yaxis in world space z.x, z.y, z.z, 0, // Zaxis in world space T.x, T.y, T.z, 1); // Translation in world space 

[Because the inverse of a orthogonal matrix is also it's transpose., the XYZ vectors of camera local space are encoded into it's first 3 columns of the view transform.

 Vector3 XAXIS (view._11, view._21, view._31) // X axis of camera space in world space coordinates. 

What KulSeran mentioned was getting this vector, which is unit length, and scaling it by the amount you wish to translate your object. So if 5 units on Z axis you would grab the 3rd column of the view transform, multiply that by 5, and add it to your camera's position.

##### Share on other sites
I see. Im learning a lot more than just directx while working on this project. So far picked up some basic trig, and now I need to learn some matrice math. Thanks for the replies! This is exciting =)

##### Share on other sites
Sorry I wasn't able to work on this over the weekend, was busy. So anyways, I just have a few questions to better my understanding. First, the members of a D3DXMATRIX such as _11 _12 _13 _14, _21 _22 _23 _24 etc.... I am assuming this represents the row than column. So _34 would be position: Row 3, Column 4?

And my other question is what the different elements of the matrix represent. So in your example you comment row 4 as being the translation in world space. So _41 is the cameras X position, _42 is the cameras Y position and _43 is the cameras Z position in world space? World space being the 3d world correct? So what is _44 used for? Or is it just a fact that it is always going to be 1?

So you labeled row 1 as being the X-Axis, row 2 as the Y-Axis, and row 3 as the Z-Axis. But I don't understand how an x axis can have a y and z coordinate ect... Could you talk me through it better, or maybe provide some links? I cant seem to find any definitive answers. Thank you for your time.

##### Share on other sites

So what is _44 used for? Or is it just a fact that it is always going to be 1?

A matrix in OpenGL/DirectX represents an Affine Transformation in homogeneous coordinate space. All the "3d" vectors are treated as 4d. The "W" coordinate as it is called can be anything, but does have some common values. A position will usually have W=1 while a direction will have W=0. For some non-standard values, look at a projection matrix. Since the projection doesn't represent an orthonormal space, the W coordinates of each axis have specific values.

So you labeled row 1 as being the X-Axis, row 2 as the Y-Axis, and row 3 as the Z-Axis. But I don't understand how an x axis can have a y and z coordinate ect... Could you talk me through it better, or maybe provide some links? I cant seem to find any definitive answers. Thank you for your time.
[/quote]
Handed Coordinate systems. Hold out your hand as illustrated (left is DirectX i guess, while I'm used to the right handed OpenGL). But notice where your fingers are pointing. If you were to point forward, this would be your "global axis" that you are used to where X is (1,0,0). If you now point to the side, you have a matrix that describes a rotation. Your hand's X is now something like (0,0,1). If you had some object that was facing the same direction as you, and you multiplied by the matrix of your hand, it would now be pointing the same direction as your hand. So, the matrix of any object (even your camera) represents the orientation of that object, with a local X,Y, and Z axis that describe how the object is pointed in the world.

The basis of all these graphics transformations is Linear Algebra (not trig. forget trig. learn matrices and vectors).

##### Share on other sites
Ok guys, Im sorry again but I have been struggling with this for a while now, and I have realized why. Doing what you all described, works for a first person camera, where the look position is adjusted by moving the mouse ect... I am trying to implement this on a third person camera, where the camera's look vector is always the player coordinates. But I can't ever change the camera's look position with out moving the player along some axis, but to do that, I can't do the way you all described. I just realized this, so I am going to do some more research in the mean time to try and find if some equation out there exist based on my angle ect...

If you go here http://littlewingengine.tumblr.com/ and look at the first post, it is a video of what I have, and can do so far. You'll notice I can only move my object along its own x,y and z axis with no relation at all to the view of the camera.

Edit: Time passed = 15 minutes at the whiteboard.

So thinking through it I think I might have found a viable way to accomplish this, and I would like your opinion. It would basically be a 3 step process. Since I want the player to move away from the camera when going "forward" -

1) I should be able to take the angle of the camera on the y-axis(which I have), get the opposite degree, so if I were at 45o, the opposite would be 225o(just add 180) .
2) Find the (x , z) coordinates at the degree using x = (h + radius(cos(225))) and z = (k + radius(sin(225))). H and K being the player's current x and z coordinates.
3) Use the mid point formula to find a location on the line between the players position and the (x , z) coordinate at the angle of 225. Instead of using a denominator of 2 in the midpoint formula, it would be what ever I wanted to scale the movement of the player.

Does this seem like a sound way to accomplish this? Or is there a more simple way to go about it? I already have all of the elements needed to use an algorithm like this.

##### Share on other sites
What do you mean by the look vector? The camera direction?

The way I solve this problem is to take the direction vector of the camera, project it in the plane on which the player is allowed to move (most likely the xz, or xy plane), and normalize it*. You then have the movement direction. Multiply it with the player speed and time per frame, then add it to the player position.

*This won't work if the direction vector is perpendicular to the plane, because the projection will be zero length, and you can't normalize a zero length vector. If the length of the projected direction vector is 'almost zero', use the camera up vector instead.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633684
• Total Posts
3013316
×