3
\$\begingroup\$

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.

Tiki Taka Soccer ball shooting swipe mechanic

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.

\$\endgroup\$
3
\$\begingroup\$

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.

no curvature

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.

maximum right curvature

One that makes a 90° left turn has a maximum left curvature, or curvature value -1.

maximum left curvature

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!

-0.90 -0.65 -0.15 0.22 0.68

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

wobbly -0.08 wobbly 0.42

Of course, you might still get particularly insane swipes that make a really far-out turn, tighter than 90°.

curvature over 1

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

forces and impulses to apply to ball

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Stefan Doychev Oct 16 '15 at 12:07
  • \$\begingroup\$ @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. \$\endgroup\$ – Anko Oct 16 '15 at 12:46
  • \$\begingroup\$ 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? \$\endgroup\$ – Stefan Doychev Oct 16 '15 at 22:37
  • \$\begingroup\$ @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. \$\endgroup\$ – Anko Oct 16 '15 at 22:46
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Anko Oct 16 '15 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.