Exemplo n.º 1
0
    protected override bool TestCollisionVsAABB(AABB_2D other, out Collision collision)
    {
        collision = null;
        // if distance between centers is greater than half widths they're not colliding
        // component wise check
        // 1. store abs(distance) on x, y
        // 2. store width on x, y
        // 3. return if distance is less than width

        Vector2 ourCenter = particle.position, otherCenter = other.particle.position;
        Vector2 dist = ourCenter - otherCenter;

        dist.x = Mathf.Abs(dist.x);
        dist.y = Mathf.Abs(dist.y);

        Vector2 sumHalfWidths = halfWidths + other.halfWidths;

        return(dist.x < sumHalfWidths.x && dist.y < sumHalfWidths.y);
    }
Exemplo n.º 2
0
    protected override bool TestCollisionVsAABB(AABB_2D other, out Collision collision)
    {
        collision = null;
        // find closest point to the circle on the box: clamp the (center - min) and (center - max)
        // circle point collision test using closest point
        Vector2 point, otherMin, otherMax, delta;

        otherMin = other.particle.position - other.halfWidths;
        otherMax = other.particle.position + other.halfWidths;

        // find the closest point on the aabb
        point.x = Mathf.Clamp(particle.position.x, otherMin.x, otherMax.x);
        point.y = Mathf.Clamp(particle.position.y, otherMin.y, otherMax.y);

        delta = particle.position - point;
        bool colliding = delta.sqrMagnitude < radius * radius;

        if (colliding)
        {
            collision = new Collision();
            Vector2 relativeVelocity = particle.velocity - other.particle.velocity;
            Vector2 relativePosition = particle.position - other.particle.position;
            // 7.1.1 closing velocity
            collision.closingVelocity = Vector3.Dot(relativeVelocity, relativePosition.normalized);
            // assuming one point of contact
            collision.contact[0].normal           = delta.normalized;
            collision.contact[0].point            = point;
            collision.contact[0].restitution      = .5f;
            collision.contact[0].penetrationDepth = radius - delta.magnitude;
            collision.a            = this;
            collision.b            = other;
            collision.status       = colliding;
            collision.contactCount = 1;
        }

        // point collision with circle test
        return(colliding);
    }
Exemplo n.º 3
0
 protected override bool TestCollisionVsAABB(AABB_2D other, out Collision collision)
 {
     return(CollisionHull2D.TestCollision(other, this, out collision));
 }
Exemplo n.º 4
0
 protected abstract bool TestCollisionVsAABB(AABB_2D other, out Collision collision);