15,947,389 members
See more:
Hi All

Recently I started WPF/C# 3D development. I am trying to rotate a rectangle. But the problem is when I rotate it, other axis also get rotated.

For example if I rotate it along x-axis by 90, y and z axes also get rotated. that is y-axis point towards negative z-axis and z-axis point to y-axis. Now if I want to rotate it around y-axis, I have to rotate it actually around z-axis only then it will rotate around y on the UI.

I'm guessing i have to keep track of each axis if I'm not missing anything. Can anybody help me ?

I have looked into quaternions but even then I have to keep track of axes.

this is the code I'm using

```public void RotateXAxis(RotateDirection rotaryDirection)
{
AxisAngleRotation3D axisAngleRotation = new AxisAngleRotation3D(new Vector3D(1,0,0), 90 * x * (int)rotaryDirection);
RotateTransform3D rotation = new RotateTransform3D(axisAngleRotation, new Point3D(7.5, 7.5, 7.5));
this.modelVisual3D.Transform = rotation;

}

/// <summary>
/// Rotates the slice arount Y-Axis
/// </summary>
/// <param name="rotaryDirection"></param>
public void RotateYAxis(RotateDirection rotaryDirection)
{
AxisAngleRotation3D axisAngleRotation = new AxisAngleRotation3D(new Vector3D(0, 1, 0), -90 * y * (int)rotaryDirection);
RotateTransform3D rotation = new RotateTransform3D(axisAngleRotation, new Point3D(7.5, 7.5, 7.5));

this.modelVisual3D.Transform = rotation;

}```

Regards,
zain ul abideen
Posted
Updated 31-Jul-18 8:14am
v2

## Solution 1

Interesting. This could be several things. :-)

A.

Somewhere in your code, you are using this rotation value and sticking it in somewhere. But the place you are using it could need a different format than what you put in (e.g. You may be sticking your values in the wrong place).

B.

This could be a bug in WPF. There are small bugs like that, littered throughout C# (e.g. when using foreach loop, you cannot modify the values, because that will cause the program to crash). So you might want to Google this, and see if this is a well-known problem.

C.

Maybe you are using the wrong technique to rotate (no offense intended). Check on MSDN (http://www.msdn.microsoft.com/),
and look in the Library, in the System.Windows section (http://msdn.microsoft.com/en-us/library/gg145013.aspx), and search through the classes to see if there is a more appropriate way to do 3D rotation.

Hope this helps.

:-)

## Solution 3

In the OpenGL and DirectX world, you "push" the model view matrix, then perform the transformation (translation and/or rotation), then "pop" the matrix. The original OpenGL programming guide had a great example in Chapter 3 of the earth orbiting the sun, demonstrating pushing and popping with the rotating and transforming between them. (Do a search for "planetary" or "solar system" for similar OpenGL or DirectX projects in C#.)

However, I/ve been struggling for some time trying to achieve the same thing with WPF/C# 3D. If I ever figure out how to "push" and "pop" the model view matrix in WPF, I'll post the code.

It's worth repeating that matrix multiplication is not commutative, so the order in which you translate and rotate is crucial.

v2

## Solution 4

zain ul abideen

See "How to: Apply Multiple Transformations to a 3-D Model" for sample code.

https://docs.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/how-to-apply-multiple-transformations-to-a-3-d-model