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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 18:10:27

问题


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?


回答1:


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;



回答2:


If the original poster's intent is to disable collisions while directly manipulating an SKNode, it can be done as follows:

@property (nonatomic, strong) SKNode *node;
@property (nonatomic, assign) uint32_t previousCollisionBitMask;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  // Disable collisions & gravity
  self.previousCollisionBitMask = self.node.physicsBody.collisionBitMask;
  self.node.physicsBody.collisionBitMask = 0;
  self.node.physicsBody.affectedByGravity = NO;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  // Enable collisions & gravity
  self.node.physicsBody.collisionBitMask = self.previousCollisionBitMask;
  self.node.physicsBody.affectedByGravity = YES;
}

This takes the object out of calculation for collisions. Disabling gravity keeps the object from falling while you're moving it.

If you're also concerned about the contactTextBitMask, you can use similar logic to disable that while you're manipulating the scene with touches.



来源:https://stackoverflow.com/questions/20462295/sprite-kit-failing-assertion-typea-b2-dynamicbody-typeb-b2-dynamicbod

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