Jump to content
  • Advertisement
Sign in to follow this  
ClaF

Getting curve of a swipe

This topic is 2711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey,
I came across a soccer game on the iphone. You can see a demo of it here. The curvature of the ball is based on the curvature of the user's finger swipe. How would this be programmed? How would I get the 'steepness' of the finger curve? I'm not great at maths, so any help would be appreciated. thanks.

Share this post


Link to post
Share on other sites
Advertisement
Are you looking for just a start and end position to determine the slope, or are you expecting the player to actually form a curve with their finger?

If you're just looking for a simple line, get the start and end positions of their finger and use the standard slope formula slope = (y[sub]2[/sub] - y[sub]1[/sub])/(x[sub]2[/sub] - x[sub]1[/sub]). From the demo video, it appears to be what the user does. Once you have the equation for the line using the slope, determine a vector and remember to normalize it. After that, you can use the dot product and the formula here (http://www.ltcconline.net/greenl/courses/107/vectors/dotcros.htm) to find the angle between the vector of the swipe, and the vector of the y axis (0,1) to determine the angle at which to launch the ball.

Alternatively, you could just apply your vector directly if you have some kind of apply impulse function that takes a 2D vector, and then adjust the magnitude of the vector based on how far and how fast the user moved their finger.

Share this post


Link to post
Share on other sites

Are you looking for just a start and end position to determine the slope, or are you expecting the player to actually form a curve with their finger?

If you're just looking for a simple line, get the start and end positions of their finger and use the standard slope formula slope = (y[sub]2[/sub] - y[sub]1[/sub])/(x[sub]2[/sub] - x[sub]1[/sub]). From the demo video, it appears to be what the user does. Once you have the equation for the line using the slope, determine a vector and remember to normalize it. After that, you can use the dot product and the formula here (http://www.ltcconlin...ors/dotcros.htm) to find the angle between the vector of the swipe, and the vector of the y axis (0,1) to determine the angle at which to launch the ball.

Alternatively, you could just apply your vector directly if you have some kind of apply impulse function that takes a 2D vector, and then adjust the magnitude of the vector based on how far and how fast the user moved their finger.



Thanks for the reply! Your method makes perfect sense. I was initially (somehow) trying to find the angle of the curve against the y axis vector, but I wasn't sure at which point of the curve I should use. Your normalization of the slope idea solves that issue. Thanks a lot for the help!

edit: If the user was to create a curve with their finger, would I use the same method? (i.e. taking start and end point of curve)?

Share this post


Link to post
Share on other sites
I don't know of any methods to determine an equation directly from a finger trace on a touch screen, but I can think of a way to hack it. What I would do is take several samples of the curve the finger traces between when they touch and release. The more samples you take, the smoother your representation will be. You can then find the tangent line to each sample using the sample before and after it, and use that tangent to apply force to the ball.

Share this post


Link to post
Share on other sites

If you're just looking for a simple line, get the start and end positions of their finger and use the standard slope formula slope = (y[sub]2[/sub] - y[sub]1[/sub])/(x[sub]2[/sub] - x[sub]1[/sub]).

Minor point, but the 'slope' form for lines is almost never used in game development, and should generally be avoided (IMO). There's almost always (maybe always) another way to express the problem that won't suffer from the numerical and representational problems the slope form does.

Share this post


Link to post
Share on other sites
You will get several events indicating that the finger has moved. Store them all and, once you get the event indicating the finger is no longer touching the screen, you can try to fit a parametric curve to the data. For instance, you can fit a circular arc, and measure the curvature of the fitted curve.

I didn't find the game in the video particularly convincing: The curvature of the trajectory of the ball is caused by the spinning of the ball, but they didn't seem to have modeled this at all.

Share this post


Link to post
Share on other sites

You will get several events indicating that the finger has moved. Store them all and, once you get the event indicating the finger is no longer touching the screen, you can try to fit a parametric curve to the data. For instance, you can fit a circular arc, and measure the curvature of the fitted curve.

I didn't find the game in the video particularly convincing: The curvature of the trajectory of the ball is caused by the spinning of the ball, but they didn't seem to have modeled this at all.



How would I go about fitting a circular arc to a group of points? Are there any algorithms I should read up, as I assume a numerical approach would be inefficient in a game.



Thanks for all the suggestions.

Share this post


Link to post
Share on other sites
Yes there are. On Eberly's site you can find some papers and source code, e.g. here and here. Look out for "least squares fitting". This should give you a start.

Not sure what you mean by "numerical approach would be inefficient". As soon as you are calculating anything with floating point numbers (floats, doubles), well that is "numerical". That's just how computers work. The opposite being symbolic calculation (like e.g. Mathematica). You hardly need the latter for a real-time a scenario, at least not in this case. Or did you mean an iterative approach ?

Anyway: Don't be concerned about efficiency now, in the example shown I bet the costs for calculating the curve is rather ridiculous compared to all the other stuff going on (animation, rendering). Also, it does not need to be done every frame, just once.

Additional notes to the suggested links: I suspect you can get problems trying to fit a circular arc if you have points on a straight line: The circle's radius can go to infinity then.

Suggestion for another - even simpler - approach: Take the first, last and one intermediate point (the latter being some average of neighbouring points) and fit a curve using interpolation instead.

Share this post


Link to post
Share on other sites
Thanks unbird. I think I can use the interpolation technique. On a side note, I meant symbolic. I figured if I opened up a text book and followed the 'standard' mathematical techniques, it wouldn't be efficient in a game. An approximation would be preferable such as the interpolation technique.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!