類似憤怒小鳥的飛行彈道,小鳥飛行彈道

來源:互聯網
上載者:User

類似憤怒小鳥的飛行彈道,小鳥飛行彈道


拋體運動的類型:
       "很多子彈不僅垂直運動而且追隨著水平的運動。那就,當他們向上移動或向下運動時也正在水平方向移動。彈體的運動 — — 橫向和縱向

運動的兩個組成部分。
垂直運動:
             在垂直運動,重力作用在物體上,並給予負加速度"-9.8 m/s²"(重心加速度)。這意味著物體的速度在每一秒減小-9.8 米/² 。自由落體的速度是 V = g * t。 如果我們有初始速度那麼,物體下落速度方程: V = Vi + g * t 加速度是-9.8 m/s²,在做自由落體時距離的計算方程 ;S= 1/2 * g * t * t ;考慮對象的初始速度情況下的

公式計算 ;S = Vi * t - 1/2 * g * t * t ;距離被減去,因為 g 的方向是向下。
橫向運動:
           在水平運動,沒有外力作用在水平方向勻速運動。因而在此基礎上,是恒定的速度的 X 分量,在 X 方向的加速度為零。下面給出了用於計算距離和速度方程。S = v * t ; 下面是簡單的 c# 代碼,將顯示球的彈道路徑時它會沿著路徑拋出。
          註: 添加如下指令碼到槍炮對象上。 建立 prefebs 球和軌跡點將運行時執行個體化。 球必須有Collider和Rigidbody。

       


 

using UnityEngine;using System.Collections;using System.Collections.Generic;public class CannonScript : MonoBehaviour {// TrajectoryPoint and Ball will be instantiated    public GameObject TrajectoryPointPrefeb;    public GameObject BallPrefb;        private GameObject ball;    private bool isPressed, isBallThrown;    private float power = 25;    private int numOfTrajectoryPoints = 30;    private List trajectoryPoints;    //---------------------------------------        void Start ()    {        trajectoryPoints = new List();        isPressed = isBallThrown =false;//   TrajectoryPoints are instatiated        for(int i=0;i<numOfTrajectoryPoints;i++)        {            GameObject dot= (GameObject) Instantiate(TrajectoryPointPrefeb);            dot.renderer.enabled = false;            trajectoryPoints.Insert(i,dot);        }    }    //---------------------------------------        void Update ()     {        if(isBallThrown)            return;        if(Input.GetMouseButtonDown(0))        {            isPressed = true;            if(!ball)                createBall();        }        else if(Input.GetMouseButtonUp(0))        {            isPressed = false;            if(!isBallThrown)            {                throwBall();            }        }    // when mouse button is pressed, cannon is rotated as per mouse movement and projectile trajectory path is displayed.        if(isPressed)        {            Vector3 vel = GetForceFrom(ball.transform.position,Camera.main.ScreenToWorldPoint(Input.mousePosition));            float angle = Mathf.Atan2(vel.y,vel.x)* Mathf.Rad2Deg;            transform.eulerAngles = new Vector3(0,0,angle);            setTrajectoryPoints(transform.position, vel/ball.rigidbody.mass);        }    }    //---------------------------------------        // Following method creates new ball    //---------------------------------------        private void createBall()    {        ball = (GameObject) Instantiate(BallPrefb);        Vector3 pos = transform.position;        pos.z=1;        ball.transform.position = pos;        ball.SetActive(false);    }    //---------------------------------------    // Following method gives force to the ball    //---------------------------------------        private void throwBall()    {        ball.SetActive(true);            ball.rigidbody.useGravity = true;        ball.rigidbody.AddForce(GetForceFrom(ball.transform.position,Camera.main.ScreenToWorldPoint(Input.mousePosition)),ForceMode.Impulse);        isBallThrown = true;    }    //---------------------------------------    // Following method returns force by calculating distance between given two points    //---------------------------------------        private Vector2 GetForceFrom(Vector3 fromPos, Vector3 toPos)    {        return (new Vector2(toPos.x, toPos.y) - new Vector2(fromPos.x, fromPos.y))*power;    }    //---------------------------------------        // Following method displays projectile trajectory path. It takes two arguments, start position of object(ball) and initial velocity of object(ball).    //---------------------------------------        void setTrajectoryPoints(Vector3 pStartPosition , Vector3 pVelocity )    {        float velocity = Mathf.Sqrt((pVelocity.x * pVelocity.x) + (pVelocity.y * pVelocity.y));        float angle = Mathf.Rad2Deg*(Mathf.Atan2(pVelocity.y , pVelocity.x));        float fTime = 0;                fTime += 0.1f;        for (int i = 0 ; i < numOfTrajectoryPoints ; i++)        {            float dx = velocity * fTime * Mathf.Cos(angle * Mathf.Deg2Rad);            float dy = velocity * fTime * Mathf.Sin(angle * Mathf.Deg2Rad) - (Physics2D.gravity.magnitude * fTime * fTime / 2.0f);            Vector3 pos = new Vector3(pStartPosition.x + dx , pStartPosition.y + dy ,2);            trajectoryPoints[i].transform.position = pos;            trajectoryPoints[i].renderer.enabled = true;            trajectoryPoints[i].transform.eulerAngles = new Vector3(0,0,Mathf.Atan2(pVelocity.y - (Physics.gravity.magnitude)*fTime,pVelocity.x)*Mathf.Rad2Deg);            fTime += 0.1f;        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.