Peter F
Peter F

Reputation: 435

Finding the change needed in location to move around the circumference of a circle#2

I asked about this yesterday as well and was told that I needed to be using radians. I had assumed that was the answer but there does appear to be another problem with the code below. 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 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.toDegrees(Math.atan2(a.center[1] - midy, a.center[0] - midx));

    x1 = Math.cos(Math.toRadians(currentAngle+angle)) * r;
    y1 = Math.sin(Math.toRadians(currentAngle+angle)) * r;
    x2 = Math.cos(Math.toRadians(currentAngle+angle)) * -r;
    y2 = Math.sin(Math.toRadians(currentAngle+angle)) * -r;
    a.move((int)x1,(int)y1);
    a.target.move((int)x2,(int)y2);


}

Anyone see anything wrong with this? At the moment they end up meeting towards what I would think would be the middle of my circle, waaay closer than they were. Any thoughts?

Edit: Also, I am currently running this twice... once for each Gladiator involved. I can do that and just have them rotate half the desired amount each time, but it would be better if I could rotate them as a whole then disinclude the second party from the Gladiator list I am iterating through. What would be the most simple implementation of this?

Edit2: I think part of my problem was that I wasn't calling Math.toDegrees on my atan2 equation. I noticed it wasn't getting any new angle value other than 0, now it is. Still, there is something wrong. They rotate from horizontal to vertical but are moving much further from each other on each rotation and once they get to the vertical alignment they end up rotating the other direction just a few degrees (rather than 45, my current input) and then do get much closer together like before.

Edit3: Note that the move method's parameters are the change needed, not the actual coordinates.

Upvotes: 0

Views: 145

Answers (1)

Guvante
Guvante

Reputation: 19203

I see you are using int a lot, be very careful since you may get stuck depending on angle.

I did a quick rewrite to simplify your repetition and use the radian logic that was recommended. (Untested).

I also converted your locations to double to avoid odd integer arithmetic problems. (Your midx/midy calculations were in int math)

After finishing I realized you were rotating around (0,0) rather than the midpoint, and your mid variables were confusingly named.

//I would do double inline with the initialization, but left here in case you had another reason
double x1,y1, x2,y2, r, midx,midy, newAngle;

x1 = a.center[0];
y1 = a.center[1];
x2 = a.target.center[0];
y1 = a.target.center[1];

r = getDistance(a, a.target)/2;
midx = x1 + (x2 - x1)/2;
midy = y1 + (y2 - y1)/2;

newAngle = Math.toRadians(angle) +
           Math.atan2(midy, midx);

x1 = Math.cos(newAngle) * r + midx;
y1 = Math.sin(newAngle) * r + midy;
x2 = Math.cos(newAngle) * -r + midx;
y2 = Math.sin(newAngle) * -r + midy;
a.move((int)x1,(int)y1);
a.target.move((int)x2,(int)y2);

Upvotes: 1

Related Questions