예제 #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);
    }
예제 #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);
    }
예제 #3
0
    public static bool SweepCircles(Circle2D target, List <Circle2D> Circles, Vector2 speed, ref SweepResult2D result)
    {
        float sweep_time = 1;
        bool  hit        = false;

        result.hit = false;
        if (speed == Vector2.zero)
        {
            return(false);
        }

        for (int i = 0; i < Circles.Count; i++)
        {
            if (target.Sweep2(speed, Circles[i], ref result))
            {
                if (result.time < sweep_time)
                {
                    hit        = true;
                    sweep_time = result.time;
                    result.c   = Circles[i];
                    result.hit = true;
                }
            }
        }
        //for test

        return(hit);
    }