# Car movement

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

## Recommended Posts

So, basically for moving a car, here is part of my coding:

 D3DXMATRIX worldMatrix; D3DXMatrixIdentity(&worldMatrix); D3DXMatrixTranslation(&worldMatrix, 0, 0, tz); D3DXMATRIX tempMatrix; D3DXMatrixRotationY(&tempMatrix, ry); D3DXMatrixMultiply(&worldMatrix, &worldMatrix, &tempMatrix); d3d_dev->SetTransform(D3DTS_WORLD, &worldMatrix); 

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!

##### Share on other sites
This:

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.

##### Share on other sites
@jyk, another problem crops up with the translation as i said..I can't do both the translation and the rotation without any problem.

##### Share on other sites
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.

##### Share on other sites
Rotation First, then Translation:

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!

##### Share on other sites

I don't know if you guys get my point

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).

##### Share on other sites
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 rotation matrices D3DXMatrixRotationX(&RotationMatrixX, Rotation.x); D3DXMatrixRotationY(&RotationMatrixY, Rotation.y); D3DXMatrixRotationZ(&RotationMatrixZ, Rotation.z); //set scalation matrix D3DXMatrixScaling(&ScaleMatrix, Scale.x, Scale.y, Scale.z); //set center of the mesh to origin D3DXMatrixTranslation(&TranslationMatrix, 0.0f, 0.0f, 0.0f); D3DXMatrixMultiply(&WorldMatrix, &ScaleMatrix, &TranslationMatrix); //apply rotation D3DXMatrixMultiply(&WorldMatrix, &WorldMatrix, &RotationMatrixZ); D3DXMatrixMultiply(&WorldMatrix, &WorldMatrix, &RotationMatrixX); D3DXMatrixMultiply(&WorldMatrix, &WorldMatrix, &RotationMatrixY); //set center of the mesh to its world position D3DXMatrixTranslation(&TranslationMatrix, Position.x, Position.y, Position.z); D3DXMatrixMultiply(&WorldMatrix, &WorldMatrix, &TranslationMatrix); //set transform m_lpDevice->SetTransform(D3DTS_WORLD, &WorldMatrix); }

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.

##### Share on other sites
Translation Then Rotation:

Rotation Then Translation:

##### Share on other sites
@The King2, How do i track the car position then?

##### Share on other sites
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.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
14

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632631
• Total Posts
3007532
• ### Who's Online (See full list)

There are no registered users currently online

×