Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

dynamic - Sprite Kit failing assertion: (typeA == b2_dynamicBody || typeB == b2_dynamicBody)

This was asked earlier, but the original asker didn't need to change the dynamic property so he answered his own question by unasking it.

I'm using Sprite Kit in iOS7 and I'd like to be able to change an SKPhysicsBody's dynamic property at runtime. Originally I was changing that in my touchesBegan: method. Someone in the Apple Dev forum suggested moving the change to the didSimulatePhysics: method but that didn't help either.

This code causes the error:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
  if (!heyWereSwappingDynamismHere) 
  {
    heyWereSwappingDynamismHere = YES;
    SKNode * rope = [self childNodeWithName:@"rope"];
    SKNode * anchor = [rope childNodeWithName:@"anchor"];
    [listOfObjectsToSwapDynamic addObject:anchor];
  }
}

-(void) didSimulatePhysics 
{
  if (heyWereSwappingDynamismHere) 
  {
    for (SKNode * node in listOfObjectsToSwapDynamic) 
    {
        bool isItDynamic = node.physicsBody.isDynamic;
        node.physicsBody.dynamic = !isItDynamic;
    }
    [listOfObjectsToSwapDynamic removeAllObjects];
    heyWereSwappingDynamismHere = NO;
}

}

The error appearing in the debugger is:

Assertion failed: (typeA == b2_dynamicBody || typeB == b2_dynamicBody), function SolveTOI, file /SourceCache/PhysicsKit/PhysicsKit-4.6/PhysicsKit/Box2D/Dynamics/b2World.cpp, line 670.

I've looked around elsewhere but this seems to a be a problem with Sprite Kit's implementation (and covering over) of Box2D.

Maybe?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can change the dynamic property back and forth, however, you should be very careful if you're using collision/contact detection.

During collision/contact between two bodies, it is asserted that at least one of them must be a dynamic body. If you change that property to NO at runtime, and the other contacted body is also static (non-dynamic), and the collision/contact callback is happening, it will throw this particular assertion error - contact will be detected between 2 static bodies mid-execution, which Box2d doesn't enjoy.

A possible workaround trick - works for me, even directly in the didBeginContact: method:

SKPhysicsBody *temp = node.physicsBody;
temp.dynamic = NO;
node.physicsBody = temp;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...