Search code examples
c#matrixxnarotationquaternions

XNA How do I rotate a car tire?


I have matrix that stores the tire rotation and translation

tireMatrix = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll);

This happens

When I move forward the wheel rotates fine but when I turn it turns as shown in the image. Can anyone help please.


Solution

  • I'm just making a guess on how you setup the rest of your code (such as whether the tire is part of the car or its own model, etc). This is one of many ways to do what you are trying to do. A car tire will only have to rotate along two axes: the axle of the car and the normal axis of the car. So to do a forward rotation you'll have to do something like this. (this is assuming your tire is part of the car mesh and has its own bone)

    tireMatrix *= Matrix.CreateRotationX(roll); //or whichever axis your axle is on
    

    And then to rotate your tire along the normal axis:

    tireMatrix *= Matrix.CreateRotationY(turnangle);
    

    If the tire is its own model (which is what your image looks like), then try:

    //rotate the tire along the right axis to make it spin
    tireMatrix *= Matrix.CreateFromAxisAngle(tireMatrix.Right, theta); 
    //rotate the tire along its normal axis
    tireMatrix *= Matrix.CreateFromAxisAngle(tireMatrix.Up, turntheta); 
    

    Also see here for more help on animating