2

I am trying to develop a simple game wherein I need to throw a bullet . I would like the ball follows the touch, or swipe direction.

However, I have a small problem that I have not had success in solving.

I take coordinates on the ACTION_DOWN of MotionEvent, and the coordinates on the ACTION_UP of MotionEvent, then calculate the straight line equation in order to move the ball according to this straight line.

The problem is that when I swipe in a direction close to "up" or "forward" or "in front of", the ball moves at lightning speed while in the other direction of more skewed (right or left) or diagonal, the ball moves at a normal speed.

Where is the flaw in my calculation?

Help me please, I am not far from the result but need to you for solve this problem!

The onTouch method:

public boolean onTouchEvent(MotionEvent event) {
    switch(event.getAction()) {
        case(MotionEvent.ACTION_DOWN):
            balle.setX(event.getX());
            balle.setY(event.getY());
            ya = event.getY();
            xa = event.getX();
            break;

        case(MotionEvent.ACTION_UP):
            xb = event.getX();
            yb = event.getY();
            balle.setMove(true);
            break;
    }

    return true;
}

This is my moveDirection method:

public void moveDirection(float xa, float ya, float xb, float yb) {
    if(!move) {
        return;
    }
    else {
        float m, b;
        float dx = xb-xa;

        m = (yb - ya) / (xb - xa);

        b = ya - (m * xa);

        if(dx > 0) {
            this.x += speedX;
        }
        else {
            this.x += -speedX;
        }

        this.y = (m * this.x + b);
    }
}

Thank you in advance!

Tomasz Dzieniak
  • 2,765
  • 3
  • 24
  • 42
  • When you move right or left, neither of those events is dispatched , so maybe you are loosing some interactions (that may cause the lower speed), try add an default option in the case, sorry for not help more, but i know very little about games in android :) – Lucas Queiroz Ribeiro Jun 19 '16 at 00:21
  • Where have I must add the option right or left ? In moveDirection function or in onTouch function ? – Charlie Tinlot Jun 19 '16 at 10:46
  • in onTouch, you are capturing just down and up movimentes, if you add a default option in your case you can capture other touch events. You can algo add an VelocityTracker to determine the velocity of the event, i will post a code with an example – Lucas Queiroz Ribeiro Jun 20 '16 at 13:09

1 Answers1

0

I see you are not capturing all the events so, maybe the code from the Android Documentation with de VelocityTracker api may help, in this example when you pull down your finger, a new tracker is created and when you move (in any direction) you capture the velocity of the event, i think you can move the ball according with the velocity and direction of touch.

public class MainActivity extends Activity {
private static final String DEBUG_TAG = "Velocity";
    ...
private VelocityTracker mVelocityTracker = null;
@Override
public boolean onTouchEvent(MotionEvent event) {
    int index = event.getActionIndex();
    int action = event.getActionMasked();
    int pointerId = event.getPointerId(index);

    switch(action) {
        case MotionEvent.ACTION_DOWN:
            if(mVelocityTracker == null) {
                // Retrieve a new VelocityTracker object to watch the velocity of a motion.
                mVelocityTracker = VelocityTracker.obtain();
            }
            else {
                // Reset the velocity tracker back to its initial state.
                mVelocityTracker.clear();
            }
            // Add a user's movement to the tracker.
            mVelocityTracker.addMovement(event);
            break;
        case MotionEvent.ACTION_MOVE:
            mVelocityTracker.addMovement(event);
            // When you want to determine the velocity, call 
            // computeCurrentVelocity(). Then call getXVelocity() 
            // and getYVelocity() to retrieve the velocity for each pointer ID. 
            mVelocityTracker.computeCurrentVelocity(1000);
            // Log velocity of pixels per second
            // Best practice to use VelocityTrackerCompat where possible.
            Log.d("", "X velocity: " + 
                    VelocityTrackerCompat.getXVelocity(mVelocityTracker, 
                    pointerId));
            Log.d("", "Y velocity: " + 
                    VelocityTrackerCompat.getYVelocity(mVelocityTracker,
                    pointerId));
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            // Return a VelocityTracker object back to be re-used by others.
            mVelocityTracker.recycle();
            break;
    }
    return true;
}
}

Link for doc.