コード例 #1
0
    void  Update()
    {
        if (!isCreated || !myLine.active)
        {
            return;
        }
        this.UpdateEndPoints();

        if ((P2 - P1).magnitude <= 0.0f)
        {
            return;
        }
        else
        {
            distance = (P2 - P1).magnitude;

            if (GameController.Instance.currentMode == GameMode.Throwing)
            {
                Length = distance;
            }
            else
            {
                if (isFukeMode)
                {
                    timeSinceFuke += Time.deltaTime;

                    if (isFukeTimeMode)
                    {
                        if (timeSinceFuke > fukeTime)
                        {
                            isFukeMode = false;
                            AudioController.Play("cover");
                            Button_Float.Instance.isCovered = true;
                        }
                        else
                        {
                            Length += TackleParams.Instance.tParams.Params_fukeSpeed;
                        }
                    }
                    else
                    {
                        if (isInWaterAdding)
                        {
                            if (timeSinceFuke > fukeTime)
                            {
                                isInWaterAdding = false;
                            }
                            else
                            {
                                Length = distance + TackleParams.Instance.tParams.Params_fukeSpeed;
                            }
                        }
                        else
                        {
                            Length = distance;
                        }
                    }
                }
            }

            lineSlack = distance - Length;
            UpdateLineProperties((PD2 - PD1).magnitude - Length, Length < distance ? distance : Length);
        }
        if (P1 != prevP1 || P2 != prevP2 || Length != prevL)
        {
            //if changes have been made
            prevP1 = P1;
            prevP2 = P2;
            prevL  = Length;
        }
        else
        {
            // no changes to update
            return;
        }
        //LineRenderer lineRenderer = GetComponent<LineRenderer>();
        // Catenary function
        Catenary.Function f = delegate(double x0, double a0, double v0, double b0)
        {
            return(a0 * Math.Cosh((x0 + v0) / a0) + b0);
        };
        float w = 1.0f;                // / W;
        float l = Length * w;
        // Before rotation
        float x1i = P1.x * w;
        float x2i = P2.x * w;
        float y1i = P1.y * w;
        float y2i = P2.y * w;
        float z1i = P1.z * w;
        float z2i = P2.z * w;

        // Rotating P2
        P2 = new Vector3(P1.x + (new Vector3(P2.x, 0.0f, P2.z) - new Vector3(P1.x, 0.0f, P1.z)).magnitude, P2.y, 0.0f);
        // After rotation
        float  x1     = P1.x * w;
        float  x2     = P2.x * w;
        float  y1     = P1.y * w;
        float  y2     = P2.y * w;
        double a      = 0;
        double v      = 0;
        double b      = 0;
        bool   solved = false;

        if (Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) < l)
        {
            solved = this.Solve(x1, y1, x2, y2, l, out a, out v, out b);
            if (solved)
            {
                double dx    = N <= 1 ? 0 : ((x2 - x1) / (N - 1));
                double dxi   = N <= 1 ? 0 : ((x2i - x1i) / (N - 1));
                double dzi   = N <= 1 ? 0 : ((z2i - z1i) / (N - 1));
                double prevY = y1;
                for (int i = 0; i < N; i++)
                {
                    double x  = x1 + ((double)i) * dx;
                    double xi = x1i + ((double)i) * dxi;
                    double zi = z1i + ((double)i) * dzi;
                    double y  = f(x, a, v, b);
                    if (
                        double.IsNaN(y) ||
                        double.IsInfinity(y) ||
                        Math.Abs(y - prevY) > l
                        )
                    {
                        solved = false;
                        break;
                    }
                    Vector3 pos = new Vector3((float)xi / w, (float)y / w, (float)zi / w);

                    SetPosition(i, pos);
                    prevY = y;
                }
            }
            if (!solved && previousSolved)
            {
                Debug.Log("Catenary could not be solved. Distance between end-points is aparently too short comparing to configured catenary length.");
            }
        }
        if (!solved)
        {
            double dxi = N <= 1 ? 0 : ((x2i - x1i) / (N - 1));
            double dyi = N <= 1 ? 0 : ((y2i - y1i) / (N - 1));
            double dzi = N <= 1 ? 0 : ((z2i - z1i) / (N - 1));
            for (int i = 0; i < N; i++)
            {
                double  xi  = x1i + ((double)i) * dxi;
                double  yi  = y1i + ((double)i) * dyi;
                double  zi  = z1i + ((double)i) * dzi;
                Vector3 pos = new Vector3((float)xi / w, (float)yi / w, (float)zi / w);
                SetPosition(i, pos);
            }
        }
        previousSolved = solved;
    }
コード例 #2
0
ファイル: Catenary.cs プロジェクト: brunoasdfgh/VBF2
    /// <summary>
    /// Update this instance.
    /// </summary>
    void Update()
    {
        this.UpdateEndPoints();
        if ((P2 - P1).magnitude <= 0)
        {
            return;
        }
        this.UpdateLineRenderer();
        if (P1 != prevP1 || P2 != prevP2 || L != prevL || N != prevN || W != prevW || arch != prevArch)
        {
            prevP1   = P1;
            prevP2   = P2;
            prevL    = L;
            prevN    = N;
            prevW    = W;
            prevArch = arch;
        }
        else
        {
            return;
        }
        LineRenderer lineRenderer = GetComponent <LineRenderer>();

        // Catenary function
        Catenary.Function f = delegate(double x0, double a0, double v0, double b0)
        {
            return(a0 * Math.Cosh((x0 + v0) / a0) + b0);
        };
        float w = 1.0f;        // / W;
        float l = L * w;
        // Before rotation
        float x1i = P1.x * w;
        float x2i = P2.x * w;
        float y1i = P1.y * w;
        float y2i = P2.y * w;
        float z1i = P1.z * w;
        float z2i = P2.z * w;

        // Rotating P2
        P2 = new Vector3(P1.x + (new Vector3(P2.x, 0.0f, P2.z) - new Vector3(P1.x, 0.0f, P1.z)).magnitude, P2.y, 0.0f);
        // After rotation
        float  x1     = P1.x * w;
        float  x2     = P2.x * w;
        float  y1     = P1.y * w;
        float  y2     = P2.y * w;
        double a      = 0;
        double v      = 0;
        double b      = 0;
        bool   solved = false;

        if (Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) < l)
        {
            solved = this.Solve(x1, (arch ? -1 : 1) * y1, x2, (arch ? -1 : 1) * y2, l, out a, out v, out b);
            if (solved)
            {
                double dx    = N <= 1 ? 0 : ((x2 - x1) / (N - 1));
                double dxi   = N <= 1 ? 0 : ((x2i - x1i) / (N - 1));
                double dzi   = N <= 1 ? 0 : ((z2i - z1i) / (N - 1));
                double prevY = y1;
                for (int i = 0; i < N; i++)
                {
                    double x  = x1 + ((double)i) * dx;
                    double xi = x1i + ((double)i) * dxi;
                    double zi = z1i + ((double)i) * dzi;
                    double y  = (arch ? -1 : 1) * f(x, a, v, b);
                    if (
                        double.IsNaN(y) ||
                        double.IsInfinity(y) ||
                        Math.Abs(y - prevY) > l
                        )
                    {
                        solved = false;
                        break;
                    }
                    Vector3 pos = new Vector3((float)xi / w, (float)y / w, (float)zi / w);
                    lineRenderer.SetPosition(i, pos);
                    prevY = y;
                }
            }
            if (!solved && previousSolved)
            {
                Debug.Log("Catenary could not be solved. Distance between end-points is aparently too short comparing to configured catenary length.");
            }
        }
        if (!solved)
        {
            double dxi = N <= 1 ? 0 : ((x2i - x1i) / (N - 1));
            double dyi = N <= 1 ? 0 : ((y2i - y1i) / (N - 1));
            double dzi = N <= 1 ? 0 : ((z2i - z1i) / (N - 1));
            for (int i = 0; i < N; i++)
            {
                double  xi  = x1i + ((double)i) * dxi;
                double  yi  = y1i + ((double)i) * dyi;
                double  zi  = z1i + ((double)i) * dzi;
                Vector3 pos = new Vector3((float)xi / w, (float)yi / w, (float)zi / w);
                lineRenderer.SetPosition(i, pos);
            }
        }
        previousSolved = solved;
    }