2
$\begingroup$

I'm programming a game. Part of the game involves a spinning rectangle, so I'd like to keep track of two points on that rectangle. The center, and the bottom right corner of it.

enter image description here

I want to draw a sine wave of the relative vertical distance between, the center and the corner.

At 0, the distance is -16.

At $\frac{\pi}{2}$, the distance is 32

At $\pi$, the distance is 16

At $\frac{3\pi}{2}$, the distance is -32

At $2\pi$, this distance is -16

This isn't like to periodic functions I studied in school, where the distance between $f(0)$ and $f(\frac{\pi}{2})$ is the same as the distance between $f(\frac{\pi}{2})$ and $f(\pi)$, etc.

I'm having trouble figuring out the equation of this function.

Would appreciate any help.

  • 0
    Your sine wave will be sightly offset since the vertical distance will be zero when the rectangle will be tilt by a small angle.2017-02-22
  • 0
    @gt6989b Thanks. However, I have no idea how to come up with an equation when tracing an ellipse. Could you possibly point me in the right direction?2017-02-22
  • 0
    @The_Questioner I'm not aware of the equations offhand, but I'm *sure* they are readily Googleable.2017-02-22
  • 0
    @gt6989b The observed corner has always the same distance to the center, unless the rectangle is subject to deformation. The set of all point with a given distance to a given center is ... a circle.2017-02-22
  • 0
    @ReinhardMeier agree, removed comment2017-02-22

1 Answers 1

3

Your corner has the initial coordinates $$\pmatrix{x_0\\y_0} = \pmatrix{32\\-16}.$$ In order to get the new coordinates, you have to apply the rotation, which can be expressed in terms of a matrix-vector multiplication: $$ \pmatrix{x\\y} = \pmatrix{\cos\varphi & -\sin\varphi \\ \sin\varphi & \cos\varphi} \pmatrix{x_0\\y_0} $$ So your $y$ can be calculated as $$ y=32\sin\varphi -16\cos\varphi $$ If you want to have only one trigonometric function, this can be written as $$ y=16\sqrt{5}\sin(\varphi-\varphi_0)\;\;\mbox{with}\;\; \varphi_0=\arctan\left(\frac{1}{2}\right) $$

  • 0
    Oh, thanks. Matrix transformations were the only things I didn't quite understand in my linear algebra course. I guess, I'll go review them then. Thanks.2017-02-22