I am trying to include the projectile motion for my game app.When the user touches the imageview it calculates the initial angle based on the motion of the imageview.But in logcat I am seeing that these angle values are negative .Can anyone explain why is this so .I want the use r to just set the value of angle between 0 and 90 degree.
Find below my code for calculating angle:-
float dy = motion.getY()-mImage.getPivotY();
float dx = motion.getX()-mImage.getPivotX();
double r = Math.atan2(dy, dx);
int angle = (int)Math.toDegrees(r);
where motion is of type MotionEvent.
Vertical screen coordinates such as that being returned by
motion.getY()andmImage.getPivotY()define the origin as the top of the screen moving positively from top to bottom.Standard mathematical convention is the opposite of this. You will need to factor this into your equation. In this case by using
-dy.By way of explanation taking your original equation:
and adjusting the screen coordinates to canonical coordinates
Which gives:
Hence:
This is why you are seeing a negative value.