math - WPF 3D - rotate a model around it's own axes -
suppose have simple wpf 3d scene set single rectangle rotated -45 degrees around x axis so:
<viewport3d> <viewport3d.camera> <perspectivecamera position="0,0,4"/> </viewport3d.camera> <modelvisual3d> <modelvisual3d.content> <directionallight color="white" direction="-1,-1,-3" /> </modelvisual3d.content> </modelvisual3d> <modelvisual3d> <modelvisual3d.content> <geometrymodel3d> <geometrymodel3d.geometry> <meshgeometry3d positions="-1,-1,0 1,-1,0 -1,1,0 1,1,0" triangleindices="0,1,2 1,3,2"/> </geometrymodel3d.geometry> <geometrymodel3d.material> <diffusematerial brush="red"/> </geometrymodel3d.material> </geometrymodel3d> </modelvisual3d.content> <modelvisual3d.transform> <transform3dgroup> <rotatetransform3d> <rotatetransform3d.rotation> <axisanglerotation3d axis="1,0,0" angle="-45"/> </rotatetransform3d.rotation> </rotatetransform3d> </transform3dgroup> </modelvisual3d.transform> </modelvisual3d> </viewport3d>
this gives me following:
alt text http://www.freeimagehosting.net/uploads/4aa48434a9.png
now want rotate image 45 degrees around model's z axis. if put second rotatetransform3d in so:
<rotatetransform3d> <rotatetransform3d.rotation> <axisanglerotation3d axis="0,0,1" angle="45"/> </rotatetransform3d.rotation> </rotatetransform3d>
it rotates around scene's z axis. particular x rotation i've worked out need is:
<rotatetransform3d> <rotatetransform3d.rotation> <axisanglerotation3d axis="0,1,1" angle="45"/> </rotatetransform3d.rotation> </rotatetransform3d>
but here maths fails me. tell me how work out arbitrary x (and y if to) rotation?
ok, spoke mathematician friend , gave me answer:
so think need if you're rotating around vector (1,0,0) angle of 'a' (i.e rotating around x-axis transforming object in y-z plane).
further rotations around
x' - (1,0,0) stays same!
y' - (0,cosa,sina)
z' - (0,-sina,cosa)
a similar principle hold rotations in x-z plane (0,1,0)
x' - (-sina,0, cosa)
y' - (0,1,0) - same
z' - (sina,o,cosa)
and in x-y plane around (0,0,1)
x' - (-sina,cosa,0)
y' - (cosa,sina,0)
z' - (0,0,1) stays same
tada!
update: created function calculate matrix rotate object in 3 axes. can used matrixtransform3d.
matrix3d calculaterotationmatrix(double x, double y, double z) { matrix3d matrix = new matrix3d(); matrix.rotate(new quaternion(new vector3d(1, 0, 0), x)); matrix.rotate(new quaternion(new vector3d(0, 1, 0) * matrix, y)); matrix.rotate(new quaternion(new vector3d(0, 0, 1) * matrix, z)); return matrix; }
Comments
Post a Comment