Sign in to follow this  

Moving/Positioning an Object

This topic is 4664 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

Ok so I finally get a model animated in my program so what is the next step? Get him to move while he runs instead of just running in place because running in place is boring.... I have been trying to get things to work but I cannot figure out how to make it work.... Here is the scenario... it is not a high class set up I have just been trying to get it to work any way I could think of: I have 3 static variables that increase or decrease when a given key is pressed/held... I use one variable to keep track of how much the camera has rotated one to keep track of how much the character has rotated and one to keep track of how far the character has moved. When the program loads the character is rendered at the origin looking down the +Z axis. I had it set up so when W/S were pressed the static variable for movement went up/down and then I did a D3DXMatrixTranslation with the variable as the Z value and 0 for x and y. Then I do D3DXMatrixRotationY twice once for Characters rotation and once for cameras rotation.... but now is where the problem lies. Say the reslt of the D3DXMatrixTranslation was Move and the result of the D3DXRotations were CamRotate and CharRotate At first I had the World matrix = Move * CamRotate * CharRotate.... and the character would successfully move in the direction he was facing but when you try to turn him he would just rotate around the origin so as you got further from the origin the worse things got. Then I realized that I should have the characters rotation transformation go first so he will turn and then I can move him then rotate the camera so WorldMatrix = CharRotate * Move * CamRotate and this resulted in the exact opposite problem he would rotate around his own Y-axis but he now only moved back and forth along the Z-axis no matter what direction he was facing.... I have been trying all sorts of crazy things and none of them have worked.... so can anyone explain to me how you go about positioning a character so he will spin around his own Y-axis and also move the direction he is facing... I am really out of ideas.... it is times like this I wish I had learned more geometry in high school : / Thanks in advance.

Share this post


Link to post
Share on other sites
I have tried both those methods already and to no avail... the first thing just makes wierd things happen when the camera rotates and the 2nd thing causes the Rotation you transform the forward vector with cancels out the REAL rotation transform resulting in the same original problem of rotating around the origin.

I also just tried a new plan I had where I take the sin and cos of the angle used to find the rotation transform and use the results to orient the forward vector and it works.... sort of. It works in the sense that I can rotate him around his own axis and move him anywhere along the XZ plane but now there is a new problem which I was afraid might happen.... once I move him away from the origin then rotate him the next time I move him he just jumps across the screen (to the other side of the circle he is standing on). So basically it seems like I need a way to reset the origin to where he is standing or something. I am really runnin out of ideas here. I know there must be someone out there who has a working model that can actually run and turn...... HOW IS IT DONE!!!

Please and Thx

Share this post


Link to post
Share on other sites
Wow Provoker, lets calm down and try the divide/conquer strategy (remember that generally speaking when you run out of ideas then the right one come... [smile])

My opinion (and everyone feel free to correct/suggest) is that you should things part by part, in this order:

1) Choose a method to store the ORIENTATION of your model. I personally prefer quaternions, because you dont suffer from "gimbal lock" (where an angle nullify another), you can sum several rotations together and you can perform smooth interpolations between quaternions (slerp). Whatever way you choose to store the rotations, you should be able to extract the object´s LOCAL AXIS from it. Note that each axis is represented by a vector, so in the end you have THREE vectors indicating the orientation. Then, test the rotation to ensure that they are working as expected.

2) Choose a method to store the POSITION of your model. You can easily do this with a vector (x,y,z). Note that this vector doesn´t mean the direction where your model is facing, just the position of it.

3) When you need to move, you must choose by what LOCAL axis of rotation your model will move (in your case is always forward, so it is always the Z axis, but if you strafe is X axis, and if you jump is Y axis). Then, you multiply the distance you want to move with this axis, and then add the result to the model´s current position. I think some code will clarify:

public void Move(Vector axis, float distance)
{
position += (axis * distance); //"position" is a vector
UpdateMatrix();
}


4) Now, the matrices. You have a vector that indicates the position, you can use D3DXMatrixTranslation to create a matrix from it. If you choose a quaternion to store the rotation, you can use D3DXMatrixRotationQuaternion to create a matrix from it. You then concatenate the matrices (rotation * translation, the inverse will make the model rotate about a imaginary point, not its own center) and call device.SetTransform for the WORLD transformation.

5) Well, at this point your model should move and rotate independently, so its time to think of your camera. The camera have the same properties of the model, so you can inherit both of them from the same base class. But instead in the camera you call device.SetTransform for the VIEW transformation. The DX will concatenate the WORLD and VIEW matrices behind the scenes to present your model according to the camera position.

Well, at least this way works for me, and I dont want to put up too much code because its best for you to travel the path yourself. But if you dont resist take a look at OGRE and PurpleSharp engines, their source code is open and I have learned a lot from them.

Hope this helps.

Share this post


Link to post
Share on other sites
Yes Raptor this helps.... I just discovered an article about doing this kind of math earlier today but just using rotation matrices... I still do not quite understand quaternions although I see there seems to be a bit of a holy war on about what is the better method for doing rotations heh.
I think I understand how things are supposed to work but I am a little shaky on doing relative motion.... how I understand things:

When things start the object is at the origin so local axes are same as world axes and the rotation matrix is just the identity matrix.
A key is pressed to turn the object so the rotation matrix is determined by the number of radians that the object gets turned by which is determined by how long the key is held down. At this time the object still has not actually moved from the origin so the objects position will be represented by the 3rd row
(or column) of the newly determined rotation matrix.
When a key is pressed to move the object forward the Translation matrix is determined by multiplying the 3rd row of the rotation matrix (the local view axis) by the factor you want the object to move forward by.... and the world transformation is determined by Rot * Translation.
If the object is rotated again the new matrix rotation is determined again by the number of radians turned and the new rotation matrix is multiplied with the old one to get the current orientation of the object.
If the object is moved forward again the position can again be determined from the 3rd row of the rotation and then the model can be moved along the local forward axis by multiplying this vector by the movement factor again.

I am hoping that I have this straight and I would appreciate it if someone could please correct me if I am wrong in anything that I just said above.

Thanks in advance

Share this post


Link to post
Share on other sites
I am still not getting this.... I still have the same problem, either he moves in a straight line or he just rotates around the origin..... I don't understand how I am supposed to get the local axes.

EDIT: Now I am able to rotate him around his local axis but he also rotates around the world axis at the same time.

Share this post


Link to post
Share on other sites
Quote:
At this time the object still has not actually moved from the origin so the objects position will be represented by the 3rd row (or column) of the newly determined rotation matrix.


Are you using 4x4 or 3x3 matrices? Because 3x3 matrices can only represent rotation, while 4x4 can represent rotation and translation, the translation being the 4th row (or column).

I think I understand better now how are you doing. You are using a single matrix to keep track of the object´s position and orientation. While you certainly can do this, maybe its better to split it up to clarify things, and once it get to work, you can put everything together in one single matrix. Remember to multiply in the correct order, rotation * translation.

Keep us informed.

Share this post


Link to post
Share on other sites
I think I know what my problem is now but I still do not know how to fix it... basicall the issue is I need to know the objects orientation in order to figure out his position but I need the objects position to figure out his orientation.... this is why I get the result of only being able to move him along the Z-axis while rotating him around his own axis OR rotating him around the origin while moving him anywhere in the XZ plane. All the things I have read explaining this kind of stuff are just like "You can just get the orientation from the 3rd row of your matrix so then you just move your character along that vector to position him." but the way to find the 3rd row of the matrix is by getting a vector from the origin to the objects position and normalizing it..... so it is the old 2 unknowns 1 equation problem and I CANNOT figure out where I am supposed to get the 2nd equation from but I need a way to figure out his position or the direction he is facing without using the rotation or translation matrices and I am slowly becoming insane thinking about this.

Share this post


Link to post
Share on other sites
The orientation of the camera is normally not included in the world matrix. Normally, it affects the view matrix instead.

The world matrix transforms points from model space to world space. You move and orient the model in the world by changing the world matrix.

The view matrix transforms points in world space to view space. When you move and rotate the camera, you change the view matrix to move and rotate the world relative to the camera.

Strictly speaking, you don't have to use the view matrix. But when you start moving and rotating the camera, it becomes much simpler to bite the bullet and go to the next level -- update the view matrix according to the position and orientation of the camera.

Share this post


Link to post
Share on other sites
To move the character:

Declare vectors that represent the character's direction and the character's position. For example:
D3DXVECTOR3 dirChar (0.0f, 0.0f, 1.0f ); (local vector)
D3DXVECTOR3 Pos (0.0f, 0.0f, 0.0f ); (global vector)

So:

// Inputs to translate the character (just an example)
if ( 'forward' )
t = 0.01f;
else if ( 'backward' )
t = -0.01f;
else
t = 0.0f;
.
.
.

D3DXMatrixRotationY( &CharRotate, rotValue );

// When you rotate the character around Y, also rotate the dirChar vector:
D3DXVec3TransformCoord( &dirChar, &dirChar, &CharRotate );

// update the character's position in the dirChar's direction
Pos = Pos + t * dirChar;

D3DXMatrixTranslation( &CharTrans, Pos.x, Pos.y, Pos.z );

// Always make the rotation first
WorldMatrix = CharRotate * CharTrans;

pDevice->SetTransform( D3DTS_WORLD, &WorldMatrix );

To move the camera, you could implement a similar code or simply to use D3DSmartCamera.

Share this post


Link to post
Share on other sites
THANK YOU SO MUCH ADRIANO (and also thanks to Raptor for keeping me sane)... the way you have shown me worked perfectly.... this is close to what I was trying originally but I did not have a static Position vector I was just trying to constantly transform him from the origin into place.... seems like I was making things alot more complicated than they needed to be.

Thanks again!

Share this post


Link to post
Share on other sites

This topic is 4664 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this