SKPhysicsBody并不像预期的那样

时间:2013-10-16 06:03:05

标签: objective-c sprite-kit skphysicsbody

我有以下代码来创建矩形砖和与之关联的物理体。我期望物理体是一个与砖块相同大小和位置的实心矩形,但我得到的是一个我认为有一个偏差的体,也许还有一个尺寸差异。我错过的坐标系有问题吗?什么是正确的方法来解决这个问题?

- (void)addBrick {
    SKShapeNode *brick = [[SKShapeNode alloc] init];

    CGRect brickBoundary = CGRectMake(0.0, 0.0, 100.0, 100.0);
    brick.position = CGPointMake(100.0, 100.0);
    brick.path = CGPathCreateWithRect(brickBoundary, nil);

    brick.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(100.0,100.0)];
    brick.physicsBody.restitution = 1.0;
    brick.physicsBody.friction = 0.0;
    brick.physicsBody.dynamic = NO;

    [self addChild:brick]; 
}

1 个答案:

答案 0 :(得分:5)

这是创建矩形并向其添加物理主体的简便方法。

SKSpriteNode *n1 = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:objectSize];
n1.position = CGPointMake(self.size.width/2, 200);
n1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:n1.size];
n1.physicsBody.dynamic = NO;
[self addChild:n1];

以下是RW教程的修改版本,为您提供调试矩阵

// RW Debug modified version
CGPathRef bodyPath = CGPathCreateWithRect( CGRectMake(-n1.size.width/2, -n1.size.height/2, n1.size.width, n1.size.height),nil);
SKShapeNode *shape = [SKShapeNode node];
shape.path = bodyPath;
shape.strokeColor = [SKColor colorWithRed:1.0 green:0 blue:0 alpha:0.5];
shape.lineWidth = 1.0;
[n1 addChild:shape];
CGPathRelease(bodyPath);

你的实现产生了这个(红色是物理盒)。

enter image description here