input - Calculating rotation degrees based on delta x,y movement of touch or mouse -
i have object in 2d space want rotate based on how input in 2d space moves. basically, think move finger or mouse around on screen either clockwise or counter clockwise, , object rotates relative how rotate finger. position of object not matter @ all, meaning rotation translated object finger movement, , there no relation between object's position , finger movement position.
i using libgdx , x , y delta movement (positive , negative integers depending on direction of movement) in call method "touchdragged".
how can use delta x , y calculate degrees of movement can apply object. preferably i'd add radian or degree value.
i can work fine if using 1 axis. i.e. if move finger or down, or rotates either clock-wise or counter-clockwise. problem getting work result of both x , y movement.
ps: did bit of looking around before posting question , know there similar questions couldn't figure out based on them. don't know why seemed different questions tackling different use cases each 1 different mine.
[edit: stated barodapride, libgdx provide vector2 api vector2.angle(vector2 reference) returns float angle value you, it's know what's going on under hood]
use cartesian polar coordinates, axis position gonna 0,0 in cartesian plane, can't test can idea following code wrote, take @ this futher reference:
public boolean touchdragged(inputevent event, float posx, float posy, int pointer){ //declare , position of axis (in screen coords, can camera.project() float axisx,axisy; axisx = getaxisx(); axisy = getaxisy(); //transform clicked position cartesian plane given axis float cartesianx,cartesiany; cartesianx = posx-axisx; cartesiany = posy-axisy; //calculate de radius float radius = math.sqrt( cartesianx * cartesianx + cartesiany * cartesiany ); //get angle in radians float angleinradians = math.acos( cartesianx / radius ); //get angle in degrees float angleindegrees = angleinradians *57.2958; return true; }
the inverse, x,y given radius , angle:
double x = getaxisx() + math.cos( angleinradians ) * radius; double y = getaxisy() + math.sin( angleinradians ) * radius;
Comments
Post a Comment