How to rotate a sinusoid axis

I program the game in 2D. I have reached to move a sprite based on a user path. The path can be represented mathematically as: y = sin (x). So the movement is a wave. I want to turn this wave in such a way that the movement is not horizontal, but vertical or with some normal angle refers to the origin. I'm a little weak with math. I'm sorry. Anyone can help. My code is

for (int i=0; i<300; i++) { coordinatesX[i] = i; coordinatesY[i] = (float) (50 * Math.sin(coordinatesX[i])); } createpath (coordinatesX, coordinatesY); ... 
+4
source share
1 answer

Well, your object is represented by some initial coordinates (x, y) ^ t. To rotate this in 2D space you have to use a rotation matrix

 R = [ cos(a) -sin(a)] [ sin(a) cos(a) ] 

Since you also want to perform the T translation (moving along a sine wave), you can compose an affine transformation by expanding 2D coordinates to three-dimensional homogeneous coordinates. Suppose your translation is (tx, ty) and your rotation angle (in radians) is a, the transformation matrix will be

 T = [ cos(a) -sin(a) tx sin(a) cos(a) ty 0 0 1 ] 

When you convert the original (x, y) point to (x, y, 1), simple

  T * (x,y,1)^t 

will do the trick.

You can return from homogeneous to Cartesian coordinates by dividing all the elements into the last (i.e. you will lose one dimension). Since in this simple case they are always 1, you can simply delete the last coordinate and return to 2D.

Edit: Muting T and (x, y, 1) ^ t gives:

 T*(x,y,1)^t = [ cos(a) -sin(a) tx ] [ x ] [ sin(a) cos(a) ty ]*[ y ] = [ 0 0 1 ] [ 1 ] = [ cos(a)*x - sin(a)*y + tx ] [ sin(a)*x + cos(a)*y + ty ] = [ 1 ] = (cos(a)*x - sin(a)*y + tx, sin(a)*x + cos(a)*y + ty, 1)^t 
+4
source

Source: https://habr.com/ru/post/1390099/


All Articles