Question

Which SKPhysicsBody body type would I use to create an elliptical physicsBody?

I know I could make a curve out of straight lines and just have it not be a real ellipse, but it seems like there must be some way to squish a circle or create one?

Was it helpful?

Solution

Create an elliptical CGPath and create a polygon body with that path:

CGPathRef path = CGPathRef CGPathCreateWithEllipseInRect(someRect, nil);
SKPhysicsBody* body = [SKPhysicsBody bodyWithPolygonFromPath:path];

It's possible though that the created path creates more than 16 vertices (the internal limit) for the ellipse. In that case it is going to crash and you'd have to create the path manually, ensuring it doesn't have more than 16 points.

If the body doesn't need to be dynamic you can also use bodyWithEdgeLoopFromPath: and bodyWithEdgeChainFromPath: - both impose no limit on number of vertices.

OTHER TIPS

Since Sprite Kit won't accept a volume-based physics body with an elliptical CGPath (it can do circles, rectangles, or any convex polygons with up to 12 vertices), you have two options:

  • Draw a polygon that's close to an ellipse with up to 12 vertices - you can use a helper tool, for example: http://dazchong.com/spritekit/
  • Idea: if you need a rounder, rolling behaviour, add a circle-based physics body, then add a child SKNode to your sprite and offset it a bit to the side, then add a smaller circle-based physics body to that child node. Now you have two circular physics bodies together. Rinse and repeat on the other side - I suppose you could come close to some sort of a cloud/elliptical shape with as few as three such circles close to one another. Or you may get better results simply connecting circular bodies with a fixed joint.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top