Light Racer 2.0 - Day 31 - Anywhere Touch Swipe Control

I had previously written a little app that I was going to call "Fingerpaint" but I never released it because there already seemed to be other apps on the market that did that better. I did get it working and I'm glad I did because it made me very familiar with how to process sliding touches. I copied the code out of that sketch app and modified it to process touches for Light Racer. Now the user can touch anywhere on the screen and swipe in the direction they'd like the racer to go.

Day 31 - What was done:

I added anywhere touch swipe control.

Essentially what the code does is when the user swipes their finger, I turn the line segment from the start to the end in to a vector and then look at the angle of the vector. If the vector up is 0 degrees and down is 180 with the right half being positive and the left half being negative then from -45 to 45 degrees would be up, 45 to 135 would be right, 135 to 180,-180 to -135 is down and -135 to -45 is left. This worked very well. You can swipe anywhere you want on the screen as long or short as you like and it can be a little off-angle. So long as it's mostly in the right direction, the code will understand.

So how does one get a vector from two points? Easy! Say x1 and y1 are the starting points and x2 and y2 are the ending points.

First, to determine if this touch should be accepted, we need to set a certain threshold to get rid of over sensitivity.

int distance = Math.abs(x2 - x1) + Math.abs(y2 - y1);
if (distance > TOUCH_SENSITIVITY) {
float angleR = LightRacerUtil.fastatan2(x2 - x1, y2 - y1);
// process the angle, apply as input

That distance is just a simple manhattan distance. It is the total number of x+y points to get from the start to finish. It may make more sense to use euclidean distance but this is a little faster and works fine. Then I just use my fast atan2 to get the angle of the vector. My fast atan2 is about 50% faster than the regular one but is also much less accurate. It can be off by as much as 4 degrees. In all of my testing though, I didn't have any problems with the touch even with that margin of error so I stuck with it.

It's still better to use the trackball but I think users will enjoy being able to touch control, especially when it works well like this.