Reputation: 435
I am trying to write a method which will allow my Gladiators to rotate a central point between the two, to circle each other so to speak. Below is my code. Gladiator variable target is of Gladiator type and is set to the second party in the rotation. center[0] is the x location, center[1] is y.
public void rotate(Gladiator a, int angle) {
double x1;
double y1;
double x2;
double y2;
double r;
double midx;
double midy;
int currentAngle2;
int currentAngle;
r = getDistance(a,a.target)/2;
midx = (a.center[0]-a.target.center[0])/2;
midy = (a.center[1]-a.target.center[1])/2;
currentAngle = (int)Math.atan2(a.center[1] - midy, a.center[0] - midx);
currentAngle2 = currentAngle + 180;
if (currentAngle2 > 360) {
currentAngle2 = currentAngle2-360;
}
System.out.println("Current angles = "+currentAngle+","+currentAngle2);
x1 = Math.cos(currentAngle+angle) * r + midx;
y1 = Math.sin(currentAngle+angle) * r + midy;
x2 = Math.cos(currentAngle2+angle) * r + midx;
y2 = Math.sin(currentAngle2+angle) * r + midy;
System.out.println("new coords: "+(int)x1+","+(int)y1+","+(int)x2+","+(int)y2);
a.move((int)x1,(int)y1);
a.target.move((int)x2,(int)y2);
}
What is the problem here? The first time this is run they end up on top of each other.
Edit:
angle is the requested rotation amount.
Upvotes: 0
Views: 102
Reputation: 158469
One problem is that you are using degrees while the math functions such as cos
etc... use radians.
Upvotes: 1