Ellipse SKPhysicsBody

跟風遠走 提交于 2019-12-12 01:07:13

问题


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?


回答1:


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.




回答2:


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.


来源:https://stackoverflow.com/questions/21270966/ellipse-skphysicsbody

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!