0

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)

3
  • 1
    You must keep in mind that rotations are not generally commutative, i.e. a rotation about X followed by one in Y does not produce the same result as identical rotations performed in the opposite order. In the case of Euler angles, changing one angle effectively rotates the axes for the other two. Better to use matrices + an established convention (such as the yaw-pitch-roll variant of Euler angles) than to reinvent the wheel. Commented Oct 21, 2019 at 13:36
  • Thanks! The problem is I'm stuck using GameMaker's built-in functions for rotating models; docs2.yoyogames.com/source/_build/3_scripting/4_gml_reference/… which I use to submit vertex buffers with. I then need the points that this code will generate in different parts for collision checking. Commented Oct 21, 2019 at 13:52
  • @meowgoesthedog looking at the function list... I actually saw matrix_transform_vertex - and it does exactly what I want the way its supposed to. Thanks :) Commented Oct 21, 2019 at 13:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.