Question

I have a problem of finding the angle of three points with mouse pointer. The rotation doesn't following the mouse as the mouse move around the rotating object.

This is my formula :

double degree = Math.toDegrees( Math.atan2(
                 (now.y - point.y) - (previous.y - point.y),
                 (now.x - point.x) - (previous.x - point.x)));

         Log.w("RESULT", "DEGREE = " + degree);
        if ( degree < 0 )
          degree = 360 - (-degree);

This last 2 line was added later.

This is the full code of tracking the mouse rotation.

Camera camera = Camera.getInstance();

     if (event.getActionMasked() == MotionEvent.ACTION_DOWN &&
         event.getPointerCount() == 1) {
         previous = new Point(event.getX(), event.getY());
         previous = camera.getMapPoint(previous);

     }
     else if (event.getActionMasked() == MotionEvent.ACTION_MOVE && 
         event.getPointerCount() == 1) 
     {  
         Point now = new Point(event.getX(), event.getY());
         now = camera.getMapPoint(now);

         ArrayList<GameObject> list = mObject.getSubObjects();
         GameObject part = list.get(0);
         Point point = part.getPolygon().getCenter();

         Log.w("RESULT", "Previous = " + previous.x + " " + previous.y);
         Log.w("RESULT", "Next = " + now.x + " " + now.y);
         Log.w("RESULT", "Center = " + point.x + " " + now.y);

         double degree = Math.toDegrees( Math.atan2(
                 (now.y - point.y) - (previous.y - point.y),
                 (now.x - point.x) - (previous.x - point.x)));

         Log.w("RESULT", "DEGREE = " + degree);
        if ( degree < 0 )
          degree = 360 - (-degree);


         android.graphics.Matrix matrix = new android.graphics.Matrix();
         matrix.postRotate((float) degree, point.x, point.y);


        // Polygon polygon;
        // Polygon.Builder builder = new Polygon.Builder();
         for (int i=0; i<part.getPolygon().getVertex().size(); i++) 
         {
             float[] newPoint = new float[2];
             Point p = part.getPolygon().getVertex().get(i);
             newPoint[0] = p.x;
             newPoint[1] = p.y;

             matrix.mapPoints(newPoint); 
             part.getPolygon().getVertex().remove(i);
             part.getPolygon().getVertex().add(i, new Point(newPoint[0], newPoint[1]));

         } 
         previous = new Point(event.getX(), event.getY());
         previous = camera.getMapPoint(previous);
     }
     else if (event.getActionMasked() == MotionEvent.ACTION_UP)
     {
        previous = null; 
        isSelected = false;
     }
Was it helpful?

Solution

If you want to find angle between vectors (point-previous) and (point-now), use:

 Math.atan2((now.x - point.x)*(previous.y - point.y) - 
           (now.y - point.y)*(previous.x - point.x), 
           (now.x - point.x)*(now.y - point.y) - 
           (previous.x - point.x)*(previous.y - point.y))

(This is atan2(vectorproduct, scalarproduct))

You may also use

angle = atan2(now.y-point.y, now.y-point.y)-
        atan2(previous.y-point.y, previous.x-point.x);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top