Exemple #1
0
    public bool Sweep2(Vector2 speed, Circle2D c, ref SweepResult2D ret)
    {
        bool  r    = false;
        float time = TimeOfClosestApproach(Center, c.Center, speed, Vector2.zero, R, c.R, out r);

        if (time < 0 || time > 1)
        {
            return(false);
        }
        if (r)
        {
            Circle2D c2 = TestCircle;
            c2.R       = this.R;
            c2.Center  = this.Center;
            c2.Center += time * speed;
            if (c2.Overlap(c))
            {
                //DebugUtil.Log("sweep error ");
                time = 0;// TimeOfClosestApproach(Center, c.Center, speed, Vector2.zero, R, c.R, out r);
                c2   = this;
            }

            ret.time   = time;
            ret.normal = (c2.Center - c.Center).normalized;
            ret.hit    = true;
        }
        return(r);
    }
Exemple #2
0
    public bool Sweep(Vector2 speed, Circle2D c, SweepResult2D ret)
    {
        //create the collision volume for o
        Circle2D b = new Circle2D();

        b.Center = c.Center;
        b.R      = R + c.R;
        float v = speed.magnitude;

        IntersectResult2D[] lcr = new IntersectResult2D[2];
        lcr[0] = new IntersectResult2D();
        lcr[1] = new IntersectResult2D();

        Vector2 rayDir = speed.normalized;


        if (b.IntersectLine2(Center, rayDir, lcr))
        {
            float time = Mathf.Min(lcr[0].depth, lcr[1].depth);
            time /= v;
            if (time < 0 || time > 1)
            {
                return(false);
            }
            Circle2D c2 = new Circle2D();
            c2.R       = this.R;
            c2.Center  = this.Center;
            c2.Center += time * speed;
            if (c2.Overlap(c))
            {
                //DebugUtil.Log("sweep error ");
                time = 0;// TimeOfClosestApproach(Center, c.Center, speed, Vector2.zero, R, c.R, out r);
            }

            ret.pos    = lcr[0].pos + rayDir * R;
            ret.normal = lcr[0].normal;
            ret.time   = time;
            return(true);
        }
        return(false);
    }