Question

Here I am calculating degree between actor and touch point and passing this degree to actor as rotation angle.I want the actor should point towards the finger touch or finger dragging direction.(consider i am pulling bus on road by finger touch and drag )

float degrees = (float) ((Math.atan2(touchPoint.x - crocodile.position.x, -(touchPoint.y - crocodile.position.y)) * 180.0d / Math.PI));

Its calculating degree properly. only sometimes in between it is giving bit different degree. so my actor is fluctuating when result is really different than expected.

result that i printed is :

Blockquote

    degree is :: 141.93233
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 158.61426
    degree is :: 90.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 90.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93057
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93306
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93257
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93134
    degree is :: 141.93257
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93134
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93306
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93257
    degree is :: 180.0
    degree is :: 141.93134
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93257
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93257
    degree is :: 180.0
    degree is :: 141.93134
    degree is :: 180.0
    degree is :: 141.93257
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93008
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93306
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93257
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93134
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0

Now , 180 and 141 why it is fluctuating with those values. considering i am expecting 141 constantly or 180 constantly what i need to change in above formula.

No correct solution

OTHER TIPS

First parameter must be y and the second is x.

double atan2(double y, double x)

Atan2 is actually specified arctangent function of y/x

Because of that usually first parameter is taken y.

Also you can get proper degree sometimes when you swap x and y because in some borders like x<0 and y<0 it doesn't matter swapped or not.

Look here for more explanation

Combining the question and the answer helped me solve my problem quickly. Just to clarify the solution a little:

double degrees = Math.atan2(
    towards.getY() - pointer.getY(),
    towards.getX() - pointer.getX()
) * 180.0d / Math.PI;
pointer.setRotation((float) degrees);

Also note that in order to rotate around the middle you need to do:

pointer.setOrigin(Align.center);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top