I'm trying to create a method which rotates a point around another point in Java for a Asteroids clone that I want to make.Right now I have 2 Point instances called point and center.The code for my method is this:
public void Rotate(double angle){
double a;
int x,y,distance;
distance=30;
a=Math.atan2((int)(point.getX()-center.getX()),(int)(point.getY()-center.getY()));
a=a+angle;
x=(int)(Math.cos(a)*distance);
y=(int)(Math.sin(a)*distance);
point.setLocation(x,y);
}
And the code for my gameloop is this:
while (true){
game.Rotate(10);
game.repaint();
Thread.sleep(10);
}
The problem is that the distance between the point and the center increases or decreases and I don't know why.Can somebody please tell me what's wrong?
EDIT [PROBLEM SOLVED]: For anyone interested here is how I solved the problem using the help that I got from the answers below: First, the Rotate function:
public void Rotate(double angle){
double x,y;
double distance=60;
x=Math.round(center.getX() + (Math.cos(Math.toRadians(angle))*distance));
y=Math.round(center.getY() + (Math.sin(Math.toRadians(angle))*distance));
point.setLocation(x,y);
}
Then I made another method called move:
public void move(){
angle+=2;
if(angle>360){
angle=0;
}
Rotate(angle);
}
And this is the game loop code:
while(true){
main.move();
main.repaint();
Thread.sleep(10);
}
Thanks again for your support.