Is there a way to visually see sprite kit's SKPhysicsbody borderline?

user2832367 picture user2832367 · Jan 19, 2014 · Viewed 12.7k times · Source

I am using bodyWithPolygonFromPath to define the volume of the physicsbody, and used

http://dazchong.com/spritekit/

to get the paths required. But the path does not seem correct and I wish to see the borderline of the physicsbody path to see if the shape is correct.

Is there any way to see the physicsbody's volume outline?

I tried the following code, but it doesn't work.

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 setScale:0.5];
ship.zRotation = - M_PI / 2;

Answer

Julio Montoya picture Julio Montoya · Jan 22, 2014

For Objective-C & iOS < 7.1

In your ViewController.m find this code

SKView *skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;

After the last line add

skView.showsPhysics = YES;

Build and Run and you should see all the physics Body borderline

i noticed that you add the shapeNode to self instead of the spriteNode so try the following

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];

For Swift & iOS >= 8.0

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

And if you want a custom borderline

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)

Already tested :]

Good Luck!!