BoxCollider manually

Hi!
We have two object A and B and these objects have inside a BoxCollider script.
Is it possible, when we want, verify these boxcollider when intersect between them? for sample in a cicly for; for(var i = 0; i < objects; i++) etc… because we move (without physic) an object and we want verify the collision with others objects when we move them.

Thanks for help! :slight_smile:

Stefano

https://docs.cocos2d-x.org/creator/manual/en/physics/collision/collision-manager.html

short version, you have to implement onCollisionEnter (and onCollisionStay/onCollisionExit, depending on your specs) in a component of a node that also has a XYZCollider attached.

thanks for answer but this don’t resolve our trouble because we don’t use the physics so when we move our player we need to control intersection inside move routine to stop the routine. (when we hit a wall for sample)

collision manager (BoxCollider/CircleCollider/PolygonCollider classes and related) is separated from the physics engine and it just provides collision detection. collision response is programmers’ task to implement.

public onCollisionEnter(other) {
  if (other.getComponent(Wall) {
    stopMovement();
  }
}

yep, we just use for other section inside game this. but in this trouble we move object and before set the next position, we control if next position is valid or invalid. for sample:

for(var i = 0; i < precision; i++) {
  var x = this.node.x + stepx;
  if(!intersect(rect, obstace.rect)) // < check collision from boxcollider
     this.node.x += stepx;
  else
    skip
}

is not possible have the rect from boxcollider and use rect.intersect for these rects found?

thanks! :slight_smile:

ah, alright. you can use cc.Intersection methods https://docs.cocos.com/creator/1.10/api/en/classes/Intersection.html - but you first need to convert from Collider data to Rect/Circle/cc.Vec2[] polygons.

there’s a related question here: How to check collision? Circle vs Rectangle

oh :open_mouth: thanks! now we try to do this! txs!

hi, is it wrong write this?

cc.BoxColliderToRect = function(node) {

    var boxCollider = node.getComponent(cc.BoxCollider);
    if(!boxCollider)
        return null;

    var rect = new cc.Rect();

    rect.x = node.x + boxCollider.offset.x;
    rect.y = node.y + boxCollider.offset.y;
    rect.width = boxCollider.size.width;
    rect.height = boxCollider.size.height;

    return rect;
}

and after we use this function to create rectA and rectB and after we use cc.Intersect.rectRect…
the rect’s position, is in center of rect?

Thanks for help!

Solved with this:

cc.BoxColliderToRect = function(node, _x, _y) {

    var boxCollider = node.getComponent(cc.BoxCollider);
    if(!boxCollider)
        return null;

    var rect = new cc.Rect();
    var x = node.x;
    var y = node.y;
    if(_x != undefined) x = _x;
    if(_y != undefined) y = _y;
    rect.x = (x + boxCollider.offset.x) - boxCollider.size.width * 0.5;
    rect.y = (y + boxCollider.offset.y) - boxCollider.size.height * 0.5;
    rect.width = boxCollider.size.width;
    rect.height = boxCollider.size.height;

    return rect;
}

and after we use

cc.Intersection.rectRect(this.rect, o.rect)

thanks for help!!

Stefano

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.