johnnysports
johnnysports

Reputation: 61

iOS - Dragging objects along curved paths

I am tearing my hair out trying to figure out what seems to be a very easy problem. I know a lot of this stuff has been talked about tangentially, so apologies if this treads on well-covered ground, but I can't find anything specific to my solution (believe me, I've looked).

Basically I want to drag an object/sprite along a pre-defined, curved path (not just move it, but DRAG IT). Think of the iPhone's "Slide to unlock" thing, but instead of just dragging the slider left-to-right, make the path an arc or a wavy line.

My basic thinking was:

None of this is trivial (at least, that's how it seems). For example:

Am I making this too hard? It doesn't seem that complicated. I don't need a whole solution, just a new way to think about this and kick in the right direction. Any help would be really appreciated.

Upvotes: 6

Views: 1980

Answers (2)

Andrew Pouliot
Andrew Pouliot

Reputation: 5421

Yes, you are making this too hard.

Take the simplification suggested above (or along a circle, line, etc) if it works for, or if you really want to do it against a bézier curve, consider the following:

  1. Look at the definition of the bézier curve
  2. What you're looking for is to define a new object position P' from a current position P and a change in touch position D.
  3. If you rephrase the original P(x,y) in terms of t (bézier curves are parametric), then the problem becomes finding how much t offset to add based on D.
  4. Something involving the differential of the bezier fn at P might be a good way to do that. Ie, how much t would have been added had the curve just been a straight line coming from point P along the curve.

EDIT: Transition between segments: If each segment has t in [0,1), then you can detect t >= 1 and move on to the next segment, setting P to the end of the previous segment, and evaluating the movement again in relation to that point. There might have to be some heuristics involved if you have a lot of small points, etc.

Upvotes: 0

arclight
arclight

Reputation: 5310

How about this?

  • Consider the X Axis of your bezier path.
  • Each time the user taps or interacts with the screen just look at the x portion of the touch
  • Map that X Coordinate with your path and move the object to the right position.

Upvotes: 2

Related Questions