I am playing with Android sample app Snake. The code for the original snake-move direction math is below. It is very simple. The direction is determined by which quadrant the touch is in based on the screen center. However this is rough. Sometime the snake is at right edge of the screen and I want to move it left but if my touch is at left of the snake but still within the right quadrant, its direction is still right. So I need an updated direction math centered with Snake head rather than the center of the screen. I was not successful making such update. Someone is good at math please help. Please note the 4 quadrants are divided by 2 diagonal lines.
@Override
public boolean onTouch(View v, MotionEvent event) {
if (mSnakeView.getGameState() == SnakeView.RUNNING) {
// Normalize x,y between 0 and 1
float x = event.getX() / v.getWidth();
float y = event.getY() / v.getHeight();
// Direction will be [0,1,2,3] depending on quadrant
int direction = 0;
direction = (x > y) ? 1 : 0;
direction |= (x > 1 - y) ? 2 : 0;
// Direction is same as the quadrant which was clicked
mSnakeView.moveSnake(direction);
} else {
// If the game is not running then on touching any part of the screen
// we start the game by sending MOVE_UP signal to SnakeView
mSnakeView.moveSnake(MOVE_UP);
}
return false;
}