How to simulate Gravity in z-axis in a 2D game with Sprite Kit

我与影子孤独终老i 提交于 2020-02-07 07:26:34

问题


I'm writing a 2D ball game with sprite kit on iOS 7 and currently struggling on one physic simulation.

To explain the expected behavior: if a ball is dropped into a tea cup, it will circle around, loosing speed and finally stand still in the center of the cup.

I've tried to archive this with gravity, but gravity in sprite kit only applies to vertical X and Y axis, not Z-axis. I also tried to use level gravity by switching gravity values with small physic bodies on beginContact depending on the current ball position in the tea cup. But some contacts are dropped and the result is far away to look realistic.

I think I need to solve this in the update: method, but I have no idea which way to go. Any advice greatly welcome and I need to mention that I'm not an expert on math, please explain your path to go. :-)


回答1:


Since there's no built-in support for this kind of behavior in SpriteKit, rather than trying to hack existing functions to get what you want, you're probably better off integrating some published 2D physics formulas in your x,y 2D world. I would think that something like simulating magnetic or a homing behavior might be right for this.

A simple example would be something like (in the scene's -update: method):

 CGFloat strength = 0.5; //(some scaling value)
 CGPoint ballLocation = ball.position;
 CGPoint cupLocation = cup.position;
 [ball.physicsBody applyForce:CGVectorMake((cupLocation.x - ballLocation.x) * strength, 
                                           (cupLocation.y - ballLocation.y) * strength)];



回答2:


following Joshd great idea, I have created an NSArray with like explained in my comment above. Hope this snippets does help some others...

The result could be found on youtube: http://youtu.be/Uephg94UH30 Sorry for the bad Airplay frame rate, it runs perfectly smooth on my iPad

The -update: functions does the work but only triggered if _meditationIsActive. This bool is set in -didBeginContact: when any ball gets in contact with a hole.

   if (_lastCheck > 0.005)
   {
      if (_meditationIsActive)
      {
         CGFloat strength = 0.1; //(some scaling value)
         CGPoint ballLocation;
         CGPoint holeLocation;

         for (MeditationHole * holeObj in _meditationHoles)
         {
            if (holeObj.connectedMeditationBall != nil)
            {
               ballLocation = holeObj.connectedMeditationBall.position;
               holeLocation = holeObj.position;
               [holeObj.connectedMeditationBall.physicsBody applyForce:CGVectorMake(
                                                                                    (holeLocation.x - ballLocation.x) * strength, (holeLocation.y - ballLocation.y) * strength)];
            }
         }
         _meditationIsActive = [self doesMeditationApplies];
      }
      _lastCheck = 0;
   }

At the end I'm checking if there is a valid ball out of the array in contact with a hole to avoid checking during every update. This is done with the following function where position check +/- 48 detects a ball close to a hole and +/-1 ball stands still

- (bool)doesMeditationApplies
{
   bool isInArea = NO;
   int perfectMatchCount = 0;
   for (MeditationHole * holeObj in _meditationHoles)
   {
      if (holeObj)
      {
         if (holeObj.connectedMeditationBall != nil)
         {
            MeditationBall * ballObj = holeObj.connectedMeditationBall;
            if ((ballObj.position.x >= holeObj.position.x - 48) &&
                (ballObj.position.x <= holeObj.position.x + 48) &&
                (ballObj.position.y >= holeObj.position.y - 48) &&
                (ballObj.position.y <= holeObj.position.y + 48))
            {
               isInArea = YES;
            }
            else
            {
               holeObj.connectedMeditationBall = nil;
            }
            if ((ballObj.position.x >= holeObj.position.x - 1) &&
                (ballObj.position.x <= holeObj.position.x + 1) &&
                (ballObj.position.y >= holeObj.position.y - 1) &&
                (ballObj.position.y <= holeObj.position.y + 1))
            {
               perfectMatchCount++;
               isInArea = YES;
            }
         }
      }
   }

   if (perfectMatchCount == _oxydStonesMax)
   {
      if (_sound)
      {
         self.pauseMusicPlaybackBlock(YES);
         NSLog(@"PlaySound Meditation");
         [OxydScene PlaySystemSound:@"Win2"];
      }

      isInArea = NO;

      [self showPauseScreenWithWin:YES andPauseOnly:NO];
   }

   return isInArea;
}


来源:https://stackoverflow.com/questions/21228604/how-to-simulate-gravity-in-z-axis-in-a-2d-game-with-sprite-kit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!