简体   繁体   中英

2d Trajectory Line Prediction not acurate

I'm current facing a problem where in my trajectory line is very inaccurate

Please see the attached image below

在此输入图像描述

Here's where it goes

在此输入图像描述

Here's my code so far

private void Start()
    {
        line = new GameObject[maxDots];
        for (int i = 0; i < line.Length; i++)
        {
            var go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            go.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
            line[i] = go;
        }

        startPos = transform.position;
    }

on my OnMouseUp is for shooting the ball

    //TEMPORARY
    private void OnMouseUp()
    {
        // Disable IsKenematic
        GetComponent<Rigidbody2D>().isKinematic = false;

        // Add the Force
        Vector2 dir = startPos - (Vector2)transform.position;
        GetComponent<Rigidbody2D>().AddForce(dir * force);
        //Remove the script (not the gameobject)
        Destroy(this);
    }

And on my OnMouseDrag is for the ball to keep in radius cause I set a limit for the dragging of the ball

    private void OnMouseDrag()
    {
        DisplayLine();
        //Convert mouse potision to world position
        Vector2 p = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        //Keep it in a certain radius
        float radius = 1.8f;
        Vector2 dir = p - startPos;

        if (dir.sqrMagnitude > radius)
        {
            dir = dir.normalized * radius;

            //Set position
            transform.position = startPos + dir;
        }
    }

Here's my method of displaying the line

    void DisplayLine()
    {
        line[0].transform.position = transform.position;
        Vector3 v3 = transform.position;
        float y = (forces * (home - transform.position)).y;
        float t = 0.0f;
        v3.y = 0.0f;

        for(int i = 1; i < line.Length; i++)
        {
            v3 += forces * (home - transform.position) * spacing;
            t += spacing;
            v3.y = y * t + 0.5f * Physics2D.gravity.y * t * t + transform.position.y;
            line[i].transform.position = v3;
        }
    }

What I am trying to do is that create a trajectory prediction line for my game I know I am almost there but still couldn't get the exact output that I want. Could someone help me. Thank you.

Use the same function to calculate both. Then you won't have the problem also any change on trajectory calculation results in only one change not two.

Solved it

line[0].transform.position = transform.position;
        Vector2 v2 = transform.position;
        float y = (force *( startPos - (Vector2)transform.position)).y;
        float t = 0.0f;
        v2.y = 0.0f;

        for(int i = 1; i < line.Length; i++)
        {
            v2 += force * (startPos - (Vector2)transform.position) * spacing;
            t += spacing;
            v2.y = y * t + 0.5f * Physics2D.gravity.y * t * t + transform.position.y;
            line[i].transform.position = v2;
        }

I need to change from vector3 to vector2 so I need to cast (vector2) between those transform.positions.y

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM