0
\$\begingroup\$

red is my origin point, blue is my offset, both are 2D vectors, how can calculate blue after rotation?

this is the code I've been trying(just throwing things against a wall to see what sticks)

public static Vector2F CalcPointWithRotation(Vector2F origin, Vector2F point, float rotation)
{
    return new Vector2F((float)Math.Cos(rotation) * origin.x, (float)Math.Sin(rotation) * origin.y);
}

enter image description here

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Assuming your rotation variable is an angle in radians around your object's origin point, and your point variable is an offset vector relative to the origin in the object's local coordinate space, then this is simply:

public static Vector2F CalcPointWithRotation(Vector2F origin, Vector2F point, float rotation)
{
    float c = (float)Math.Cos(rotation);
    float s = (float)Math.Sin(rotation);

    return new Vector2F(
                        origin.x + c * point.x - s * point.y,
                        origin.y + s * point.x + c * point.y,
    );
}

You may recognize this as a matrix multiplication:

$$\vec p ^\prime = \begin{bmatrix} \cos\theta &-\sin\theta\\\sin \theta & cos\theta \end{bmatrix}\vec p + \vec o$$

Or with a homogeneous matrix...

$$\begin{bmatrix}p^\prime_x \\ p^\prime_y \\ 1 \end{bmatrix} = \begin{bmatrix} \cos\theta &-\sin\theta & o_x\\\sin \theta & cos\theta & o_y \\ 0 & 0 & 1\end{bmatrix} \begin{bmatrix}p_x \\ p_y \\ 1 \end{bmatrix}$$

When rotation is 0 (or 2 pi, 4 pi, etc.) then this just gives origin + point. As the rotation value increases, the offset from the origin swings around, tracing a circle around that point.

\$\endgroup\$
2
  • \$\begingroup\$ this seems to work fine on the X-axis but the Y-axis barely moves at all \$\endgroup\$
    – BBQGiraffe
    Commented Mar 9, 2021 at 21:26
  • \$\begingroup\$ My apologies, I went too fast and left out the second terms on each line. That should be better now. You can flip the signs on the two s terms if you want a positive angle to rotate in the opposite direction. \$\endgroup\$
    – DMGregory
    Commented Mar 9, 2021 at 21:29

You must log in to answer this question.

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