Application of physics knowledge in iOS Animation: Angry Birds-Collision Detection and ios Collision Detection
Collision Detection
Source code: https://github.com/Esdeath/07-13-
I believe that iOS players are more or less familiar with the ball play game. The trajectory of a different point can be changed. For many people, if they do not know the idea, they may think that the ball hits the screen, and it seems difficult to change the movement direction when they hit the wall.
In fact, this only requires a little iOS drawing and animation basics, but also a little physical knowledge.
1. Both velocity and displacement are vectors.
In the 2D coordinate system, both velocity and displacement can be divided into components on the X and Y axes.
Therefore, we can describe the motion of an object in Vx and Vy Based on the velocity. Every time the page refreshes, the object's velocity component Vx and Vy are used to calculate the position of the next object:
- Next X axis coordinate = Vx + current X axis Coordinate
- Next Y axis coordinate = Vy + current Y axis Coordinate
2. Collision speed Model
If an object hits the bottom side of the screen, the orientation of the object on the X axis remains the same, and the speed on the Y axis is reversed. Similarly, it hits the top side of the screen.
Physical Model:
Similarly, if an object hits the Left or Right:
3. Code Implementation 1. Create a New MRView and add it to the root View.
2. Add CADisplayLink
CADisplayLink is a timer that allows us to draw content to the screen at the same frequency as the screen update rate. Create a new CADisplayLink object in the application, add it to a runloop, and provide it with a target and selector called when the screen is refreshed.
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
3. Set member attributes
// Bird image @ property (nonatomic, strong) UIImage * imageBird; // Bird position @ property (nonatomic, assign) CGFloat birdX; @ property (nonatomic, assign) CGFloat birdY; // bird speed @ property (nonatomic, assign) CGFloat birdSpeedX; @ property (nonatomic, assign) CGFloat birdSpeedY;
4. Initialization
It should be noted that it must be referenced from the xib, storyboard, and code initialization. Call setNeedsDisplay in CADisplayLink for non-stop re-painting.
// Initialize the call from xib or storyboard-(void) awakeFromNib {[self setUpBird];} // call this call from the code initialization-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {[self setUpBird];} return self ;}- (void) setUpBird {self. backgroundColor = [UIColor blackColor]; CADisplayLink * link = [CADisplayLink displayLinkWithTarget: self selector: @ selector (callback)]; [link addToRunLoop: [nsunloop mainRunLoop] forMode: callback]; self. imageBird = [UIImage imageNamed: @ "QQ20150714-1"]; // set the initial position self. birdX = 1; self. birdY = 1; // set the initial speed self. birdSpeedX = 5; self. birdSpeedY = 5 ;}
5. Drawing
The main difference is that the speed is reversed when a bird hits around.
- (void)drawRect:(CGRect)rect{ [self.imageBird drawInRect:CGRectMake(self.birdX , self.birdY, IMAGEWIDTH, IMAGEHEIGHT)]; if (self.birdX < 0 || (self.birdX > SCREENWIDTH - IMAGEWIDTH) ) { self.birdSpeedX = -self.birdSpeedX; } if (self.birdY < 0 || (self.birdY > SCREENHEIGHT - IMAGEHEIGHT) ) { self.birdSpeedY = -self.birdSpeedY; } self.birdX += self.birdSpeedX; self.birdY += self.birdSpeedY;}
6. complete code
Here, MRView is a child control of self. view. Completely overwrite self. view
# 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 * imageBird; // position of the bird @ property (nonatomic, assign) CGFloat birdX; @ property (nonatomic, assign) CGFloat birdY; // bird speed @ property (nonatomic, assign) CGFloat birdSpeedX; @ property (nonatomic, assign) CGFloat birdSpeedY; @ end @ implementation MRView-(void) awakeFromNib {[self setUpBird];}-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {[self setUpBird];} return self;}-(void) setUpBird {self. backgroundColor = [UIColor blackColor]; CADisplayLink * link = [CADisplayLink displayLinkWithTarget: self selector: @ selector (callback)]; [link addToRunLoop: [nsunloop mainRunLoop] forMode: callback]; self. imageBird = [UIImage imageNamed: @ "QQ20150714-1"]; // set the initial position self. birdX = 1; self. birdY = 1; // set the initial speed self. birdSpeedX = 5; self. birdSpeedY = 5;}-(void) drawRect :( CGRect) rect {[self. imageBird drawInRect: CGRectMake (self. birdX, self. birdY, IMAGEWIDTH, IMAGEHEIGHT)]; if (self. birdX <0 | (self. birdX> SCREENWIDTH-IMAGEWIDTH) {self. birdSpeedX =-self. birdSpeedX;} if (self. birdY <0 | (self. birdY> SCREENHEIGHT-IMAGEHEIGHT) {self. birdSpeedY =-self. birdSpeedY;} self. birdX + = self. birdSpeedX; self. birdY + = self. birdSpeedY;} @ end
7. Result Display
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.