Application of physics knowledge in iOS Animation: Angry Birds-acceleration of gravity
Flat throwing
Do you still remember the beautiful arc drawn by the birds in the air when they first played Angry Birds? Do you still remember the scenes in which birds bounce and drop on the ground? Here we will introduce how to implement the flat throwing motion.
1. Physical knowledge of flat Motion
Vc/examples/mxvrXEzu/examples + examples/T0Muuxr23vc/examples + a + example + jrMvZtsi3tM/yoaM8L3A + DQo8aDMgaWQ9 "2 code implementation "> 2. code Implementation 1. set initial volume
-(Void) setUpStartValue {// The initial position self on the screen. birdX = 0; self. birdY = 0; // x, the initial speed self on the Y axis. birdSpeedX = 10; self. birdSpeedY = 0; // gravity acceleration self. gbirdspeed= 9.8 ;}
2. Core code for simulating a gravity environment
// The speed remains unchanged on the X axis. Each time a refresh is performed, the position on the X axis is equal to the speed, plus the first position self. birdX + = self. birdSpeedX; // each time the Y axis is refreshed, the speed of the bird increases by a gravity acceleration self. birdSpeedY + = self. gBirdSpeed; // The position on the Y axis is equal to the added speed on the Y axis, plus the previous position self. birdY + = self. birdSpeedY; // if (self. birdX> = SCREENWIDTH-IMAGEWIDTH) {self. birdX = SCREENWIDTH-IMAGEWIDTH;} // determines whether to touch the ground. if you touch the window boundary, Vy is adjusted to the opposite direction if (self. birdY> = SCREENHEIGHT-IMAGEHEIGHT) {self. birdY = SCREENHEIGHT-IMAGEHEIGHT; self. birdSpeedY =-self. birdSpeedY ;}
3. Complete project code
# Import MRView. h # define SCREENHEIGHT [UIScreen mainScreen]. bounds. size. height # define SCREENWIDTH [UIScreen mainScreen]. bounds. size. width # define IMAGEWIDTH 50 # define IMAGEHEIGHT 50 @ interface MRView () @ property (nonatomic, strong) UIImage * imageBackgound; @ property (nonatomic, strong) UIImage * imageBird; @ property (nonatomic, assign) CGFloat birdX; @ property (nonatomic, assign) CGFloat birdY; @ property (nonatomic, assign) CGFloat birdSpeedX; @ property (nonatomic, assign) CGFloat birdSpeedY; @ property (nonatomic, assign) CGFloat gBirdSpeed; @ end @ implementation MRView-(void) awakeFromNib {self. contentMode = UIViewContentModeRedraw; self. backgroundColor = [UIColor blackColor]; [self setUpStartValue];}-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {[self setUpStartValue];} return self;}-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {[self setUpStartValue]; CADisplayLink * link = [CADisplayLink displayLinkWithTarget: self selector: @ selector (setNeedsDisplay)]; [link addToRunLoop: [nsunloop mainRunLoop] forMode: Unknown];}-(void) setUpStartValue {self. imageBackgound = [UIImage imageNamed: @IMG_0465.jpg]; self. imageBird = [UIImage imageNamed: @ QQ20150714-1]; self. birdX = 0; self. birdY = 0; self. birdSpeedX = 10; self. birdSpeedY = 0; self. gBirdSpeed = 9;}-(void) drawRect :( CGRect) rect {[self. imageBird drawInRect: CGRectMake (self. birdX, self. birdY, IMAGEWIDTH, IMAGEHEIGHT)]; // The speed on the X axis remains unchanged. Each time you refresh, the position on the X axis is equal to the speed, plus the previous position self. birdX + = self. birdSpeedX; // each time the Y axis is refreshed, the speed of the bird increases by a gravity acceleration self. birdSpeedY + = self. gBirdSpeed; // The position on the Y axis is equal to the added speed on the Y axis, plus the previous position self. birdY + = self. birdSpeedY; // if (self. birdX> = SCREENWIDTH-IMAGEWIDTH) {self. birdX = SCREENWIDTH-IMAGEWIDTH;} // determines whether to touch the ground. if you touch the window boundary, Vy is adjusted to the opposite direction if (self. birdY> = SCREENHEIGHT-IMAGEHEIGHT) {self. birdY = SCREENHEIGHT-IMAGEHEIGHT; self. birdSpeedY =-self. birdSpeedY ;}}@ end
4. Running result