0
\$\begingroup\$

I can't manage to rotate point around another point.

I did some searching around google and found algorithm that should work and it kinda does but it also moves the point I am rotating closer to pivot point.

Here is whole class where the problem is

public class Player {

float rotation;
Vector2 position;
Vector2 center;

public Player() {
    position = new Vector2(Core.WIDTH / 2 - 10, Core.HEIGHT - 50 - 10);
    center = new Vector2(Core.WIDTH / 2, Core.HEIGHT / 2);
    rotation = 0;
}

public void turnRight() {
    rotation += 0.001;

    position.x = (float) (Math.cos(rotation) * (position.x - center.x) - Math.sin(rotation) * (position.y - center.y) + center.x);
    position.y = (float) (Math.sin(rotation) * (position.x - center.x) + Math.cos(rotation) * (position.y - center.y) + center.y);
}

public void render(ShapeRenderer sr) {
    sr.circle(position.x, position.y, 20);
}

}
\$\endgroup\$
1
\$\begingroup\$
public class Player {

float rotation;
Vector2 position;
Vector2 center;
float length;

public Player() {
position = new Vector2(Core.WIDTH / 2 - 10, Core.HEIGHT - 50 - 10);
center = new Vector2(Core.WIDTH / 2, Core.HEIGHT / 2);
rotation = 0;
}

public void turnRight() {
    rotation += 0.001;
    length = Math.sqrt(((position.x - center.x) * (position.x - center.x)) + ((position.y - center.y) * (position.y - center.y)))

    position.x = (float) (Math.cos(rotation) * length + center.x);
    position.y = (float) (Math.sin(rotation) * length + center.y);
}

public void render(ShapeRenderer sr) {
    sr.circle(position.x, position.y, 20);
}

}

Changing it to this should work. I don't like using square roots because they're terribly slow (using a few each frame is not a problem, using hundreds is.) I suggest you read up on trigenometry, because the calculation you posed makes little sense. No offense intended. Also, I noticed position = new Vector2(Core.WIDTH / 2 - 10, Core.HEIGHT - 50 - 10);. Are you sure you don't mean it to be position = new Vector2(Core.WIDTH / 2 - 10, Core.HEIGHT / 2 - 10);?

\$\endgroup\$
2
\$\begingroup\$
float rotation = 90f;
while(true)
{
    position = CircleRadius * new Vector2(Math.Cos(MathHelper.ToRadians(rotation)), 
    Math.Sin(MathHelper.ToRadians(rotation))) + center;
    rotation += 1f;
}

1

\$\endgroup\$

You must log in to answer this question.

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