Simen Russnes
Simen Russnes

Reputation: 2270

Calculating rotation degrees based on delta x,y movement of touch or mouse

I have an object in 2D space that I want to rotate based on how an input in 2D space moves. Basically, think that you move your finger or mouse around on the screen either clockwise or counter clockwise, and the object rotates relative to how you rotate your finger. The position of the object does not matter at all, meaning the rotation is only translated to the object from the finger movement, and there is no relation between the object's position and the finger movement position.

I am using libGdx and I get the X and Y delta movement (positive and negative integers depending on direction of movement) in a call to a method "touchDragged".

How can I use this delta X and Y to calculate the degrees of movement that I can then apply to my object. Preferably I'd just like to add to a radian or degree value.

I can get it to work fine if only using one axis. I.e. if I move the finger up or down, or rotates either clock-wise or counter-clockwise. My problem is getting it to work as a result of both X and Y movement.

PS: I did a bit of looking around before posting this question and I know there are similar questions but I couldn't figure it out based on them. I don't know exactly why but it seemed like all the different questions were tackling different use cases each one different from mine.

Upvotes: 3

Views: 3655

Answers (3)

KookieMonster
KookieMonster

Reputation: 503

The simplest way of retrieving angle from [x0, y0] to [x1, y1] is using Math.atan2(y, x); (which finds the angle difference from the x axis).

Essentially, you could do something like this;

//direction from old to new location in radians, easy to convert to degrees
dir = Math.atan2(dy, dx);

(I've got a cold, not thinking straight today, this might give the exact opposite direction, in which case just add PI or 180° or w/e to flip it).

Upvotes: 2

Hllink
Hllink

Reputation: 918

[EDIT: as stated by Barodapride, libgdx provide the Vector2 API Vector2.angle(Vector2 Reference) that already returns the float angle value to you, but it's good to know what's going on under the hood]

Use the cartesian to polar coordinates, your axis position gonna be your 0,0 in your cartesian plane, i can't test it now but you can get the idea by the following code i wrote, take a look at this for futher reference:

public boolean touchDragged(InputEvent event, float posX, float posY, int pointer){
    //Declare and get the position of your axis (in screen coords, you can get that by camera.project()
    Float axisX,axisY;
    axisX = getAxisX();
    axisY = getAxisY();
    //Transform the clicked position to the cartesian plane given the axis
    Float cartesianX,cartesianY;
    cartesianX = posX-axisX;
    cartesianY = posY-axisY;
    //Calculate de radius
    Float radius = Math.sqrt( cartesianX * cartesianX + cartesianY * cartesianY );
    //Get the angle in radians
    Float angleInRadians = Math.acos( cartesianX / radius );
    //Get the angle in degrees
    Float angleInDegrees = angleInRadians *57.2958;
    return true;
}

the inverse, to get the x,y from a given radius and angle:

double x = getAxisX() + Math.cos( angleInRadians ) * radius;
double y = getAxisY() + Math.sin( angleInRadians ) * radius;

Upvotes: 2

Barodapride
Barodapride

Reputation: 3733

Make a Vector2 from the difference in X and Y. Use Vector2.angle() to get the angle of the vector relative to the x-axis. Apply that angle to your sprite object.

Vector2 API

Upvotes: 2

Related Questions