Question

I'm creating a simple OpenGL 'app' to rotate a triangle. I wish, on the first touch, to save the angle the touch position corresponds to. Then, on motion, rotate the shape by the angle corresponding to current position minus angle of first touch.

It was my understanding that the first step should be done in MotionEvent.ACTION_DOWN, and the second in MotionEvent.ACTION_MOVE. However, it seems as if ACTION_DOWN is being called during the motion. That is, the below code causes the shape to rotate as a finger is dragged (and I understood that it would rotate only to the position of the initial touch):

private double mTheta;      

@Override
public boolean onTouchEvent(MotionEvent e) {

    super.onTouchEvent(e);

    float x = e.getX();
    float y = e.getY();

    switch (e.getAction()) {

    case MotionEvent.ACTION_DOWN:
        x -= getWidth() / 2;
        y -= getHeight() / 2;
        mTheta = Math.atan2(-x,-y) * 180.0f / Math.PI;
        GL20Renderer.mAngle = (float) mTheta;
        requestRender();
    }
    return true;
}

Is my code wrong, or is this some weird behaviour of the emulator? (I don't currently have access to an android device.)

(Addendum: I original attempted to implement the above fully, with a MotionEvent.ACTION_MOVE case for calculating the new angle and rendering. The ACTION_DOWN case was only saving the starting offset angle. This didn't work, in that the shape didn't rotate - because the offset angle was being re-calculated during movement - which is how I ended up at this point.)

Was it helpful?

Solution 2

Needed to be using getActionMasked() rather than getAction(). See comment from WarrenFaith.

OTHER TIPS

It might have been that you forgot to put a break statement in your switch/case. So once ACTION_MOVE is done, ACTION_DOWN follows immediately after

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top