Whenever the circular view collides with the paddle on storyboard, it actually collides with the rectangular frame of the view. How do I resolve this issue. I have also tried UIBezierPath
, CAShapeLayer
and it doesn't seem to be working.
The red arrow is the colliding point, As it does not seem to be colliding with ball instead it collides with the frame of the view. once, the frame is touched with the paddle it bounces back and so on.
An even simpler answer than the accepted answer would be to set the bounds to be elliptical instead of a defined path:
If you're using
UIDynamicAnimator
, you can define thecollisionBoundsType
and thecollisionBoundingPath
for the items that will be colliding, for example:If you do this, and then add these items to your
UICollisionBehavior
, it will respect the collision boundaries you defined for the items. For example:The above illustrates a glancing blow, like in your original image. If you want it to bounce up even if it hits the edge of the paddle, you can specify the
collisionDelegate
for theUICollisionBehavior
and give it a new direction:And conform to
UICollisionBehaviorDelegate
and then implementcollisionBehavior(beganContactForItem:withItem:atPoint:)
:That yields something like:
Clearly, you'll have to play around with this quite a bit to get the desired effect, but it illustrates the basic idea of detecting a collision and adding a linear velocity to the ball, accordingly.