有没有一种方法可以直观地看到Sprite Kit的SKPhysicsBody边界线?

31

我正在使用bodyWithPolygonFromPath来定义物理体的体积,并使用http://dazchong.com/spritekit/获取所需路径。但是路径似乎不正确,我希望能够看到物理体路径的边界以查看形状是否正确。

有没有方法可以查看物理体的边界?

我尝试了以下代码,但并没有起作用。

ship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];

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

CGMutablePathRef path = CGPathCreateMutable();

CGPathMoveToPoint(path, NULL, 50 - offsetX, 110 - offsetY);
CGPathAddLineToPoint(path, NULL, 18 - offsetX, 16 - offsetY);
CGPathAddLineToPoint(path, NULL, 140 - offsetX, 15 - offsetY);

CGPathCloseSubpath(path);

SKShapeNode *yourline = [SKShapeNode node];
yourline.name = @"yourline";
yourline.path = path;
[yourline setStrokeColor:[UIColor redColor]];
[self addChild:yourline];

ship.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
ship.zRotation = - M_PI / 2;
8个回答

70

针对Objective-C和iOS < 7.1

在你的ViewController.m文件中找到以下代码:

SKView *skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
在最后一行后添加。
skView.showsPhysics = YES;

构建和运行,您应该看到所有物理体的边界线

我注意到您将shapeNode添加到self而不是spriteNode中,请尝试以下操作

SKSpriteNode *ship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
CGFloat offsetX = ship.frame.size.width * ship.anchorPoint.x;
CGFloat offsetY = ship.frame.size.height * ship.anchorPoint.y;

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 50 - offsetX, 110 - offsetY);
CGPathAddLineToPoint(path, NULL, 18 - offsetX, 16 - offsetY);
CGPathAddLineToPoint(path, NULL, 140 - offsetX, 15 - offsetY);
CGPathCloseSubpath(path);

ship.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];

[self addChild:ship];

SKShapeNode *shape = [SKShapeNode node];
shape.path = path;
shape.strokeColor = [SKColor colorWithRed:1.0 green:0 blue:0 alpha:0.5];
shape.lineWidth = 1.0;
[ship addChild:shape];

适用于Swift和iOS >= 8.0

let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsPhysics = true

如果您想要一个自定义边框

let ship = SKSpriteNode(imageNamed: "Spaceship")
let offsetX = ship.frame.size.width * ship.anchorPoint.x
let offsetY = ship.frame.size.height * ship.anchorPoint.y

let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 50 - offsetX, 110 - offsetY)
CGPathAddLineToPoint(path, nil, 18 - offsetX, 16 - offsetY)
CGPathAddLineToPoint(path, nil, 140 - offsetX, 15 - offsetY)
CGPathCloseSubpath(path)

ship.physicsBody = SKPhysicsBody(polygonFromPath: path)
addChild(ship)

let shape = SKShapeNode()
shape.path = path
shape.strokeColor = SKColor(red: 1.0, green: 0, blue: 0, alpha: 0.5)
shape.lineWidth = 1.0
addChild(shape)

已经测试过啦 :]

祝你好运哦!!


你在哪里看到了'skView.showsPhysics'?也许你引用了一个beta更新? - joshd
是的,那只在iOS 7.1 Beta中有效,我的回答已更新,谢谢。 - Julio Montoya

5

Swift

GameViewController.swift中添加以下行:

override func viewDidLoad() {
    ...
    if let view = self.view as? SKView {
        ...
        view.showsPhysics = true // <--- add this line
    }
}

1
我知道有些晚了,但我认为你可能会对我的新工具感兴趣,它可以自动创建精灵图像周围的路径(这样你就不必手动点击点),然后您可以调整各种设置以更好地满足您的要求。该工具还输出添加路径到精灵物理体的Objective C和Swift程序代码。希望对一些人有所帮助。谢谢。

http://www.radicalphase.com/pathgen/


抱歉,域名已过期 - 不过工具现在已经恢复在线了 :) - Clive Minnican
1
新更新:现在包括一个强制凸模式(默认启用),以确保与SpriteKit物理体的100%兼容性,并减少点数 - 享受吧! :) - Clive Minnican

1

为了测试目的,我会将飞船设置为SKShapeNode,并先注释掉你的那行SKShapeNode代码,然后将飞船的路径设置为与你的SKPhysicsBody使用的相同路径。

或者你可以使用这个:

https://github.com/ymc-thzi/physicsDebugger


1
在新的7.1测试版中,如上所述,有允许此操作的功能。

1

SpriteKit提供了一个内置功能,可以在所有物理体周围绘制浅蓝色边框。

显示带有物理体边框的示例精灵

SwiftUI

在实例化SpriteView时,包括showsPhysics调试选项:

var body: some View {
    let scene = SKScene(fileNamed: "MyScene")!
    SpriteView(scene: scene, debugOptions: [.showsPhysics])  // <-- HERE
}

这里是所有调试选项的文档,其中还包括有用的功能如showsFPSshowsNodeCount

Cocoa/UIKit (Swift)

在您的视图控制器的viewDidLoad中,启用您的SKViewshowsPhysics属性:

override func viewDidLoad() {
    super.viewDidLoad()
    let skView = self.view as! SKView
    skView.showsPhysics = true         // <-- HERE
}

Cocoa/UIKit (Objective-C)

在您的视图控制器的viewDidLoad方法中,启用SKViewshowsPhysics属性:

- (void)viewDidLoad {
    [super viewDidLoad];
    SKView *skView = (SKView*)self.view;
    skView.showsPhysics = YES;            // <-- HERE
}

0

这将在您的节点顶部添加一个红色线形状。使用与您为节点定义物理体相同的路径。

+(void)debugPath:(CGPathRef)path onNode:(SKNode*)node {
    SKShapeNode *yourline = [SKShapeNode node];
    yourline.name = @"yourline";
    yourline.path = path;
    [yourline setStrokeColor:[UIColor redColor]];
    [node addChild:yourline];
}

即用法:

[[self class] debugPath:ship.physicsBody.path onNode:ship];

0
你的形状节点代码是正确的。看起来yourline可能会在实际船只之前添加到场景中,因此船只精灵将被绘制在其上方,使其被遮挡。确保将yourline.zPosition设置为最高值,或确保在[self addChild:ship]之后执行[self addChild:yourline]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接