0

I have a simple game using UiKit dynamics implementation. The game has blocks (the ones in red) aligned with grids as shown in below picture.The blocks falls from top and rest in bottom grid

enter image description here

Say the bottom grid row is called with reference 1, I want to check in which grid reference the block is in. I am doing this by using CGGeomentry, CGRectContainsPoint([grid frame], and then assign the grid number i.e 1 to blocks. It works fine for the single block as using in above picture.

Now take a look at second picture.

enter image description here

Once the second block is stacked on top of first block, you can notice a clearance with grid row 2 and the second block. This clearance is getting increased once more blocks are stacked on top of each other. Because of this clearance, CGRectContainsPoint calculation gives wrong grid number for grid rows more than 6.

In other words, first block should be assigned with grid row 1, second block should be assigned with grid row 2. Since the blocks frame size and grid frame size are same, this should be true to block 10 or block 100. But because of this clearance, after some blocks, the grid assignments looks wrong, i.e, block 6 is getting grid number as 5.

I am using UIKit dynamics gravity behaviour to make the blocks fall from top and collision behaviour to determine collisions. I even set the elasticity property to 0.0 to reduce the bounciness.

My question is why I am getting this clearance when the grid frame size and block frame size are same. Shouldn't they fit in exactly? How can I get rid of this clearance?

You can see the clearance is getting more when more blocks are stacked in below picture enter image description here

Thank you

slysid
  • 5,236
  • 7
  • 36
  • 59

1 Answers1

1

Don't use UIKit Dynamics for physics simulations. It isn't designed for it. It's designed for creating animations that give a sense of physics to them.

If I was doing something like this I'd probably just use UIKit and UIView animations.

In fact, I've blogged something similar that uses AutoLayout for the placement of blocks which might be useful?

You can see the auto layout game here.

Either that or use SpriteKit, but still don't use physics.

I would avoid physics (i.e., physics bodies acting under gravity or other forces) for most games. Even games like Mario (2D side platformers) and Flappy Bird don't use physics. They use very precisely calculated movements.

The jump in Mario is actually split into several different animations to make timing a jump and nailing a landing easier for the user to do. Just using Physics actually makes a game harder to play in most cases.

Obviously games like Angry Birds are different because their very nature requires physics to simulate the flight of the bird and the falling of blocks. But the physics isn't interacted with by the user. It just plays out based on their previous actions.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Fogmeister
  • 76,236
  • 42
  • 207
  • 306