How to identify direction of a swipe (android)

Started by urubu, June 04, 2012, 03:47:01 AM

Previous topic - Next topic

urubu

Is it possible to identify the direction of a swipe in relation to an object?
I have attached an image to help clarify what I need. I have a cube and I would like to be able to identify which edge of the cube the swipe move is targeting.
I am currently detecting the 2d coordinates of first and last touch on the screen but I don;t know how to translate that to local coordinates of the cube.
I would think that if I have the two points in local coordinates I could infer the direction of the movement.
Any thoughts?

[attachment deleted by admin]

urubu

I guess I figured that out mtself:

public int calcFlingDirection(PointF start, PointF finish,
                                     FrameBuffer fb, Object3D selCube, World world) {
        Camera cam=world.getCamera();
        // Convert start into a direction vector in world space:
        SimpleVector direction1 = new SimpleVector( Interact2D.reproject2D3DWS(
                                                       cam, fb, (int)start.x, (int) start.y ) ).normalize();
        // Convert finish into a direction vector in world space:
        SimpleVector direction2 = new SimpleVector( Interact2D.reproject2D3DWS(
                                                       cam, fb,(int) finish.x, (int) finish.y ) ).normalize();

        // Calculate the distance to whatever was clicked on:
        SimpleVector pos=cam.getCamera().getPosition();
     
        float distance1 = world.calcMinDistance( pos, direction1, 10000 );
       
        float distance2 = world.calcMinDistance( pos, direction2, 10000 );

        // Calculate the 3D coordinates for the point that was clicked in world coordinates:
        SimpleVector collisionPoint1 = new SimpleVector( direction1 );
        collisionPoint1.scalarMul( distance1 );
        collisionPoint1.add( cam.getCamera().getPosition() );
        collisionPoint1.matMul(selCube.getInverseWorldTransformation());
       
        SimpleVector collisionPoint2 = new SimpleVector( direction2 );
        collisionPoint2.scalarMul( distance2 );
        collisionPoint2.add( cam.getCamera().getPosition() );
        collisionPoint2.matMul(selCube.getInverseWorldTransformation());
        collisionPoint2=round(collisionPoint2);

        SimpleVector diff=new SimpleVector(collisionPoint2);       
        diff.calcSub(collisionPoint1);
        int res='x';
        if (diff.x<0)
        res=-res;
       
        float x2=(diff.x>0 ? diff.x : - diff.x);
        float y2=(diff.y>0 ? diff.y : - diff.y);
        float z2=(diff.z>0 ? diff.z : - diff.z);

        if (y2>x2) {
        res='y';
        if (diff.y<0)
        res=-res;
        }
        if ( (z2>y2) && (z2>x2)) {
        res='z';
        if (diff.z<0)
        res=-res;
        }
        return res;
}



The above code returns 'x',-'x','y',-'y','z',-'z' in local coordinates (cube coordinates)
This is working fine on my tests. Please notice that both points MUST collide with the cube surface.

EgonOlsen

...Opps, sorry for not answering this post...i somehow missed it. But you've figured it out, so... ;D

urubu

does not work reliably though.
Sometimes  I get strange big numbers for the coordinates.
Before I call that method I check for collision with the object. To do that I have to offset the y position 77 pixels (don't know why, got it by experimentation). May be that is confusing the calculations.