While trying to replicate the game “Breakout” using ios7’s UIKit Dynamics, I have not been able to figure out how to alter an items trajectory.
In the game, the trajectory of the ball is determined based on where it strikes the paddle. What method/s should I call to alter regular trajectory calculations.
What I have so far is copied below. Thanks for your help.
viewDidLoad.m
- (void)viewDidLoad
{
[super viewDidLoad];
unstruckBlocks = [NSMutableArray array];
[self drawBlocks];
gameHasBegun = NO;
// Set ball image
NSString *ballImagePath = [[NSBundle mainBundle] pathForResource:@"ball" ofType:@"png" inDirectory:@"pongAssets"];
ballView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:ballImagePath]];
[ballView setOpaque:NO];
// Set paddle image
NSString *paddleImagePath = [[NSBundle mainBundle] pathForResource:@"paddle" ofType:@"png" inDirectory:@"pongAssets"];
paddleView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:paddleImagePath]];
[paddleView setOpaque:NO];
dynamicAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
paddleDynamicBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[paddleView]];
ballDynamicBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[ballView]];
blockDynamicBehavior = [[UIDynamicItemBehavior alloc] initWithItems:unstruckBlocks];
pushBehavior = [[UIPushBehavior alloc] initWithItems:@[ballView] mode:UIPushBehaviorModeInstantaneous];
collisionBehavior = [[UICollisionBehavior alloc] initWithItems:unstruckBlocks];
collisionBehavior.collisionDelegate = self;
[collisionBehavior addItem:paddleView];
[collisionBehavior addItem:ballView];
paddleDynamicBehavior.allowsRotation = NO;
paddleDynamicBehavior.density = 100000000;
paddleDynamicBehavior.elasticity = 1.0;
[dynamicAnimator addBehavior:paddleDynamicBehavior];
ballDynamicBehavior.allowsRotation = NO;
ballDynamicBehavior.elasticity = 1.0;
ballDynamicBehavior.friction = 0.0;
ballDynamicBehavior.resistance = 0.0;
[dynamicAnimator addBehavior:ballDynamicBehavior];
blockDynamicBehavior.allowsRotation = NO;
blockDynamicBehavior.elasticity = 1.0;
blockDynamicBehavior.friction = 0.0;
blockDynamicBehavior.resistance = 0.0;
blockDynamicBehavior.density = 100000000;
[dynamicAnimator addBehavior:blockDynamicBehavior];
pushBehavior.pushDirection = CGVectorMake(0.3, 0.9);
pushBehavior.magnitude = 0.5;
pushBehavior.active = NO;
[dynamicAnimator addBehavior:pushBehavior];
collisionBehavior.collisionMode = UICollisionBehaviorModeEverything;
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[dynamicAnimator addBehavior:collisionBehavior];
}