【问题标题】:2D projectile trajectory prediction (unity3d)2D弹丸轨迹预测(unity3d)
【发布时间】:2013-12-01 21:51:24
【问题描述】:

(使用 unity3d 4.3 2d,它使用 box2d 类似物理)。

我在预测轨迹方面遇到问题 我正在使用:

Vector2 startPos;
float power = 10.0f;
float interval = 1/30.0f; 
GameObject[] ind;


void Start (){
        transform.rigidbody2D.isKinematic = true;
        ind = new GameObject[dots];
        for(int i = 0; i<dots; i++){
            GameObject dot = (GameObject)Instantiate(Dot);
            dot.renderer.enabled = false;
            ind[i] = dot;
        }
    }

void Update (){
        if(shot) return;
        if(Input.GetAxis("Fire1") == 1){
            if(!aiming){
                aiming = true;
                startPos = Input.mousePosition;
                ShowPath();
            }
            else{
                CalculatePath();
            }

        }
        else if(aiming && !shot){
            transform.rigidbody2D.isKinematic =  false; 
            transform.rigidbody2D.AddForce(GetForce(Input.mous  ePosition));        
            shot = true;
            aiming = false;
            HidePath();
        }
}

Vector2 GetForce(Vector3 mouse){
        return (new Vector2(startPos.x, startPos.y)- new Vector2(mouse.x, mouse.y))*power;

}

void CalculatePath(){
        ind[0].transform.position = transform.position; //set frist dot to ball position
        Vector2 vel = GetForce(Input.mousePosition); //get velocity

        for(int i = 1; i < dots; i++){          
            ind[i].renderer.enabled = true; //make them visible
            Vector3 point = PathPoint(transform.position, vel, i); //get position of the dot 
            point.z = -1.0f;
            ind[i].transform.position = point;
        } 
    }

    Vector2 PathPoint(Vector2 startP, Vector2 startVel, int n){
               //Standard formula for trajectory prediction
        float t = interval;
        Vector2 stepVelocity = t*startVel;
        Vector2 StepGravity = t*t*Physics.gravity;

        Vector2 whattoreturn = ((startP + (n * stepVelocity)+(n*n+n)*StepGravity) * 0.5f);

        return whattoreturn;
    }

使用这个,我得到了错误的轨迹。 1. 就好像重力根本不会拖曳轨迹,是的,我知道重力很弱,因为:

t*t*Physics.gravity = 0.03^2 * vector2(0, -9.8) = vector2(0, -0.00882)

但这就是公式:S 2.由于重力低,速度太强。

这是视频: http://tinypic.com/player.php?v=1z50w3m&s=5

轨迹公式形式: http://www.iforce2d.net/b2dtut/projected-trajectory

我该怎么办?

我发现如果我设置 StepGravity 到更强大的东西,例如 (0, -0.1) 并将 startVel 除以 8 我得到了几乎正确的轨迹,但我不想要那样,我需要真正的轨迹路径。

来自 answer.unity3d.com 的用户说我应该在这里问,因为这里有更多的数学编码人员。

我搜索了很多关于这个问题(我是如何找到那个公式的)。

【问题讨论】:

    标签: unity3d 2d physics game-physics prediction


    【解决方案1】:

    您只计算每一步超过 1/30 秒的重力影响 - 您需要累积计算。第 1 步应以 0.09G 的速度结束,第 2 步以 0.18G 结束,第3 步以 0.27G 等结束。

    这是一个非常简单的示例,它根据起始速度和提供的时间绘制弹道:

    using UnityEngine;
    using System.Collections;
    
    public class grav : MonoBehaviour {
    
        public Vector3 StartVelocity;
        public float PredictionTime;
        private Vector3 G;
    
        void OnDrawGizmos()
        {
            if (G == Vector3.zero)
            {
                 // a hacky way of making sure this gets initialized in editor too...
                // this assumes 60 samples / sec
                G = new Vector3(0,-9.8f,0) / 360f;
            }
            Vector3 momentum = StartVelocity;
            Vector3 pos = gameObject.transform.position;
            Vector3 last = gameObject.transform.position;
            for (int i = 0; i < (int) (PredictionTime * 60); i++)
            {
                momentum += G;
                pos += momentum;
                Gizmos.DrawLine(last, pos);
                last = pos;
            }
    
        }
    }
    

    在您的版本中,您希望在我绘制 Gizmo 的位置绘制点,但这是相同的想法,除非我误解了您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多