0

I am trying to create an ellipses. I used bodyWithEdgeLoopFromPath and it worked but there seems to be something wrong with it because sometimes other objects get caught in the middle of it.

But I want the ellipses to be solid, So I tried bodyWithPolygonFromPath (I want it static)

horizontalOval = [[SKShapeNode alloc] init];
theRect = CGRectMake(0, 0, self.frame.size.width/6 , 15);
CGMutablePathRef ovalPath = CGPathCreateMutable();
CGPathAddEllipseInRect(ovalPath, NULL, theRect);
horizontalOval.path = ovalPath;
horizontalOval.fillColor = [UIColor blueColor];
horizontalOval.physicsBody.dynamic = NO;
horizontalOval.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:ovalPath];

But got the error

SKPhysicsBody: Error attempting to create polygon with 17 vertices, maximum is 12

How do I create complex paths and make them solid?

Also when I pud it in position self.frame.size.width/2 and self.frame.size.height/2 It doesn't stay center, it goes a little to the right.

I had to theRect = CGRectMake(-40, 0........) to make it center but why is that?


UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: _paddleRect];

But has 13 vertices. Trying to use PaintCode.

Jay
  • 6,572
  • 3
  • 37
  • 65
majidarif
  • 18,694
  • 16
  • 88
  • 133

1 Answers1

0

You can think of an edge body as one that has no volume, just the edges, a body with 'negative space' - that's why your objects got caught in the middle of it. As the Sprite Kit Programming Guide says:

The main difference between a edge and a volume is that an edge permits movement inside its own boundaries, while a volume is considered a solid object.

Since you want your oval to be a solid object, you do need a volume-based body. For these bodies, you have three options to create their shape: a circle (bodyWithCircleOfRadius:), a rectangle (bodyWithRectangleOfSize:), or a polygon (bodyWithPolygonFromPath:).

For an oval shape, you probably have to draw a polygon - however, the Sprite Kit physics engine will only accept those with a maximum of 12 vertices (that's why you were getting an error when drawing an actual ellipse). Your best bet is drawing a polygon using a helper tool, such as this one: http://dazchong.com/spritekit/ - just drag and drop your sprite and draw the path. Remember that the polygon must be convex (no angles over 180 degrees inside it) and that it can have a maximum of 12 vertices.

Also check out this answer for a similar issue: Ellipse SKPhysicsBody

Community
  • 1
  • 1
Batalia
  • 2,425
  • 1
  • 17
  • 18
  • the tool is hard to create a perfect curve. it only makes points. – majidarif Feb 02 '14 at 13:49
  • I tried the answer on the link you gave, still git the error `SKPhysicsBody: Error attempting to create polygon with 17 vertices, maximum is 12` – majidarif Feb 02 '14 at 14:12
  • A polygon is not a curved body, you can only trace an estimation of an ellipse this way. Also, you can do only 12 clicks in the tool (for 12 vertices). Unfortunately, that's the limit of the volume-based polygon body in Sprite Kit. Make sure you use the path generated in the tool, not your original ovalPath (which had the 17 vertices). – Batalia Feb 02 '14 at 14:19
  • I tried something, just 1 more verticy. updated my question. :) thanks though. really appreciated. – majidarif Feb 02 '14 at 14:21