3
\$\begingroup\$

I'm would like to achieve something like Object A will jump to position A in parabolic way. For example:

enter image description here

I am able to make the object jump but i do not know how to calculate to make the object jump to the precise location.

\$\endgroup\$
4
  • \$\begingroup\$ do you need real physics or fake physics ? \$\endgroup\$
    – Raxvan
    Commented Jul 30, 2014 at 14:01
  • \$\begingroup\$ What is the different between real physics and fake physics? which one is better? \$\endgroup\$
    – tekminewe
    Commented Jul 30, 2014 at 15:17
  • \$\begingroup\$ "real" physics use Newton's laws of motion and take into account gravity, mass, and other forces. "Fake" physics is just a function that you write out of your head that looks good, something like f(x) = sin( pow(x,0.5f) * Pi);. Consider having point A and B and you want to jump ObjectA from A to B => ObjectA.position = lerp(A,B,Time) + Vector.Up * f(Time) and Time is from 0 to 1. No approach is better than the other, "fake" physics can look better or worse , it all depends on your imagination \$\endgroup\$
    – Raxvan
    Commented Jul 30, 2014 at 15:35
  • \$\begingroup\$ Thank you for your explaination. Then how can I achieve that in real physics? \$\endgroup\$
    – tekminewe
    Commented Jul 31, 2014 at 5:29

1 Answer 1

2
\$\begingroup\$

SpriteKit uses SKAction objects to accomplish most of its functionality. What you're seeking is SKAction's followPath:duration: action.

The following accomplishes this:

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointZero];
    [path addQuadCurveToPoint:CGPointMake(deltaX/2, desiredHeight) controlPoint:CGPointMake(0, desiredHeight)];
    [path addQuadCurveToPoint:CGPointMake(deltaX, deltaY) controlPoint:CGPointMake(deltaX, desiredHeight)];
    CGPathRef pathref = path.CGPath;

    [jumpingNode runAction:[SKAction followPath:pathref duration:desiredDuration]];

This allows the Node to move along the specified path. It looks very uniform, but you can always add easing (related StackOverflow question) to give a more natural look

\$\endgroup\$
2
  • \$\begingroup\$ Thank you for your answer, it's really work as I want, but still not so smooth like jumping. Maybe still need some value tweaking \$\endgroup\$
    – tekminewe
    Commented Jul 31, 2014 at 5:31
  • \$\begingroup\$ Anytime. Re: smoothness, use the easing link I provided. Or create your own. You can theoretically do the same with the physics engine in SpriteKit, but this solution is much simpler, and likely just needs some easing to look realistic. What you might want to do is set up two paths (one with the first quadCurve, one with the second quadCurve) and use two different easing methods with both. \$\endgroup\$
    – Attackfarm
    Commented Jul 31, 2014 at 5:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .