Note: tz is a variable which value increases when up arrow key is pressed and decreases when down arrow key is pressed. Same for ry, when left arrow key is pressed, its value decreases and when right arrow key is pressed its value increases.
The Problem: The car is always rotated around the origin, which makes the movement unrealistic. So, if the car is like far away from the origin, it is not rotated around itself but around the origin.
I have tried with D3DXMatrixRotationAxis() function but with no success..
Another thing, if i do the rotation first and then the translation, the car rotate around itself anywhere it is, even far from the origin. BUT, the translation of the car is messed up!
Another thing, if i do the rotation first and then the translation, the car rotate around itself anywhere it is, even far from the origin.
Is what you want. However, you'll also need to change this:
D3DXMatrixTranslation(&worldMatrix, 0, 0, tz);
Specifically, you'll need to track and update the car's position in world space, and then submit the car's world-space position in the above line of code.
I can't do both the translation and the rotation without any problem. [/quote]
As you said, the car is always rotated around its origin. You can't change this behavior, you have to change the order to rotation->translation, this will rotate the car in the origin (=rotation around itself) and then translate it to wherever you want.
Problem: Car rotation OK But when i press up or down, the car will NOT move in the direction it is facing.
Translation First, then Rotation:
Problem: When pressing Up or down, the car will move according to the direction it is facing, but the when pressing right/left, the rotation will be around the origin.
I don't know if you guys get my point, i will try to upload a video on youtube if i can to show you the problem!
We understand what you're saying :) As I said above though, you'll need to apply the transforms in the order rotation->translation, and track and update the world position of the object in world space (which you don't appear to be doing currently).
Yes I get it. Well I was facing the same problem at first. I'm not so sure how I solved it, but does you code for translation/rotation looks something like this:
void CPrimitive::SetWorldTransformation(D3DXVECTOR3 Position, D3DXVECTOR3 Rotation, D3DXVECTOR3 Scale)
{
D3DXMATRIX WorldMatrix, RotationMatrixX, RotationMatrixY, RotationMatrixZ, TranslationMatrix, ScaleMatrix;
//set center of the mesh to origin
D3DXMatrixTranslation(&TranslationMatrix, 0.0f, 0.0f, 0.0f);
D3DXMatrixMultiply(&WorldMatrix, &ScaleMatrix, &TranslationMatrix);
//set center of the mesh to its world position
D3DXMatrixTranslation(&TranslationMatrix, Position.x, Position.y, Position.z);
D3DXMatrixMultiply(&WorldMatrix, &WorldMatrix, &TranslationMatrix);
Thats the way I'm doing it, and so it should work. Only difference is that I'm calculating the position elsewhere in the code so you might want to change that. Hope that helps.
Nice example videos :) But yeah, what you're seeing in the second video is exactly what you should expect.
I explained what the problem is a couple of times previously (and I think The King2 did as well), but perhaps it was lacking context. If there's anything about those answers that was unclear, feel free to ask and I'm sure we can get it cleared up.