问题
From this answer; https://stackoverflow.com/a/34060479/1493455 I have made some code that should rotate a point around an origin point. The origin is at 0,0,0.
Instead of my rotations being yaw pitch roll, I have xrot yrot and zrot which are degrees (0-360). A rotation around the X axis results in the object pitching back and forth, a rotation around the Z axis results in it rotating as if it was the yaw of the object.
I think I messed up somewhere in translating the values, but I can't seem to figure out why.
When rotating the points around each seperate angle (keeping the other 2 on 0 degrees) every rotation gives the right solution.
Combined, the results are good when rotating over X and Z or Y and Z. The results are not correct when rotating around X and Y.
var cosa = cos(-degtorad(zrot));
var sina = sin(-degtorad(zrot));
var cosb = cos(-degtorad(yrot));
var sinb = sin(-degtorad(yrot));
var cosc = cos(-degtorad(xrot));
var sinc = sin(-degtorad(xrot));
var Axx = cosa*cosb;
var Axy = cosa*sinb*sinc - sina*cosc;
var Axz = cosa*sinb*cosc + sina*sinc;
var Ayx = sina*cosb;
var Ayy = sina*sinb*sinc + cosa*cosc;
var Ayz = sina*sinb*cosc - cosa*sinc;
var Azx = -sinb;
var Azy = cosb*sinc;
var Azz = cosb*cosc;
var px = Axx*x + Axy*y + Axz*z;
var py = Ayx*x + Ayy*y + Ayz*z;
var pz = Azx*x + Azy*y + Azz*z;
degtorad is basically return input * pi / 180
I feel like there is an issue with the order of the calculation - since individual rotations give the proper outcome, just the combination of these 3 don't.
I know this because I'm rotating a 3D model using the zrot, yrot and xrot values, and drawing the point in the same 3D space - the points line up with the model in most cases, but not rotations on X and Y, or X, Y and Z.
VIDEO OF RESULT: https://streamable.com/481ly (there are 2 points on the corners of that cupboard which are calculated through this code)
来源:https://stackoverflow.com/questions/58485284/applying-a-rotation-matrix-on-a-point-with-an-origin-fails