예제 #1
0
    public override bool TestCollsionVsAABB(AABBCollisionHull2D other, ref Collision c)
    {
        // see AABB
        // 1....

        return(false);
    }
    public override bool TestCollsionVsAABB(AABBCollisionHull2D other /*,ref Collision c*/)
    {
        // calculate closest point by clamping circle center on each dimension
        // passes if closest point vs circle passes
        // 1....


        // check x max and min
        float checkX = Mathf.Clamp(this.position.x, other.position.x - (other.rectWidth * 0.5f), other.position.x + (other.rectWidth * 0.5f));

        // check y max and min
        float checkY = Mathf.Clamp(this.position.y, other.position.y - (other.rectHeight * 0.5f), other.position.y + (other.rectHeight * 0.5f));

        checkX = this.position.x - checkX;
        checkY = this.position.y - checkY;

        // get distance
        float distance = (checkX * checkX) + (checkY * checkY);

        if ((distance) <= (radius * radius))
        {
            return(true);
        }

        else
        {
            return(false);
        }
    }
    public override bool TestCollsionVsAABB(AABBCollisionHull2D other)
    {
        // for each dimension, max extent of A greater than min extent of B
        // 1....

        if (((this.position.x < other.position.x + other.rectWidth && this.position.x + this.rectWidth > other.position.x &&
              this.position.y < other.position.y + other.rectHeight && this.position.y + this.rectHeight > other.position.y)))
        {
            return(true);
        }

        else
        {
            return(false);
        }
    }
예제 #4
0
    public override bool TestCollisionVsAABB(AABBCollisionHull2D other, ref Collision c)
    {
        updatePosition();
        other.updatePosition();
        // init corners.
        Vector2 myBLC = Vector2.zero;
        Vector2 myTRC = myBLC;
        Vector2 oBLC  = myBLC;
        Vector2 oTRC  = myBLC;

        getDimensions(ref myBLC, ref myTRC);
        other.getDimensions(ref oBLC, ref oTRC);


        bool isColiding = checkOverlap(myTRC, myBLC, oTRC, oBLC);

        return(isColiding);
    }
예제 #5
0
    public override bool TestCollsionVsAABB(AABBCollisionHull2D other, ref Collision c)
    {
        // calculate closest point by clamping circle center on each dimension
        // passes if closest point vs circle passes
        // 1....
        float rectX = other.position.x;
        float rectY = other.position.y;

        float deltaX = this.position.x - Mathf.Max(rectX, Mathf.Min(this.position.x, rectX + rectWidth));
        float deltaY = this.position.y - Mathf.Max(rectY, Mathf.Min(this.position.y, rectY + rectHeight));

        float distance = deltaX * deltaX + deltaY * deltaY;

        if ((distance) < (other.radius * other.radius))
        {
            return(true);
        }

        else
        {
            return(false);
        }
    }
예제 #6
0
    public override bool TestCollisionVsAABB(AABBCollisionHull2D other, ref Collision c)
    {
        updatePosition();
        other.updatePosition();
        // find the closest point to the circle on the box.
        // - Clamp the center of the circle to be in dimensions of box, gets closest point?
        Vector2 closest_point;
        Vector2 blc = Vector2.zero, trc = Vector2.zero; // bot left and top right of AABB

        other.getDimensions(ref blc, ref trc);
        closest_point.x = Mathf.Clamp(position.x, blc.x, trc.x);
        closest_point.y = Mathf.Clamp(position.y, blc.y, trc.y);

        // if closest point is within circle, pass. (point vs circle test). square for efficiency
        bool isColiding = (position - closest_point).SqrMagnitude() < radius * radius;

        // Contact Points
        Vector2 contact = closest_point;
        // Normal should still go to center of circle.
        Vector2 normal = (position - contact).normalized;

        return(isColiding);
    }
 // Start is called before the first frame update
 void Start()
 {
     otherCircle = colliderTarget.GetComponent <CirlceCollisionHull2D>();
     otherRect   = colliderTarget.GetComponent <AABBCollisionHull2D>();
     otherOBB    = colliderTarget.GetComponent <OBBCollisionHull2D>();
 }
예제 #8
0
 public abstract bool TestCollsionVsAABB(AABBCollisionHull2D other);
예제 #9
0
 public override bool TestCollsionVsAABB(AABBCollisionHull2D other /*,ref Collision c*/)
 {
     // see AABB
     // 1....
     return(other.TestCollsionVsOBB(this.GetComponent <OBBCollisionHull2D>()));
 }
예제 #10
0
 public virtual bool TestCollisionVsAABB(AABBCollisionHull2D other, ref Collision c)
 {
     return(false);
 }
예제 #11
0
 public abstract bool TestCollsionVsAABB(AABBCollisionHull2D other /*,ref Collision c*/);
예제 #12
0
    public override bool TestCollisionVsOBB(OBBCollisionHull2D other, ref Collision c)
    {
        if (pauseForDebug)
        {
            pauseForDebug = false;
        }
        updatePosition();
        other.updatePosition();
        // TODO: remove extra OBB corners when projecting onto axis where they'll be duplicated.
        // see vs Circle

        // same as AABB-OBB part2, twice
        Vector2 XminYmin = Vector2.zero;
        Vector2 XmaxYmax = Vector2.zero;
        Vector2 XminYMax = Vector2.zero;
        Vector2 XmaxYmin = Vector2.zero;
        Vector2 oXYMax = Vector2.zero, oXYMin = Vector2.zero;
        Vector2 mXYMax = Vector2.zero, mXYMin = Vector2.zero;



        // 1. project others corners onto this up axis
        XminYmin = other.topRightTranslated();
        XmaxYmax = other.botRightTranslated();
        XminYMax = other.botLeftTranslated();
        XmaxYmin = other.topLeftTranslated();
        projectCornersOnAxis(transform.up, ref oXYMax, ref oXYMin);

        // project this corners onto this up axis
        XminYmin = topRightTranslated();
        XmaxYmax = botRightTranslated();
        XminYMax = botLeftTranslated();
        XmaxYmin = topLeftTranslated();
        projectCornersOnAxis(transform.up, ref mXYMax, ref mXYMin);
        // check for overlap
        bool isCollidingOnThisUpAxis = AABBCollisionHull2D.checkOverlap(oXYMax, oXYMin, mXYMax, mXYMin);


        // 2. project others corners onto this right axis
        XminYmin = other.topRightTranslated();
        XmaxYmax = other.botRightTranslated();
        XminYMax = other.botLeftTranslated();
        XmaxYmin = other.topLeftTranslated();
        projectCornersOnAxis(transform.right, ref oXYMax, ref oXYMin);

        //project this corners onto this right axis
        XminYmin = topRightTranslated();
        XmaxYmax = botRightTranslated();
        XminYMax = botLeftTranslated();
        XmaxYmin = topLeftTranslated();
        projectCornersOnAxis(transform.right, ref mXYMax, ref mXYMin);
        // check for overlap
        bool isCollidingOnThisRightAxis = AABBCollisionHull2D.checkOverlap(oXYMax, oXYMin, mXYMax, mXYMin);


        // 3. project others corners onto others up axis
        XminYmin = other.topRightTranslated();
        XmaxYmax = other.botRightTranslated();
        XminYMax = other.botLeftTranslated();
        XmaxYmin = other.topLeftTranslated();
        projectCornersOnAxis(other.transform.up, ref oXYMax, ref oXYMin);

        // project this corners onto others up axis
        XminYmin = topRightTranslated();
        XmaxYmax = botRightTranslated();
        XminYMax = botLeftTranslated();
        XmaxYmin = topLeftTranslated();
        projectCornersOnAxis(other.transform.up, ref mXYMax, ref mXYMin);

        bool isCollidingOnOtherUpAxis = AABBCollisionHull2D.checkOverlap(oXYMax, oXYMin, mXYMax, mXYMin);

        // 4. project others corners onto others right axis
        XminYmin = other.topRightTranslated();
        XmaxYmax = other.botRightTranslated();
        XminYMax = other.botLeftTranslated();
        XmaxYmin = other.topLeftTranslated();
        projectCornersOnAxis(other.transform.right, ref oXYMax, ref oXYMin);

        // project this corners onto others right axis
        XminYmin = topRightTranslated();
        XmaxYmax = botRightTranslated();
        XminYMax = botLeftTranslated();
        XmaxYmin = topLeftTranslated();
        projectCornersOnAxis(other.transform.right, ref mXYMax, ref mXYMin);

        bool isCollidingOnOtherRightAxis = AABBCollisionHull2D.checkOverlap(oXYMax, oXYMin, mXYMax, mXYMin);

        // internal function to project corners onto given axis
        void projectCornersOnAxis(Vector2 axis, ref Vector2 XYMax, ref Vector2 XYMin)
        {
            Vector2 oXminYminProj = project(XminYmin, axis);
            Vector2 oXmaxYmaxProj = project(XmaxYmax, axis);
            Vector2 oXminYmaxProj = project(XminYMax, axis);
            Vector2 oXmaxYminProj = project(XmaxYmin, axis);
            // get min / max
            // TODO: I'm not sure just taking the min is right, but it might work anyway.
            // it might not actually be a point on the axis.
            float yMin = Mathf.Min(oXminYminProj.y, oXmaxYmaxProj.y, oXminYmaxProj.y, oXmaxYminProj.y);
            float yMax = Mathf.Max(oXminYminProj.y, oXmaxYmaxProj.y, oXminYmaxProj.y, oXmaxYminProj.y);
            float xMin = Mathf.Min(oXminYminProj.x, oXmaxYmaxProj.x, oXminYmaxProj.x, oXmaxYminProj.x);
            float xMax = Mathf.Max(oXminYminProj.x, oXmaxYmaxProj.x, oXminYmaxProj.x, oXmaxYminProj.x);

            // convert to terms to test in checkOverlap function
            XYMax = new Vector2(xMax, yMax);
            XYMin = new Vector2(xMin, yMin);
        };
        // TODO: short circuit if false;
        // Also get rid of repeated assigns to XminYmin etc.

        return(isCollidingOnThisUpAxis && isCollidingOnThisRightAxis && isCollidingOnOtherUpAxis && isCollidingOnOtherRightAxis);
    }
예제 #13
0
    public override bool TestCollisionVsAABB(AABBCollisionHull2D other, ref Collision c)
    {
        updatePosition();
        other.updatePosition();
        // init corners.
        Vector2 oBLC = Vector2.zero;
        Vector2 oTRC = oBLC;

        other.getDimensions(ref oBLC, ref oTRC);
        // not needed other.getDimensions(ref oBLC, ref oTRC);
        // same as above, but first...
        // take axis extents of non-axis aligned box (make a bigger aabb) and run above test.
        // then, transform AABB into OBB's space, find max extents, run above test.
        // 1. get obb min / max
        // 2. transform obb min / max - breaks encapsulation?
        // 3. do AABB vs AABB
        // 4. if false RETURN

        // 1 & 2 & 3
        bool isColiding = AABBCollisionHull2D.checkOverlap(oTRC, oBLC, xyMax(), xyMin());

        // if not possibly colliding
        if (!isColiding)
        {
            // 4
            // return isColiding;
        }
        // 5
        // project AABB and OBB onto axii
        // check overlap on each axii

        // set up last two corners
        Vector2 oBRC = new Vector2(oTRC.x, oBLC.y);
        Vector2 oTLC = new Vector2(oBLC.x, oTRC.y);

        // project other on Up
        Vector2 oProj1 = project(oBLC, transform.up);
        Vector2 oProj2 = project(oBRC, transform.up);
        Vector2 oProj3 = project(oTRC, transform.up);
        Vector2 oProj4 = project(oTLC, transform.up);
        //Find min / max points
        Vector2 oMin, oMax;

        oMin.x = Mathf.Min(oProj1.x, oProj2.x, oProj3.x, oProj4.x);
        oMin.y = Mathf.Min(oProj1.y, oProj2.y, oProj3.y, oProj4.y);
        oMax.x = Mathf.Max(oProj1.x, oProj2.x, oProj3.x, oProj4.x);
        oMax.y = Mathf.Max(oProj1.y, oProj2.y, oProj3.y, oProj4.y);
        // project this on Up.
        Vector2 mProj1 = project(topLeftTranslated(), transform.up);
        Vector2 mProj2 = project(botLeftTranslated(), transform.up);
        Vector2 min, max;

        min.x = Mathf.Min(mProj1.x, mProj2.x);
        min.y = Mathf.Min(mProj1.y, mProj2.y);
        max.x = Mathf.Max(mProj1.x, mProj2.x);
        max.y = Mathf.Max(mProj1.y, mProj2.y);
        //if it doesn't work add in the last two corners and find min / max for this.
        bool isColidingOnUp = AABBCollisionHull2D.checkOverlap(max, min, oMax, oMin);

        //project other on Right
        oProj1 = project(oBLC, transform.right);
        oProj2 = project(oBRC, transform.right);
        oProj3 = project(oTRC, transform.right);
        oProj4 = project(oTLC, transform.right);
        //Find min / max points
        oMin.x = Mathf.Min(oProj1.x, oProj2.x, oProj3.x, oProj4.x);
        oMin.y = Mathf.Min(oProj1.y, oProj2.y, oProj3.y, oProj4.y);
        oMax.x = Mathf.Max(oProj1.x, oProj2.x, oProj3.x, oProj4.x);
        oMax.y = Mathf.Max(oProj1.y, oProj2.y, oProj3.y, oProj4.y);
        // project this on Right.
        mProj1 = project(topLeftTranslated(), transform.right);
        mProj2 = project(botRightTranslated(), transform.right);
        min.x  = Mathf.Min(mProj1.x, mProj2.x);
        min.y  = Mathf.Min(mProj1.y, mProj2.y);
        max.x  = Mathf.Max(mProj1.x, mProj2.x);
        max.y  = Mathf.Max(mProj1.y, mProj2.y);

        bool isColidingOnRight = AABBCollisionHull2D.checkOverlap(max, min, oMax, oMin);

        return(isColidingOnRight && isColidingOnUp);
    }