Ben Wilson February 2016

Backwards movement jitery Sprit Kit

So i implemented some movement for a character in sprite kit and the forward motion works fine, nice and smooth but when i apply the opposite (-) force the the section for backwards movement it works but looks like there is still a small forward force pushing it which is making it jitter so looks ugly and not smooth,

Here is the code i am using for the all the forces including a gravity and jump (makes for better understanding)

 - (void)update:(NSTimeInterval)delta
{
  CGPoint gravity = CGPointMake(0.0, -450.0);
  CGPoint gravityStep = CGPointMultiplyScalar(gravity, delta);


  //FORWARD MOVE
  CGPoint forwardMove = CGPointMake(800.0, 0.0);
  CGPoint forwardMoveStep = CGPointMultiplyScalar(forwardMove, delta);

  //BACKMOVE
  CGPoint BackwardsMove = CGPointMake(-800.0, 0.0);
  CGPoint BackMoveStep = CGPointMultiplyScalar(BackwardsMove, delta);

  self.velocity = CGPointAdd(self.velocity, gravityStep);

  self.velocity = CGPointMake(self.velocity.x * 0.9, self.velocity.y);

  CGPoint jumpForce = CGPointMake(0.0, 310.0);
  float jumpCutoff = 150.0;

  if (self.mightAsWellJump && self.onGround) {
    self.velocity = CGPointAdd(self.velocity, jumpForce);
    [self runAction:[SKAction playSoundFileNamed:@"jump.wav" waitForCompletion:NO]];
  } else if (!self.mightAsWellJump && self.velocity.y > jumpCutoff) {
    self.velocity = CGPointMake(self.velocity.x, jumpCutoff);
  }

  if (self.forwardMarch) {

    self.velocity = CGPointAdd(self.velocity, forwardMoveStep);
    CGPoint minMovement = CGPointMake(0.0, -450);
    CGPoint maxMovement = CGPointMake(120.0, 250.0);

    self.velocity = CGPointMake(Clamp(self.velocity.x, minMovement.x, maxMovement.x), Clamp(self.velocity.y, minMovement.y, maxMovement.y));
  }
  if (self.BackwardsMarch) {


    self.velocity = CGPointSubtract(self.velocity, BackMoveStep);
    //self.velocity = CGPointAdd(self.velocity, BackMoveStep);
    CGPoint minMovement = CGPointMake(0.0, -450);
    CGPoint         

Answers


helloGo February 2016

My guess is that there is still forward velocity when you apply the backward velocity, and that is causing the jitters. When you want to apply the backward velocity first set self.velocity to zero. Then apply backward movement.


Ben Wilson February 2016

So, i figured it out and @Knight0fDragon was on the right track,

I amended the below code. And all is working correctly.

 if (self.velocity.x >0) {

  self.velocity = CGPointMake(self.velocity.x * 0.9, self.velocity.y);
  }
  else
  {
    self.velocity = CGPointMake(0.0, self.velocity.y);
    self.velocity = CGPointMake(self.velocity.x * 0.9, self.velocity.y);
  }

Post Status

Asked in February 2016
Viewed 2,328 times
Voted 5
Answered 2 times

Search




Leave an answer