Physical knowledge application in iOS animations-acceleration of gravity

Source: Internet
Author: User

Flat Throw Motion

Remember the first time to play Angry Birds, pop the bird that moment, the birds in the air to draw a graceful arc? Do you remember the scene where the birds bounce and fall on the ground? Here is how to realize the flat throw motion.

1. Physical knowledge of flat throw motion


We can see that after the object is thrown, the velocity is constant in the x-axis direction. But as time changes the y-axis direction the speed is increasing (the length of the red Arrow).
By the basic physics knowledge we know:

    • VX = VX
    • Vy = Vy + at

When simulating in a computer, we can assume that T is a unit time, so Vy = Vy + A in the y-axis direction. Here we can take a constant for a, which represents the acceleration value. In real life a = 9.8 (or 10).

In this example: the bird will have a horizontal direction of the initial velocity, and by the downward gravity, that is, the bird has a downward acceleration, if hit the ground will bounce, speed reverse.

2. Code implementation 1. Set the initial amount
- (void)setUpStartValue{    //在屏幕上的初始位置    self.birdX0;    self.birdY0;    //x,y轴上的初始速度    self.birdSpeedX10;    self.birdSpeedY0;    //重力加速度    self.gBirdSpeed9.8;}
2. Core code for simulating the gravity environment
//on the x-axis, the position on the x-axis is equal to the speed at each refresh, plus the previous position     Self. BIRDX + = Self. Birdspeedx;//Each refresh on the y-axis, the bird's speed increases by an acceleration of gravity     Self. birdspeedy + = Self. gbirdspeed;the position on the//y axis equals the increased speed on the y-axis, plus the previous position     Self. BirdY + = Self. birdspeedy;//Hit the x-axis screen edge, the position on the x-axis is not changed    if( Self. BIRDX >= Screenwidth-imagewidth) { Self. BIRDX = Screenwidth-imagewidth; }//To determine if touching the ground, if touching the window border, the VY adjusts to the opposite direction    if( Self. BirdY >= Screenheight-imageheight) { Self. BirdY = Screenheight-imageheight; Self. Birdspeedy =- Self. birdspeedy; }
3. Complete code of the project
#import "MRView.h" #define ScreenHeight [UIScreen mainscreen].bounds.size.height#define ScreenWidth [UIScreen mainscreen].bounds.size.width#define ImageWidth#define ImageHeight @interface mrview()@property(nonatomic,Strong)UIImage*imagebackgound;@property(nonatomic,Strong)UIImage*imagebird;@property(nonatomic,Assign)CGFloatBIRDX;@property(nonatomic,Assign)CGFloatBirdY;@property(nonatomic,Assign)CGFloatBirdspeedx;@property(nonatomic,Assign)CGFloatBirdspeedy;@property(nonatomic,Assign)CGFloatGbirdspeed;@end @implementation mrview - (void) awakefromnib{ Self. Contentmode= Uiviewcontentmoderedraw; Self. BackgroundColor= [UicolorBlackcolor]; [ SelfSetupstartvalue];} -(Instancetype) initWithFrame: (CGRect) frame{if( Self= [SuperInitwithframe:frame]) {[ SelfSetupstartvalue]; }return  Self;} - (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{[ SelfSetupstartvalue]; Cadisplaylink *link = [Cadisplaylink displaylinkwithtarget: SelfSelector@selector(Setneedsdisplay)]; [link Addtorunloop:[nsrunloop Mainrunloop] formode:nsdefaultrunloopmode];} - (void) setupstartvalue{ Self. Imagebackgound= [UIImageimagenamed:@"Img_0465.jpg"]; Self. Imagebird= [UIImageimagenamed:@"Qq20150714-1"]; Self. BIRDX=0; Self. BirdY=0; Self. Birdspeedx=Ten; Self. Birdspeedy=0; Self. Gbirdspeed=9;} - (void) DrawRect: (CGRect) rect{[ Self. ImagebirdDrawinrect:cgrectmake ( Self. BIRDX, Self. BirdY, Imagewidth,imageheight)];//on the x-axis, the position on the x-axis is equal to the speed at each refresh, plus the previous position     Self. BIRDX+= Self. Birdspeedx;//Each refresh on the y-axis, the bird's speed increases by an acceleration of gravity     Self. Birdspeedy+= Self. Gbirdspeed;the position on the//y axis equals the increased speed on the y-axis, plus the previous position     Self. BirdY+= Self. Birdspeedy;//Hit the x-axis screen edge, the position on the x-axis is not changed    if( Self. BIRDX>= screenwidth-imagewidth) { Self. BIRDX= Screenwidth-imagewidth; }//To determine if touching the ground, if touching the window border, the VY adjusts to the opposite direction    if( Self. BirdY>= screenheight-imageheight) { Self. BirdY= Screenheight-imageheight; Self. Birdspeedy= - Self. Birdspeedy; }}@end
4. Running Results

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Physical knowledge application in iOS animations-acceleration of gravity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.