4

I keep getting a crash when using UIKit Dynamics. It doesn't happen every time the code runs, but it happens fairly often. Has anyone come across this?

Assertion failed: (a.x >= 0.0f && a.y >= 0.0f), 
function SolveVelocityConstraints, file 
/SourceCache/PhysicsKit/PhysicsKit-
4.6/PhysicsKit/Box2D/Dynamics/Contacts/b2ContactSolver.cpp, line 422.

This is the code that generates the error. I'm guessing it has something to do with the collision boundaries.

UIDynamicAnimator *animator = 
 [[UIDynamicAnimator alloc] initWithReferenceView:self];

UIGravityBehavior *gravityBehavior = 
 [[UIGravityBehavior alloc] initWithItems:@[self.rewardButton]];
gravityBehavior.gravityDirection = CGVectorMake(0.0, 1.0);
gravityBehavior.magnitude = 1.0f;

UICollisionBehavior* collisionBehavior = 
 [[UICollisionBehavior alloc] initWithItems:@[self.rewardButton, 
                                              self.processingView]];
collisionBehavior.collisionDelegate = self;
UIEdgeInsets insets = UIEdgeInsetsMake(-700, 60, 200, 60);
[collisionBehavior setTranslatesReferenceBoundsIntoBoundaryWithInsets:insets];

UIDynamicItemBehavior* propertiesBehavior = 
 [[UIDynamicItemBehavior alloc] initWithItems:@[self.rewardButton]];
float elasticity = arc4random() % 4 * 0.1 + 0.1f;
propertiesBehavior.elasticity = elasticity;

[animator addBehavior:propertiesBehavior];
[animator addBehavior:gravityBehavior];
[animator addBehavior:collisionBehavior];

self.animator = animator;
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
bmueller
  • 2,681
  • 1
  • 27
  • 45
  • I get the same error on SpriteKit, not UIDynamics. I thought I found an answer on [this Box2D thread](http://www.box2d.org/forum/viewtopic.php?f=4&t=5250), which suggests setting the body's mass to some default would fixed the `Assertion` failure, but it didn't. Thought I would share. – kvn Nov 25 '13 at 21:23
  • I'm getting the same crash with UIKit Dynamics but it is only happening on my iPad (mini retina iOS 7.0.4) and never on my iPhone (5 iOS 7.0.4). Is the crash iPad specific for you too? – Brandon Nov 27 '13 at 00:53
  • Also, it only appears to happen when using a UICollisionBehavior collisionMode of UICollisionBehaviorModeEverything. I tried with UICollisionBehaviorModeBoundaries and UICollisionBehaviorModeItems and didn't get the crash. – Brandon Nov 27 '13 at 05:25
  • It was on the iPhone for me. And I wasn't explicitly setting a collisionMode. – bmueller Nov 27 '13 at 05:58
  • bmueller, are you on an iPhone 5s or something older? I think this may only happen on the new devices with the A7 chip (iPhone 5s, iPad Air, iPad Mini Retina). As for the collision mode, UICollisionBehaviorModeEverything is the default so that's probably what is being used if you didn't explicitly set it. – Brandon Nov 27 '13 at 13:52
  • Yep, it was on a 5s. Thanks for the heads up, I'll try changing that. – bmueller Nov 27 '13 at 18:26

2 Answers2

1

A bit of a hack that seems to work for me is setting a very large density amount for UIDynamicItemBehavior. Using a density of 1 or 2 still gave me the error but 500 didn't seem to. Like I said, it is pretty hacky but, in my very limited testing it seems to do the trick.

UIDynamicItemBehavior *itemBehavior = 
 [[UIDynamicItemBehavior alloc] initWithItems:self.items];
[itemBehavior setDensity:500.0f];
[self.animator itemBehavior];

Thanks to KillerRhino who's comment and the Box2D thread he linked to lead me down this road of looking into the mass of the item as the issue. According to the UIDynamicItemBehavior documentation the density along with it's size determines the mass. Hopefully this workaround holds up under other conditions / setups.

I think this issue only happens on the new devices with the new A7 chip (iPhone 5s, iPad Air, iPad Mini Retina). Maybe a 64 bit issue?

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Brandon
  • 953
  • 10
  • 12
0

I had the exact same crasher. I solved it by increasing the mass on the physicsBody objects on my sprites. They were really small, like 0.05. Increased them to 50 (so multiplied everything by 1,000), which made the crasher stop. Of course, you'll have to alter some other properties to make the physics effects appear the same as before increasing mass.

Rachid Finge Jr
  • 1,171
  • 10
  • 14