[Unity数学]两点之间抛物线移动

一元二次方程

 

一、基本知识

一般式:y = a*x^2 + b*x + c (a != 0) ,其中 a>0 抛物线开口向上,a<0 抛物线开口向下。|a| 越大开口越小。

顶点式:y = a*(x-h)^2 + k 点(h,k)是抛物线的顶点。h=-b/2a,k=(4ac-b^2)/4a

焦点式:y = a*(x-x1)*(x-x2)。(x1,0)(x2,0)是抛物线与X轴的交点,设Δ=b^2-4ac,求根公式 x1,x2=[-b±√Δ]/2a

 

、问题分析

抛物线主要解决高度的问题就好了。

在直角坐标系中,不妨设y轴是高度,x轴是时间,

已知Δ>0,两点pointA,pointB是方程的两个根x1,x2

 

pointA是原点,则c=0

此时需要两个参数,一个是a,控制抛物线开口朝向和大小。

另一个是速度speed,两点之间的距离dis/速度speed=时间time。

x轴是时间轴deltaTime,顶点式 h=-b/2a=time/2,所以b=-a*time

∵ a是参数,b=-a*(dis/speed),c=0

∴ y = a*x^2 + b*x + c = a*deltaTime*deltaTime - a*(dis/speed)*deltaTime

三、代码实现

using UnityEngine;

public class Parabola : MonoBehaviour
{
    public Transform tr1;
    public Transform tr2;
    public GameObject go;

    public float speed = 10;
    public float a = 10;

    void Start()
    {
        Reset();
        InvokeRepeating("Reset", 2, 5);
    }

    float deltaTime = 0;
    void Update()
    {
        a = -Mathf.Abs(a);
        speed = Mathf.Abs(speed);
        deltaTime += Time.deltaTime;
        
        Vector3 vec = (tr2.position - tr1.position).normalized;
        float dis = Vector3.Distance(tr1.position, tr2.position);

        float x = deltaTime * speed;
        float y = a * deltaTime * deltaTime -a * (dis / speed) * deltaTime;
        go.transform.position = vec * x + Vector3.up * y;
    }

    void Reset()
    {
        deltaTime = 0;
        go.transform.position = tr1.position;
    }
}

 

三维抛物线

    void Update()
    {
        start = sTr.position;
        end = eTr.position;

        Vector4 y = new Vector4(0, 1, 0, 0);
        Vector4 x = (end - start).normalized;
        Vector4 z = Vector3.Cross(x, y);
        Matrix4x4 planeToWorld = new Matrix4x4(x, y, z, new Vector4(start.x, start.y, start.z, 1));

        Vector3 startPlane = planeToWorld.inverse.MultiplyPoint3x4(start);
        Vector3 endPlane = planeToWorld.inverse.MultiplyPoint3x4(end);
        //最高点
        Vector3 highestPos = (endPlane + startPlane) / 2.0f + Vector3.up * h;
        float p = Mathf.Pow(startPlane.x - highestPos.x, 2) / (-2 * (startPlane.y - highestPos.y));
        for (int i = 0; i < density; i++)
        {
            float posX = startPlane.x + (endPlane.x - startPlane.x) * (i / (float)density);
            float posY = Mathf.Pow(posX - highestPos.x, 2) / (-2 * p) + highestPos.y;
            Vector3 worldPos = planeToWorld.MultiplyPoint3x4(new Vector3(posX, posY, 0));
            line.SetPosition(i, worldPos);
        }
    }

 

 

 

你可能感兴趣的:(Unity,Math)