C# 改变抛物线';s方向跟随鼠标拖动

C# 改变抛物线';s方向跟随鼠标拖动,c#,math,direction,calculus,C#,Math,Direction,Calculus,我想计算一下,如果物体被拖过水面,水中留下的抛物线轨迹。 我认为使用带有焦点、顶点和准线的轨迹法是可行的。这适用于垂直和水平移动(取决于准线轴),但由于准线不断移动其轴方向,我被卡住了 这就是我现在所做的(为垂直运动而写) 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; [RequiredComponent(typeof(LineRenderer))] 公共类抛物线:单行为{ 公共游戏对象点D、点F、点M; 公共线路; 公共向量3鼠标=向

我想计算一下,如果物体被拖过水面,水中留下的抛物线轨迹。 我认为使用带有焦点、顶点和准线的轨迹法是可行的。这适用于垂直和水平移动(取决于准线轴),但由于准线不断移动其轴方向,我被卡住了

这就是我现在所做的(为垂直运动而写)

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
[RequiredComponent(typeof(LineRenderer))]
公共类抛物线:单行为{
公共游戏对象点D、点F、点M;
公共线路;
公共向量3鼠标=向量3.1,上一个;
公共向量3准线;
公共向量3焦点点、directrixPointMid、directrixPointL、directrixPointR;
公共浮点数方向X长度;
公共决议;
公共向量3 focalLine;
公众关注;
公共向量3p;
公共列表点=新列表();
无效开始()
{
prev=小鼠;
line=GetComponent();
line.positionCount=分辨率;
}
无效更新()
{
if(输入。GetMouseButton(0))
{
Ray-Ray=Camera.main.screenpointoray(输入.鼠标位置);
雷卡斯特击中;

如果(Physics.Raycast)(光线,命中率,数学无穷大,1我将设置更改为使用y=x^2,并使所有坐标位于拖动方向的局部

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Parabola : MonoBehaviour {

public GameObject linePrefab, pointV;

public float offset, amp, speed;
public int freq;
public float directrixLength;
public int resolution;

private Vector3 mouse, prev;
private Vector3 directrix;
private Vector3 focusPoint, directrixPointMid, directrixPointL, directrixPointR;
private Vector3 focalLine;
private Vector3 p;
private List<List<Vector3>> pointsR = new List<List<Vector3>>();
private List<LineRenderer> lines = new List<LineRenderer>();
public Renderer render;

void Start () 
{
    render.material.SetInt("_Length", resolution);
    render.material.SetVectorArray("_Points", new Vector4[resolution]); 

    prev = mouse;
    int i = 0;
    while(i <= freq)
    {
        GameObject g = Instantiate(linePrefab);
        g.transform.SetParent(transform);
        g.transform.localPosition = Vector3.zero;
        LineRenderer line = g.GetComponent<LineRenderer>();
        line.positionCount = resolution;
        lines.Add(line);
        List<Vector3> lR = new List<Vector3>();
        pointsR.Add(lR);
        i++;
    }
}

void FixedUpdate () 
{
    if(Input.GetMouseButton(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if(Physics.Raycast(ray, out hit, Mathf.Infinity, 1<<LayerMask.NameToLayer("Plane")))
        {
            mouse = new Vector3(hit.point.x,0,hit.point.z);
            StopCoroutine(CalculateParabola(mouse));
            StartCoroutine(CalculateParabola(mouse));
            transform.position = mouse;
            transform.forward = -focalLine;
        }
    }
}
IEnumerator CalculateParabola(Vector3 v)
{
    while(prev != v)
    {
        focalLine = prev - v;
        //calculate a parabola for each n in frequency, make it go wider the higher n is
        for(int n=0; h<=freq; n++)
        {
            pointsR[n] = new List<Vector3>();
            float s = Vector3.Distance(prev.normalized,v.normalized)*speed;
            for(float i = -directrixLength/2; i<=(directrixLength+1)/2; i+=directrixLength/resolution)
            {
                p.x = pointV.transform.localPosition.x+(i*(freq-n+Time.fixedDeltaTime*s));
                p.z = ((amp*(freq-n))*(Time.fixedDeltaTime*s))*-Mathf.Pow(p.x,2)+(1+offset*n);
                pointsR[n].Add(p);
            }
            lines[h].SetPositions(pointsR[n].ToArray());
            //attempt to translate the points to a shader, only for the first parabola for now
            Vector4[] renderPoints = new Vector4[resolution];
            for(int i=0; i<resolution; i++)
            {
                renderPoints[i] = transform.TransformPoint(pointsR[0][i]) - render.transform.position;
            }
            render.material.SetVectorArray("_Points", renderPoints); 
        }
        yield return new WaitForSeconds(0.1f);
        prev = v;
    }
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类抛物线:单行为{
公共游戏对象线预制,pointV;
公共浮动偏移量,安培,速度;
公共国际频率;
公共浮点数方向X长度;
公共决议;
私人矢量3鼠标,prev;
专用矢量3准线;
专用矢量3焦点、directrixPointMid、directrixPointL、directrixPointR;
私有向量3 focalLine;
私有向量3p;
私有列表点sr=新列表();
私有列表行=新列表();
公共服务;
无效开始()
{
render.material.SetInt(“长度”,分辨率);
render.material.SetVectorArray(“_点”,新矢量4[分辨率]);
prev=小鼠;
int i=0;

而(我)那是什么“抛物线轨迹”呢?这应该是弓形波吗?那真的是抛物线吗?抛物线的特征是什么?我假设一个点形物体会留下一个三角形(或2d圆锥体)弓形波,而对于扩展对象,弓形波的形状取决于对象的形状。因此,请尝试添加其他约束以唯一地描述所需的抛物线。是的,在我使抛物线跟随鼠标拖动的方向后,我将添加变量以更改陡度等。但这仍然不能解决我的问题我把它改成只使用y=x^2;(或者在我的例子中是z=x^2;)并使所有东西都是本地的。所以现在它可以工作了。如果我得到了完美的答案,我会把它作为一个答案发布。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Parabola : MonoBehaviour {

public GameObject linePrefab, pointV;

public float offset, amp, speed;
public int freq;
public float directrixLength;
public int resolution;

private Vector3 mouse, prev;
private Vector3 directrix;
private Vector3 focusPoint, directrixPointMid, directrixPointL, directrixPointR;
private Vector3 focalLine;
private Vector3 p;
private List<List<Vector3>> pointsR = new List<List<Vector3>>();
private List<LineRenderer> lines = new List<LineRenderer>();
public Renderer render;

void Start () 
{
    render.material.SetInt("_Length", resolution);
    render.material.SetVectorArray("_Points", new Vector4[resolution]); 

    prev = mouse;
    int i = 0;
    while(i <= freq)
    {
        GameObject g = Instantiate(linePrefab);
        g.transform.SetParent(transform);
        g.transform.localPosition = Vector3.zero;
        LineRenderer line = g.GetComponent<LineRenderer>();
        line.positionCount = resolution;
        lines.Add(line);
        List<Vector3> lR = new List<Vector3>();
        pointsR.Add(lR);
        i++;
    }
}

void FixedUpdate () 
{
    if(Input.GetMouseButton(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if(Physics.Raycast(ray, out hit, Mathf.Infinity, 1<<LayerMask.NameToLayer("Plane")))
        {
            mouse = new Vector3(hit.point.x,0,hit.point.z);
            StopCoroutine(CalculateParabola(mouse));
            StartCoroutine(CalculateParabola(mouse));
            transform.position = mouse;
            transform.forward = -focalLine;
        }
    }
}
IEnumerator CalculateParabola(Vector3 v)
{
    while(prev != v)
    {
        focalLine = prev - v;
        //calculate a parabola for each n in frequency, make it go wider the higher n is
        for(int n=0; h<=freq; n++)
        {
            pointsR[n] = new List<Vector3>();
            float s = Vector3.Distance(prev.normalized,v.normalized)*speed;
            for(float i = -directrixLength/2; i<=(directrixLength+1)/2; i+=directrixLength/resolution)
            {
                p.x = pointV.transform.localPosition.x+(i*(freq-n+Time.fixedDeltaTime*s));
                p.z = ((amp*(freq-n))*(Time.fixedDeltaTime*s))*-Mathf.Pow(p.x,2)+(1+offset*n);
                pointsR[n].Add(p);
            }
            lines[h].SetPositions(pointsR[n].ToArray());
            //attempt to translate the points to a shader, only for the first parabola for now
            Vector4[] renderPoints = new Vector4[resolution];
            for(int i=0; i<resolution; i++)
            {
                renderPoints[i] = transform.TransformPoint(pointsR[0][i]) - render.transform.position;
            }
            render.material.SetVectorArray("_Points", renderPoints); 
        }
        yield return new WaitForSeconds(0.1f);
        prev = v;
    }
}
}