# How do I quantify the curvature of a touch swipe?

I'm implementing a touch controller for my game.

I am attempting to imitate Tiki Taka Soccer's "ball shooting" controls (gameplay video), where the curvature of the player's swipe affects the curve of the shot's trajectory. Basically, I want to move the object to where the user raised their finger, but also apply a curve to the movement, corresponding to the curve of the swipe.

How can I do this?

I'm using Unity 5, and I'll be using the Lean Touch plugin from the asset store to capture swipes.

## Detecting swipe curvature

Treat the finger swipe as a polyline. Approximate its curvature, and use that as a multiplier for how much to “curl” the resulting shot either left or right.

Let's say a swipe path has no curvature if it goes linearly from the start (the circle), to the end, or curvature value 0. I'll emphasise other swipes' differences to this baseline with blue and red colours.

One that makes a 90° right turn has a maximum right curvature, or curvature value 1. One that makes a 90° left turn has a maximum left curvature, or curvature value -1. We can hence find any intermediate curvature by calculating the proportion of the area it covers on either side of the baseline path, as a proportion of that maximum!

When the line curves partially to either side, just add the signed areas to get the overall curvature.

Of course, you might still get particularly insane swipes that make a really far-out turn, tighter than 90°. It's up to you how you handle these. Maybe you want to clamp them to the [-1, 1] range to prevent people from doing a Fire Tornado Spin Kick—but then again, maybe that would be fun!

## Applying curvature to object motion To apply a curve to an object's forward motion, apply an impulse in its intended direction (the red arrow to the right; to create the forward motion), then an impulse perpendicular to it (the red arrow upward; to push it "outward"), then apply a constant force perpendicular to it in the other direction (the green arrow downward; to pull it "in" again). For a motion curve in the other direction, just flip the perpendicular impulse and force directions.

You can find the perpendicular of a 2D vector by exchanging its x and y components and negating one of them. Here, you probably want to normalise the perpendicular vectors and multiply them by the curvature value.

Tweak the strength of the forces and impulses to get the effect you want.

• Thanks for the comment and all the explanations. Still I'm confused how to implement such a behavior in Unity - is it applying force to the ball, or a basic translation of the ball game object? Any help or comments will be really useful. – Stefan Doychev Oct 16 '15 at 12:07
• @Stefan I added a second section about that, taking the applying forces approach. It's possible also to compute the whole curve when the shot is taken and interpolate along it, applying basic translations, but that's harder and sounds like it might work better as a separate question. – Anko Oct 16 '15 at 12:46
• thanks so much for all the explanations and shared knoledge. I'm now trying to implement the controller, but I'm stuck with the perpendicular impulse (the red arrow upward). From what I read there is an undefinite number of such vectors, please correct me if I'm wrong. What will be the formula to calculate this vector? – Stefan Doychev Oct 16 '15 at 22:37
• @Stefan Assuming you have the vector u shown in black going from the start to the end of the stroke, the direction of the vector perpendicular to that is { x: u.y, y: -u.x }. You can divide it by its length to find the unit vector (called normalisation), which you can then multiply by the desired strength of the force. – Anko Oct 16 '15 at 22:46
• @Stefan Ah, I see. You are correct that a 3D vector has infinitely many perpendiculars. Unfortunately if your touch data is 2D, you'll need to choose the "correct" one by some different measure. (Perhaps just do the perpendicular calculation in 2D, then set the third component to some arbitrary constant? Up to you.) Generalising this to 3D sounds like it should be in a separate question though. – Anko Oct 16 '15 at 22:58