【问题标题】:Sprite kit ball bounce physics objective c雪碧套件球反弹物理目标 c
【发布时间】:2015-12-21 16:33:12
【问题描述】:

我想给球添加重力,所以它会上下弹跳,稍微向右或向左弹跳到哪个角度,我不想失去球的能量,所以它总是在相同的高度弹跳.现在,即使我将摩擦力设置为 0,球也会在各个方向反弹并在几分钟后停止。这里有一点我的代码:

    ball.position = CGPointMake(self.frame.size.width/4 +arc4random() % ((int)self.frame.size.width/2),
                                self.frame.size.height/2 +arc4random() % ((int)self.frame.size.height/2));
    [self addChild:ball];

    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/3];
    ball.physicsBody.friction = 0.0f;
    ball.physicsBody.restitution = 1.0f;
    ball.physicsBody.linearDamping = 0.0f;
    ball.physicsBody.allowsRotation = YES;
    [ball.physicsBody applyImpulse:CGVectorMake(50.0f, -50.0f)]; 

【问题讨论】:

  • 这取决于你的冲动,现在你正在向克罗斯方向打球。所以你是在横向击球

标签: ios objective-c game-physics skphysicsbody


【解决方案1】:

如果你不想让球向各个方向反弹,那么你应该直接击球。

如果你想在整个游戏中保持恒定的速度,那么你需要确保你保持速度:

  if(myNode.physicsBody.velocity.dx < 30)
myNode.physicsBody.velocity = CGVectorMake(30, myNode.physicsBody.velocity.dy);

这适用于速度 30 ,当您的节点速度小于 30 时

你可以用这个方法:

-(void)addBallOfType:(NSString *)imageName ofSize:(CGSize) size addSpriteName:(NSString *)name atPoint:(CGPoint)point withBounce:(BOOL) bounce withVelocity:(CGVector)velocity andHitCategory:(int) category addEmitter:(BOOL) addEmitter{


    SKSpriteNode * ball = [SKSpriteNode spriteNodeWithImageNamed:imageName];
    ball.name=name;
    ball.size=size;
    ball.anchorPoint=CGPointMake(0, 0);

    ball.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/2];
    ball.physicsBody.categoryBitMask=category;
    ball.physicsBody.collisionBitMask=youCollisionBitMask;
    ball.physicsBody.contactTestBitMask= youCollisionBitMask;

    ball.physicsBody.dynamic=YES;
    ball.physicsBody.affectedByGravity=NO;
    ball.physicsBody.restitution=1.0;
    ball.physicsBody.friction=0.0;
    ball.physicsBody.linearDamping=0.0;
    ball.physicsBody.angularDamping=0.0;

    ball.position=point;
    [self addChild:ball];

     if (addEmitter) {
       _emitterNodePath =
        [[NSBundle mainBundle] pathForResource:@"RParticle" ofType:@"sks"];
       SKEmitterNode *burstEmitter =
    [NSKeyedUnarchiver unarchiveObjectWithFile:_emitterNodePath];
    [ball addChild:burstEmitter];
    }



    if (bounce) {
        CGVector impulse = velocity;
        [ball.physicsBody applyImpulse:impulse];

    }


}

针对不同方向的击球:

-(CGVector)hitToLeftWithSpeed:(float)speed{

    return CGVectorMake(-speed, 0);
}

-(CGVector)hitToRightWithSpeed:(float)speed{

    return CGVectorMake(speed, 0);
}

-(CGVector)hitTotopWithSpeed:(float)speed{

    return CGVectorMake(0, speed);
}

-(CGVector)hitToBottomWithSpeed:(float)speed{

    return CGVectorMake(0, -speed);
}

-(CGVector)forwardCrossHitWithSpeed:(float)speed{

    return CGVectorMake(speed, speed);
}

-(CGVector)backwardCrossHitWithSpeed:(float)speed{

    return CGVectorMake(-speed, -speed);
}

定位球:

  -(CGPoint)addBallAtCenterWithOffsetX:(int)x withOffsetY:(int)y{

    return CGPointMake(100+x, 100+y);
}
-(CGPoint)addBallAtTopLeftWithOffsetX:(int) x withOffsetY:(int)y{

    return CGPointMake(200+x, 100+y);
}

-(CGPoint)addBallAtTopRightWithOffsetX:(int)x withOffsetY:(int)y{

    return CGPointMake(100+x, 100+y);
}

-(CGPoint)addBallAtBottomRightWithOffsetX:(int)x withOffsetY:(int)y{

    return CGPointMake(100+x, 2100+y);
}

-(CGPoint)addBallAtBottomLeftWithOffsetX:(int)x withOffsetY:(int)y{

    return CGPointMake(100+x, 200+y);
}

您可以通过以下方式调用它:

 [self addBallOfType:hBlueBall ofSize:CGSizeMake(75, 75) addSpriteName:@"greenball" atPoint:CGPointMake(100, 100) withBounce:YES withVelocity:[self hitTotopWithSpeed:50] andHitCategory:blueBallHitCategory addEmitter:YES];

【讨论】:

  • 有更简单的方法吗?我的意思是,我不明白这段代码的一半
  • 您不需要复制粘贴所有这些代码。我发布了辅助方法,以便您可以参考这些方法。
  • 你不明白哪一部分?我会尽量详细解释
  • 我不懂位掩码的东西,我读过,但不明白
  • 位掩码用于检测节点之间的冲突。如果您的游戏包含 ball、wall、bat,则每个都有不同的位掩码。你可以暂时忽略它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多