How to check collision? Circle vs Rectangle

How to check collision? Circle vs Rectangle
0

my gameObject sometime need to check it immediately.
it’s not case of onCollisionxxx(Enter, Stay, Exit).

so i want check collision myself, how can i do it?
the gameObject has BoxCollision.(unitArea) and CircleCollision(AttackRange area)

gameObject [A] check about [A]'s AttackRange and [B]'s unitArea.

now i use this (Rect vs Circle)
cc.Intersection.polygonCircle();
( did not have cc.Intersection.RectCircle? so i use to polygonCircle() )

// create target's rect every time when need to check it
let tArea: cc.BoxCollider = this.target.unitArea;
let targetRect: cc.Rect = new cc.Rect(this.target.node.x + tArea.offset.x, this.target.node.y + tArea.offset.y, tArea.size.width, tArea.size.height);

if(cc.Intersection.polygonCircle(
    [cc.v2(targetRect.x, targetRect.y), cc.v2(targetRect.x + targetRect.width, targetRect.y),
    cc.v2(targetRect.x + targetRect.width, targetRect.y + targetRect.height), cc.v2(targetRect.x, targetRect.y + targetRect.height)], 
    {
        position: this.node.position, 
        radius: this.attackRange.radius  // this.attackRange: cc.CircleCollider
    }))) {
       // do process collision
}

cc.BoxCollider, cc.CircleCollider has not cc.Rect and Circle.
it’s create new rect object every time when need to check it.
i think is it low efficiency.
how can i check intersect cc.BoxCollider and cc.CircleCollider directly?

please tell me, any idea, any advice!

as far as i know, cc.BoxCollider and siblings cannot be tested for collision in a direct manner.

you could use class members to store cc.Rect/cc.Vec2 instances used by the collision, instead of new’ing them every time, e.g.

  private targetRect: cc.Rect = new cc.Rect();
  private p1: cc.Vec2 = new cc.Vec2();
  private p2: cc.Vec2 = new cc.Vec2();
  private p3: cc.Vec2 = new cc.Vec2();
  private p4: cc.Vec2 = new cc.Vec2();

  private handleCollision() {
    // create target's rect every time when need to check it
    let tArea: cc.BoxCollider = this.target.unitArea;
    this.targetRect.x = this.target.node.x + tArea.offset.x;
    this.targetRect.y = this.target.node.y + tArea.offset.y;
    this.targetRect.width = tArea.size.width;
    this.targetRect.height = tArea.size.height;

    this.p1.x = targetRect.x;
    this.p1.y = targetRect.y;

    this.p2.x = targetRect.x + targetRect.width;
    this.p2.y = targetRect.y;

    this.p3.x = targetRect.x + targetRect.width;
    this.p3.y = targetRect.y + targetRect.height;

    this.p4.x = targetRect.x;
    this.p4.y = targetRect.y + targetRect.height;
    
    if (cc.Intersection.polygonCircle(
            [p1, p2, p3, p4],
            {
              position: this.node.position,
              radius: this.attackRange.radius // this.attackRange: cc.CircleCollider
            })) {
      // do collision crap
    }
  }

you could go even further and make members out of the polygon array and circle object used by the cc.Intersection.polygonCircle.

there’s also an idea to create a Pool for cc.Vec2 and cc.Rect if have this kind of code in different places.