1
\$\begingroup\$

I am new to computer graphics and am trying to make a simple little game where I can move a cube around with the keyboard. I have it so you can rotate the cube with the arrow keys, and I wanted to make it move forward, back, left and right based on which way it was rotated. I saw a similar question on here posted in 2012 (Translate along local axis).I tried to do what they suggested, and it only works when the x, y, and z rotations are 0. If my cube is rotated, it seems to move in a circle. I am not sure if I misunderstood or if I implemented it wrong.

these are my rotation matrices. the x is the x rotation in degrees converted to radians, and so on for y and z.

xrot_mat = [[1,0,0,0],[0,np.cos(x*CONVERT),np.sin(x*CONVERT),0],[0,-np.sin(x*CONVERT),np.cos(x*CONVERT),0],[0,0,0,1]]
xrot_mat = np.array(xrot_mat)

yrot_mat = [[np.cos(y*CONVERT),0,-np.sin(y*CONVERT),0],[0,1,0,0],[np.sin(y*CONVERT),0,np.cos(y*CONVERT),0],[0,0,0,1]]
yrot_mat = np.array(yrot_mat)

zrot_mat = [[np.cos(z*CONVERT),np.sin(z*CONVERT),0,0],[-np.sin(z*CONVERT),np.cos(z*CONVERT),0,0],[0,0,1,0],[0,0,0,1]]
zrot_mat = np.array(zrot_mat)

I multiplied the x by the y and the answer of that by z (left to right order: x,y,z).

I then multiplied

ztrans_mat = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,1,1]] ztrans_mat = np.array(ztrans_mat)

by the product of those 3 matrices, with ztrans_mat on the left of the rotation matrix.

Finally, I multiplied

pos= Gf.Vec3d(position.Get())
fourd= Gf.Vec4d(pos[0],pos[1],pos[2],1)

with fourd (the cubes x, y, and z coordinate) on the left of the rotation matrix multiplied by the translation matrix.

Sorry to make this so long, I just tried to give as much information as possible. I am stuck and starting to get a little frustrated, since I feel so close yet so far.

If you need any clarification or any additional information, please let me know. Thank you in advance for any help you can give me!

\$\endgroup\$
0

3 Answers 3

1
\$\begingroup\$

If I understand correctly, you are transforming the position vector by the rotation matrix. Which would explain the mentioned circular motion.

Instead you want to transform the un-rotated movement vector (e.g. a vector in the direction the cube would move if it weren't rotated) by the rotation matrix. Which gives you the movement vector you would add to the position vector.

It is also possible to extract a direction vector from the rotation matrix, then scale it according the distance you want, which gives you a movement vector. See Translating an object along its heading.

\$\endgroup\$
0
\$\begingroup\$

Or you could just use basic maths. Calculate the local axis using formula y=ax+B Use two static points on the object. Do two lines. Calculate a and B. And then you have lokal axis. Every move you make the object do will have movement on X and formula for movement on y axis.

\$\endgroup\$
2
  • \$\begingroup\$ Given that the question describes x, y, and z angles, it sounds like they're working in 3D, so y = mx + b is not so suitable (even in 2D, it has a singularity for vertical lines). Instead you'd look for something like p_t = p_0 + t * d \$\endgroup\$
    – DMGregory
    Dec 1, 2022 at 16:27
  • \$\begingroup\$ True although the function would be relative move from current position and for 3d space any 3dimensional straight can be expressed as two 2d straights one on the xy and one on the eg zy \$\endgroup\$
    – Monogeon
    Dec 1, 2022 at 17:00
0
\$\begingroup\$

Your rotation matrix tells you which world axes are the object's local axes. You can directly read these vectors from the matrix without any processing:

annotated matrix

If you want to see which world direction is the object's local X direction, just read it directly from the first column of the matrix. The second column tells you the Y direction, and the third column tells you the Z direction. You can also see how the translation is the "local nothing direction".

(note: I think this is right, but I always get matrix order confused, so it might actually be the first row)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .