Unity game Development Math and Physics 4 (adding gravity to Object motion)

Source: Internet
Author: User
Tags cos sin time 0

Adding gravity to the motion of an object

Implementation NOTE:

    • Using the series overlay algorithm
    • Y + = VY; The acceleration on the position
    • VY + = GR;//acceleration on SPEED
    • GR is the gravitational acceleration, the acceleration unit used in the computer is a special dot/f^2 ( pixel/square frame ) instead of 9.8 m/s^2 (M/s)
    • Differential is an operation, is a differential coefficient of operation, and the differential coefficient refers to the function about a value of the rate of change , this concept in game development is often used to examine the speed of a function change; F (x) about the rate of change of x, the basic differential formula
    • D/DX (x) = 1
    • D/DX (x^2) = 2x
    • D/DX (x^3) = 3x
    • D/DX (x^n) = nx^ (n-1)
    • D/dx (sin x) = cos x
    • D/DX (cos x) =-sin x
    • The derivative of a synthetic function can be regarded as "the differential of an equation that contains another layer of function, which can be converted into a derivative of the outer layer function multiplied by the differentiation of the inner layer function"
    • Progression is the result of adding up each item of a sequence.
    • The integral and the series have similar parts, from the formal point of view is the differential inverse
    • Parabolic motion program with integral production
    • x = VX * t;
    • y = (0.5f * GR * t * t) + (VY * t) + 200.0f;

    • The following code uses integrals to make parabolic

usingUnityengine;usingSystem.Collections;//Adding gravity to the motion of the object Public classParabolicmotiontest:monobehaviour {//X position of Object    floatPosX =0;//Y position of Object    floatPosY =0;//speed of the object in the x direction    floatSpeedx =0;//speed of the object in the y direction    floatSpeedY =-8;//The top right pixel of the screen in world space coordinatesVector3 Screenrighttoppos;//The left lower pixel of the screen in world space coordinatesVector3 Screenleftbottompos;half width of//box    floatBoxhalfwidth;//Run time    floatRunTime;//Gravitational acceleration    Const floatGR =0.4F//Where the object was bornVector2 Bornpos;voidStart () {//Convert the pixel on the right of the screen to the coordinates of world spaceScreenrighttoppos = Camera.main.ScreenToWorldPoint (NewVector3 (Screen.width, Screen.height,0));//Convert the pixel at the bottom right of the screen to the coordinates of world spaceScreenleftbottompos = Camera.main.ScreenToWorldPoint (NewVector3 (0,0,0));//box half width, because box is squareBoxhalfwidth = transform.localscale.x *0.5F//Birth location upper left cornerBornpos =NewVector2 (screenleftbottompos.x + boxhalfwidth, screenrighttoppos.y-boxhalfwidth);//Initial position screen upper left cornerTransform.localposition =NewVector3 (Bornpos.x, Bornpos.y,0); }voidUpdate () {//Detect, if the object is out of the interface, let it go back to the Bornpos point        if(posx-boxhalfwidth >= screenrighttoppos.x | | PosX + boxhalfwidth <= screenleftbottompos.x | | Posy-boxhalfwidth >= Screenrighttoppos.y | | PosY + boxhalfwidth <= screenleftbottompos.y) {//upper left corner of the screenPosX = bornpos.x; PosY = Bornpos.y;//Run time 0RunTime =0; }//x direction of displacement to a value per frame of timePosX = Speedx * runTime;//y direction of displacement to a value per frame of timePosY = (0.5F * GR * Runtime * Runtime) + (SpeedY * runtime) + Bornpos.y;//Time change valueRunTime + = Time.deltatime;//Set the position of the frameTransform.localposition =NewVector3 (PosX, PosY,0); }}

Unity game Development Math and Physics 4 (adding gravity to Object motion)

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.