Saturday, November 23, 2019

unity - 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.


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.



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...