Elliptical sprite movement

I am trying to get a 2d sprite to move in an “arc” (half an ellipse) instead of a straight line. I have start and end positions X and Y, as well as the desired radius.

What is the best way to implement this?

+3
source share
4 answers

If you want it to move in an ellipse, the easiest way that I know is to give y values ​​as a function of time with sin, and x values ​​as a function of time with cos. Assuming you use System.currentTimeMillis () ;, you save the start time in a variable (e.g. double startTime = System.currentTimeMillis ()), and then in each frame you get the elapsed time by subtracting the current time from the start time. (e.g. elapsedTme = System.currentTimeMillis () - startTime). Then the y value will be (radius in the y direction) * sin (elapsed time value) + the y value of the center of your ellipse, and the x value will (radius in the x direction) * cos (elapsed time * speed) + x in the center of your ellipse.

EDIT: X Y, , , - , , .

0

, . , u v.

, - u v (1, 0) (-1, 0) , 1. . , , :

x(t) = cos(pi * t)
y(t) = sin(pi * t)

, , , . -, w u = (x0, y0) v = (x1, y1). :

w = (x2, y2) = ((x0 + x1) / 2, (y0 + y1) / 2)

, u v , w . , u v . ,

     | 1  0 -x2 |
 T = | 0  1 -y2 |
     | 0  0   1 |

u v Tu Tv. u ' v'.

u' = (x0 - x2, x1 - y2) = (x0 / 2 - x1 / 2, y0 / 2 - y1 / 2)
v' = (x1 - x2, y1 - y2) = (x1 / 2 - x0 / 2, y1 / 2 - y0 / 2)

, , u ' v' x, . , , u ' (1, 0), v' (0, 1). , u ', - , . :

e0 = u' / ||u||
e1 = perp(e0)

perp - , e0. - , e0 = (x3, y3), e1 = perp(e0) = (-y3, x3). , (x3, y3), .

, (1, 0) e0 (0, 1) e1

|x3 -y3  0|
|y3  x3  0|
| 0   0  1|

( )

, , - e0 (1, 0) e1 (0, 1). , . , e0 e1 , , :

    | x3 y3 0|
R = |-y3 x3 0|
    |  0  0 1|

, R u' v', (1, 0) (-1, 0), , . , , , . , h, h 1. , 1 / h, , , 1. :

    | 1  0  0 |
S = | 0 1/h 0 |
    | 0  0  1 |

, , , , u v, SRT, , (1, 0) (-1, 0). , , . SRT , u v! , , (1, 0) (-1, 0), :

  • t , , (1, 0) (-1, 0) t. p.
  • p '= (SRT) -1 p.
  • p' - , .

, (SRT) -1. , (SRT) -1= T -1 R -1 S -1 :

     | 1  0 -x2 |          | 1  0  x2 |
 T = | 0  1 -y2 |   T^-1 = | 0  1  y2 |
     | 0  0   1 |          | 0  0   1 |

     | x3  y3  0|          | x3 -y3 0 |
 R = |-y3  x3  0|   R^-1 = | y3  x3 0 |
     |  0   0  1|          |  0   0 1 |

     | 1  0   0 |          | 1  0   0 |
 S = | 0 1/h  0 |   S^-1 = | 0  h   0 |
     | 0  0   1 |          | 0  0   1 |

, :

  • u = (x0, y0) v = (x1, y1), w = (x2, y2) = ((x0 + x1)/2, (y0 + y1)/2).
  • u '= u/|| u || = (x3, y3).
  • t ( 0 & le; t & le; 1) p = (cos (& pi; t), sin (& pi; t))
  • p '= S -1 p = (cos (& pi; t), h sin (& pi; t))
  • p '' = R -1 p '= (x3 cos (& pi; t) - y3 sin (& pi; t), y3 cos (& pi; t) + x3 sin (& pi; t))
  • p '' '= T -1 p' '= (x3 cos (& pi; t) - y3 sin (& pi; t) + x2, y3 cos (& pi; t) + x3 sin (& pi; t) + y2)
  • p '' ' .

, , ()! .

0

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


All Articles