### Scaling a D3DXVECTOR3

Hi Guys,

I hope this is the correct part of the forums to post in, anyway here goes...

I am in the process of making a basic 3d board game using directX. I am using D3DXVECTOR3's for positions in my game world. I am currently trying to implement movement for my player where when i press a key (W), the the goal position of the player is set to a place in front of the the player. Since i am using a timestep, i want the a scaled down version of the goalPosition vector to be added on to the player position vector each frame until the goalPosition and playerPosition are equal. I am quite confident that i have the basic logic down, however i am bad with vector math and im not quite sure how to obtain a scaled down version of any given vector.

It is kind of late and i have been looking at this for hours, so i hope my question makes sense, if not i am happy to clarify or provide snippets of code if needed.

Any help is much appreciated.
Last edited on
There's probably someone who has a better math way of doing this but you could use D3DXVec3Normalize to change the vector pointing to the goal into a unit length, and then just multiply that vector by your elapsed time, or whatever scaling factor you want to use and move the player by that amount each frame.
For a uniform scale of a vector's length/magnitude, you would have to multiply each component with the scaling factor.

 123456789101112 D3DXVECTOR3 vec1(1.0f, 2.0f, 3.0f); float scalingFactor; //... //determine your scaling factor based on the elapsed time //suppose scalingFactor = 2.0f; //... vec1.x *= scalingFactor; // vec1.x * 2 = 2.0f; vec1.y *= scalingFactor; // vec1.y * 2 = 4.0f; vec1.z *= scalingFactor; // vec1.z * 2 = 6.0f;

So this would uniformly scale vec1 in all 3 dimensions, retaining direction (towards goalPosition) but scaling it's length / magnitude (doubling it, in the above example).

It's important to make sure you use the appropriate coordinate systems though. A vector can represent a 3D location in the coordinate system where it's initial point coincides with the origin for that coordinate system and the endpoint coincides with the vector's coordinate values.

So if we assume that the player position and the goal position are both expressed in world coordinates, a scaled down version of the goalPosition vector would not be the appropriate vector for adding to the player position (because the vector you want has it's initial point in the player position and it's terminal point in the goal position, while the goalPosition vector, if specified in world coordinates, has it's initial point in the world origin and it's terminal point in the goal position).

So you would probably have to do some coordinate system conversion as well, if all your values are in world coordinates. Usually, for 3D movement of a character, you would want to specify your movement in the character's local coordinate system (like in the direction of a cardinal half-axis, say +Z) and then convert to world coordinates per-frame to get the actual movement vector for that frame. By doing that, you would press "forward" on your keyboard (say, W) and depending on your character's orientation (specified by the rotation matrix for your character) the "forward according to character orientation" would be converted to world coordinates.
Last edited on
Thanks, that makes a lot of sense. Would i be right in assuming that the D3DXVec3Scale function does the same thing?

I should also point out that since this is a 3d board game, i dont actually use the y coordinate as i only want to move forward/back and side to side. All of this is done in the world space.

I am still having trouble but i am sure it is now something to do with the timestep and my other logic as my player stops moving when the condition of goalPosition and playerPosition being the same is met but then the player also goes on for a little bit extra.

Thanks again.
Yes, D3DXVec3Scale does the same thing, it uniformly scales a D3DXVECTOR3. So an equivalent for my code above would be:

 12345 D3DXVECTOR3 vec1(1.0f, 2.0f, 3.0f); D3DXVec3Scale(&vec1, &vec1, 2); cout << vec1.x << " " << vec1.y << " " << vec1.z << endl;

(which outputs "2 4 6").
Topic archived. No new replies allowed.