iOS 7.x和iOS 8.x上的Spritekit物理不同?

时间:2015-04-20 08:38:54

标签: ios objective-c sprite-kit skphysicsbody

为什么精灵套件物理在iOS 7.x与iOS 8.x上不同?

我有一个简单的spritekit场景......我有2个静态引脚,在它之间我有精灵通过弹簧接头连接,在弹性绳上放下一个物体就像蹦床一样,一切都在iOS上看起来不错7.x直到我开始在iOS 8.x上测试。我已经在类似设备上测试过,2个iphone 5s和2个ipod touch第5代,每个7& 8,我得到相同的fps,具有相同数量的节点。

我尝试了各种关节,因素,重力,速度以及每次在7.x上的不同设备上运行这段简单的代码我得到了类似的行为,但如果我在8.x设备上运行它'弹性'不同的弹簧接头是不同的,在7.x上更硬,弹性会在8.x上弯曲更多而没有任何东西但是如果你在它上面添加几个球,你甚至可以看到对8.x设备的更大影响。在我问之前,这里是skcene代码

#define kFrequency 30.0f
#define kDamping 0.0f
#define kNodes 20

typedef NS_OPTIONS(uint32_t, CategoryMask)
{
    CategoryMaskBall = 1 << 0,
    CategoryMaskPin = 1 << 1,
    CategoryMaskElastic = 1 << 2,
    CategoryMaskBorder = 1 << 3
};

@implementation SimpleGameScene

-(void)didMoveToView:(SKView *)view {
    self.backgroundColor = [SKColor  blackColor];
    self.physicsWorld.gravity = CGVectorMake(0.0, -4.9);
    NSMutableArray *nodes=[NSMutableArray arrayWithCapacity:kNodes+2];
    SKSpriteNode *pin = [self addPin:CGPointMake(self.size.width/2, self.size.height/4)];
    [self addChild:pin];
    [nodes addObject:pin];

    for( int i = 1; i<=kNodes; i++) {
        SKSpriteNode *elastic = [self addElastic:CGPointMake(self.size.width/2+i*(pin.size.width+1), self.size.height/4)];
        [self addChild:elastic];
        [nodes addObject:elastic];
    }
    SKSpriteNode *pin2 = [self addPin:CGPointMake(self.size.width/2+(kNodes+1)*(pin.size.width+1), self.size.height/4)];
    [self addChild:pin2];
    [nodes addObject:pin2];

    for( int i = 1; i<=kNodes+1; i++) {
        SKSpriteNode *nodeA = [nodes objectAtIndex:i-1];
        SKSpriteNode *nodeB = [nodes objectAtIndex:i];
        [self addSpringJointFrom:nodeA to:nodeB];
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        SKSpriteNode *ball = [self spawnBallAt:[touch locationInNode:self]];
        [self addChild:ball];
    }
}

-(SKSpriteNode*) spawnBallAt:(CGPoint)location
{
    SKSpriteNode *ball = [SKSpriteNode spriteNodeWithTexture:[[SKTextureAtlas atlasNamed:@"game"] textureNamed:@"circle"]];
    ball.position = CGPointMake( location.x, location.y);
    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.size.width/2];

    ball.physicsBody.dynamic = YES;
    ball.physicsBody.affectedByGravity = YES;
    ball.physicsBody.mass = 40.0f;

    ball.physicsBody.categoryBitMask = CategoryMaskBall;
    ball.physicsBody.collisionBitMask = CategoryMaskElastic ;
    ball.physicsBody.contactTestBitMask =  0;
    ball.name = @"ball";

    return ball;
}

-(SKSpriteNode*) addPin:(CGPoint) location {
    SKSpriteNode *pin = [SKSpriteNode spriteNodeWithTexture:[[SKTextureAtlas atlasNamed:@"game"] textureNamed:@"pin"]];
    pin.position = location;
    pin.zPosition = 1;
    CGFloat offsetX = pin.size.width * pin.anchorPoint.x;
    CGFloat offsetY = pin.size.height * pin.anchorPoint.y;

    CGMutablePathRef path = CGPathCreateMutable();
    CGFloat side = pin.size.width;
    CGPathMoveToPoint(path, NULL, 0 - offsetX, side - offsetY);
    CGPathAddLineToPoint(path, NULL, side - offsetX, side - offsetY);
    CGPathAddLineToPoint(path, NULL, side - offsetX, 0 - offsetY);
    CGPathAddLineToPoint(path, NULL, 0 - offsetX, 0 - offsetY);
    CGPathCloseSubpath(path);

    pin.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
    pin.physicsBody.affectedByGravity = NO;
    pin.physicsBody.dynamic = NO;

    pin.physicsBody.categoryBitMask = CategoryMaskPin;
    pin.physicsBody.collisionBitMask = 0;
    pin.physicsBody.contactTestBitMask =  0;

    CGPathRelease(path);

    return pin;
}

-(SKSpriteNode*) addElastic:(CGPoint) location
{
    SKSpriteNode *elastic = [SKSpriteNode spriteNodeWithTexture:[[SKTextureAtlas atlasNamed:@"game"] textureNamed:@"elastic"]];
    elastic.position = location;
    elastic.zPosition = 1;

    CGFloat offsetX = elastic.frame.size.width * elastic.anchorPoint.x;
    CGFloat offsetY = elastic.frame.size.height * elastic.anchorPoint.y;

    CGMutablePathRef path = CGPathCreateMutable();
    CGFloat side = elastic.size.width;
    CGPathMoveToPoint(path, NULL, 0 - offsetX, side - offsetY);
    CGPathAddLineToPoint(path, NULL, side - offsetX, side - offsetY);
    CGPathAddLineToPoint(path, NULL, side - offsetX, 0 - offsetY);
    CGPathAddLineToPoint(path, NULL, 0 - offsetX, 0 - offsetY);
    CGPathCloseSubpath(path);

    elastic.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
    elastic.physicsBody.affectedByGravity = YES;
    elastic.physicsBody.allowsRotation = NO;
    elastic.physicsBody.dynamic = YES;
    elastic.physicsBody.mass = 1.0f;

    elastic.physicsBody.categoryBitMask = CategoryMaskElastic;
    elastic.physicsBody.collisionBitMask = CategoryMaskBall;
    elastic.physicsBody.contactTestBitMask =  0;

    CGPathRelease(path);

    return elastic;
}

- (void) addSpringJointFrom:(SKSpriteNode*) nodeA to:(SKSpriteNode*) nodeB {
    SKPhysicsJointSpring *joint = [SKPhysicsJointSpring jointWithBodyA:nodeA.physicsBody bodyB:nodeB.physicsBody anchorA:nodeA.position anchorB:nodeB.position];
    joint.frequency = kFrequency; //gives the joint some elasticity.
    joint.damping = kDamping; // 0.0f Will remove damping to create the 'pendulum'
    [self.physicsWorld addJoint:joint];
}

@end

0 个答案:

没有答案