Question

I am simulating physics to an egg shape SKSprite node. However, there is assertion failed error occur:

Assertion failed: (edge.LengthSquared() > 1.19209290e-7F * 1.19209290e-7F), function Set, file /SourceCache/PhysicsKit_Sim/PhysicsKit-6.5.4/PhysicsKit/Box2D/Collision/Shapes/b2PolygonShape.cpp, line 180.

the following is the code:

self.egg = [SKSpriteNode spriteNodeWithImageNamed:IMAGE_NAME_EGG];
    [self.egg setScale:0.2];
    self.egg.position = CGPointMake(self.size.width/2,self.size.height - self.egg.size.height/2);
    self.egg.name = IMAGE_NAME_EGG;

    CGPoint startPoint = CGPointMake(0, self.egg.size.height*0.4);
    CGPoint endPoint = CGPointMake(self.egg.size.width, startPoint.y);
    CGPoint controlPointLeft = CGPointMake(startPoint.x, self.egg.size.height);
    CGPoint controlPointRight = CGPointMake(endPoint.x, controlPointLeft.y);


    CGMutablePathRef pathRef = CGPathCreateMutable();        
    CGPathMoveToPoint(pathRef, NULL, startPoint.x, startPoint.y);
    CGPathAddQuadCurveToPoint(pathRef, NULL, controlPointLeft.x, controlPointLeft.y, self.egg.size.width/2, controlPointLeft.y);
    CGPathAddQuadCurveToPoint(pathRef, NULL, controlPointRight.x, controlPointRight.y, endPoint.x,endPoint.y);
    CGPathAddArc(pathRef, NULL, self.egg.size.width/2, startPoint.y, self.egg.size.width/2, 0, M_PI, NO);

    self.egg.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:pathRef];
    self.egg.physicsBody.dynamic = YES;
    self.egg.physicsBody.categoryBitMask = eggCategory;
    self.egg.physicsBody.contactTestBitMask = rabbitCategory;
    self.egg.physicsBody.collisionBitMask = rabbitCategory;
    self.egg.physicsBody.allowsRotation = YES;
    [self addChild:self.egg];

What's wrong? can anyone help me to fix? thank you very much!

Was it helpful?

Solution 2

According to the assertion, one of the edges is shorter than the minimum size expected by sprite kit.

Assertion failed: (edge.LengthSquared() > 1.19209290e-7F * 1.19209290e-7F)

Check your path coordinates and ensure it's not too small.

OTHER TIPS

when you use " bodyWithPolygonFromPath: ", you mut make sure that the path meats the following conditions:

1). convex polygonal path 2). counterclockwise winding 3). no self intersections. //The points are specified relative to the owning node’s origin.

you can find it in apply class reference for SKPhysicsBody here: https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/Reference/Reference.html#//apple_ref/occ/clm/SKPhysicsBody/bodyWithPolygonFromPath:

I had this error on an older iOS7 iPhone when attempting to make a triangular path. It turns out that adding a final line to bring the path back to the starting point led to the error (though newer iOS versions allow this.) IE:

CGPathMoveToPoint(path, nil, self.size.width/2, self.size.height/2)
CGPathAddLineToPoint(path, nil, -self.size.width/2, 0.0)
CGPathAddLineToPoint(path, nil, self.size.width/2, -self.size.height/2)
CGPathAddLineToPoint(path, nil, self.size.width/2, self.size.height/2) //REMOVED THIS LINE TO FIX
CGPathCloseSubpath(path)

This explains why there was an edge that was too small to evaluate - the edge between two of the same points is 0!

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