Oliver Dixon
Oliver Dixon

Reputation: 7405

Getting a lower rotational value from Angle & Degree

public void TurnTowardsPoint(double DestinationX, double DestinationY)
{
    int distX = (int) (DestinationX - x);
    int distY = (int) (DestinationY - y);

    double angleRadians = Math.atan2(distY, distX);
    int angleDegrees = (int)Math.toDegrees(angleRadians);

    //angleDegrees = Math.abs(angleDegrees);

    Log.w("Angle", Integer.toString(angleDegrees));
    Log.w("Degrees", Integer.toString((int) this._degrees));

    if((this._degrees) - 360 != angleDegrees)
    {
        if(this._degrees >= 0)
        {
            RotateItem(true);
        }
        else
        {
            RotateItem(false);
        }


    }
}

RotateItem(true is clockwise false is anti, it can never go minus otherwise it resets to 0, although this is changeable).

What I'm trying to achieve is to get the shortest way to rotate & also finish when it's pointing at the Angle point of origin.

I've been on this for a few hours now my problem is I never got taught Trigonometry at school so I'm struggling with the concepts & understanding what to use when. (I've been reading up on it).

How would I do this? I can't find anything online so I had to eventually give up and asked you guys after staring at a piece of paper for 2 hours and getting no where.

Notes: Facing right the ship is 360/0D. If the ship is directly under the point it is -90, above it is +90. If the ship is to the right directly it is 180D.

The problem lies with the 2 if statements. The First if statement needs to stop the rotation when it's pointing at the angle, the second one is suppose to decide which way is shortest. Those 2 statements are wrong.

The problem lies with this part:

    if((this._degrees) - 360 != angleDegrees)
    {
        if(this._degrees >= 0)
        {
            RotateItem(true);
        }
        else
        {
            RotateItem(false);
        }


    }

Upvotes: 1

Views: 186

Answers (1)

Daniel Fischer
Daniel Fischer

Reputation: 183888

Okay, if I haven't misunderstood, you have this._degrees, which gives the direction your object is currently pointing in, in integer degrees, normalized to lie between 0 (inclusive) and 360 (in- or exclusive?). And you calculate the direction in which the object shall point after rotating as angleDegrees.

If I'm understanding correctly, you have a sign error in that calculation because the convention is for counterclockwise turns to be the positive direction (but if your Y-axis has 0 above 1, that would cancel out, you have to check and determine whether angleDegrees is as it should or has to be negated).

Anyway, angleDegrees gives the direction you want to turn to, in the range from -180 to 180 degrees. Normalize it so that the direction lies in the range 0 to 360,

if (angleDegrees < 0) {
    angleDegrees += 360;
}

Now, the amount and direction in which to turn can be obtained by subtraction,

int turn = angleDegrees - this._degrees;

That value lies between -360 and 360 degrees (more precisely between -this._degrees and angleDegrees). And it may not give the shortest turn yet, if the difference is smaller than -180 degrees or larger than 180, it's more than a half turn and the other way would be shorter, therefore we normalize once more,

if (turn < -180) {
    turn += 360;
} else if (turn > 180) {
    turn -= 360;
}

Now turn lies between -180 and 180 and gives the shortest turn to the desired direction. If turn >= 0, you want to turn clockwise, else counterclockwise

boolean clockwise = (turn >= 0);

and now you only have to rotate abs(turn) degrees, e.g.

int amount = (turn < 0) ? (-turn) : turn;
for(int i = 0; i < amount; ++i) {
    RotateItem(clockwise);
}

Upvotes: 1

Related Questions