manuzhang
manuzhang

Reputation: 3025

touch interaction in openGLES

I've been exploring into Nim Game on Android. The players are going to take objects from heaps. I use openGLES to draw the objects and heaps. Where I'm stuck is how to "take".
As the samples shown on official dev guide , I can override onTouchEvent method in the class that extends GLSurfaceView for the interaction. However, how I could tell where the objects have been drawn? Or are there any objects at the coordinates where I touch?
Any ideas?
Thx in advance!

Upvotes: 1

Views: 440

Answers (2)

Patt Mehta
Patt Mehta

Reputation: 4204

public class Main extends Activity implements OnTouchListener {
public boolean onTouch(View v, MotionEvent event) {
    synchronized (this) {
        if (!_isPaused) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                _touchedX               = event.getX();
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                float touchedX          = event.getX();
                float dx                = Math.abs(_touchedX - touchedX);
                _dxLowPassed            = lowPass(dx, _dxLowPassed);

                switch (_screenUsage) {
                    case HALF_SCREEN:
                        if (touchedX < _width / 2) {
                            if(touchedX < _touchedX) {
                                _zAngle                 = (2 * _dxLowPassed / _width) * TOUCH_SENSITIVITY * ANGLE_SPAN;
                                _zAngleLowPassed        = lowPass(_zAngle, _zAngleLowPassed);
                                GLES20Renderer._zAngle  = GLES20Renderer._zAngle + _zAngleLowPassed;
                            }
                        } else {
                            if( touchedX > _touchedX ) {
                                _zAngle                 = (2 * _dxLowPassed / _width) * TOUCH_SENSITIVITY * ANGLE_SPAN;
                                _zAngleLowPassed        = lowPass(_zAngle, _zAngleLowPassed);
                                GLES20Renderer._zAngle  = GLES20Renderer._zAngle - _zAngleLowPassed;
                            }
                        }
                        Log.d("TOUCH", new Float(_zAngleLowPassed).toString());
                    break;

Upvotes: 1

alexc
alexc

Reputation: 1687

If I'm understanding your question correctly, it sounds like you want to do some simple collision detection to see if your touch point is inside one of the objects on the heap. You can do this with some basic math between the coordinates of the touch point and the center coordinates which you used to draw the object.

For instance, assuming your objects are rectangles, this would be the general idea:

boolean detectCollision(Object object, TouchPoint touch) {
      return object.x - object.width/2 <= touch.x &&
             object.x + object.width/2 >= touch.x &&
             object.y - object.height/2 <= touch.y &&
             object.y + object.height/2 >= touch y;
}

You could then iterate through all of the objects in your heaps and if this returns true for any of them, then you know your touchpoint is inside of that object and can proceed to call whatever you need to call on it.

Keep in mind that the touch coordinates the system gives you will be screen coordinates, so you have to take into account any discrepancies between the screen coordinate system and the coordinate system you defined with your view frustum.

Upvotes: 1

Related Questions