2

I really hope someone understands what the effect is that I am asking for. Have you ever played the games where a character is sliding one way and when you try and change the characters direction it is not immediate as they need to slow down in the initial direction before they can start sliding the other way? The new game on the App Store 'Swing Copters' by the maker of Flappy Bird is exactly the effect I am talking about. Can someone please help me create this effect in SpriteKit. I have already tried achieving it by applying different forces but I am either not doing it correctly or the effect isn't possible with forces.

Thanks in advance!

Burkhard
  • 14,596
  • 22
  • 87
  • 108
Mike Simz
  • 3,976
  • 4
  • 26
  • 43
  • 2
    This is called "inertia". – ceejayoz Sep 02 '14 at 19:33
  • @ceejayoz how do i emulate this in sprite kit..using physics? – Mike Simz Sep 02 '14 at 19:35
  • 2
    If the node is going one way, and you keep applying an impulse in the opposite direction, the node will slow down and eventually speed up again. How much impulse you apply determines how quickly the slowdown and speedup effect is, use a low impulse for ice and one that's a couple times more for other surfaces. – CodeSmile Sep 02 '14 at 19:39
  • @LearnCocos2D Mr. Itterheim you are the man..your learningcocos2d book is what started me on this game-dev path! Thanks for your help..quick question, how can I set a max-speed, because I certainly don't want my character moving too fast – Mike Simz Sep 02 '14 at 19:42
  • 1
    the usual way would be to enumerate all bodies at the end of the update: method or in didSimulatePhysics and cap their velocity to a given length (speed). In principle: get length of velocity vector, if length > maxSpeed then normalize velocity and multiply with maxSpeed, assign the result to velocity. GLKit has a bunch of vector math functions to simplify this: https://developer.apple.com/Library/ios/documentation/GLkit/Reference/GLKVector2/Reference/reference.html – CodeSmile Sep 02 '14 at 19:50

1 Answers1

0

Maybe you want to try working with some acceleration-stuff.

Let's think about the gravity on the earth (a = 9.81 metres/s²). If you throw a ball straight to the sky, the Ball is starting with a velocity of: lets say 5metres per second(positive velocity). After a short time, the gravity is pulling the velocity of the ball down to 0, so the ball can't get any higher. Right after this, the gravity pulls the ball down until it hits the ground. (Negative velocity)

If we're talking about this in a game, where the ball doesn't move up or down but from left to right, you can use something like this. The moment when you throw the ball, is the moment in the game where you send the command to change the direction. The ball keeps going in the direction where it used to go, gets slower, stops, changes the direction and finally gets faster and faster until you send another command to change the direction again(Or it hits the Wall/the ground). In that case you have to inverse the acceleration you want to use, so the whole thing repeats in the other way.

If the ball should move to the right, positive acceleration, if the ball should move to the left, negative acceleration.

As formula you can use something like

v = (int) (a * t + v0);
v0 = v;

v is the next velocity, a is the acceleration you want to use, t is the spent time and v0 is the current velocity. t should count the nano-time since the last direction-change. (int) casts the whole thing to an integer, so you can use this directly to move the ball/graphics on the screen.

Repeat this each frame.

For the direction change you can use

t = 0;
a = a * (-1);

t has to be 0 again, otherwise it gets buggy. I hope this was helpful.

Officer Bacon
  • 724
  • 1
  • 7
  • 22