Question

I'm currently making an IOS app and I would like to create a custom SKPhysicsBody. I used an editor to create a .xml file, which contains the coordinates of the points I'm using. Is there any way of creating a physics body from these points? I'm using Sprite kit.

Was it helpful?

Solution

I was able to re-create the physics body with this code :

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

 CGMutablePathRef spritePath = CGPathCreateMutable();

 CGPathMoveToPoint(spritePath, NULL, 3 - offsetX, 1 - offsetY);
 CGPathAddLineToPoint(spritePath, NULL, 1 - offsetX, 3 - offsetY);
 CGPathAddLineToPoint(spritePath, NULL, 1 - offsetX, 6 - offsetY);
 CGPathAddLineToPoint(spritePath, NULL, 6 - offsetX, 7 - offsetY);
 CGPathAddLineToPoint(spritePath, NULL, 7 - offsetX, 10 - offsetY);
 CGPathAddLineToPoint(spritePath, NULL, 19 - offsetX, 9 - offsetY);
 CGPathAddLineToPoint(spritePath, NULL, 20 - offsetX, 7 - offsetY);
 CGPathAddLineToPoint(spritePath, NULL, 14 - offsetX, 6 - offsetY);
 CGPathAddLineToPoint(spritePath, NULL, 11 - offsetX, 5 - offsetY);
 CGPathAddLineToPoint(spritePath, NULL, 9 - offsetX, 5 - offsetY);
 CGPathAddLineToPoint(spritePath, NULL, 9 - offsetX, 2 - offsetY);
 CGPathAddLineToPoint(spritePath, NULL, 8 - offsetX, 1 - offsetY);

 sprite.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:spritePath];

I entered manually the coordinates, and it works! However, physics body are limited to 12 lines I believe, so I had to change the physics body.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top