Is there a way to detect collisions of other nodes

Is there a way to detect collisions of other nodes
0

so i have this parent node and i have multiple childs of that node which i wanna detect its collisions. right now im using a script attached to those nodes individually but i was wondering if there was a way to detect collisions from another script.

I think the only way you can do that is checking the collision yourself.
I don’t know if you can listen to the collision events of the children in the parent node, I would like to know.

Anyway I’ve made a very little workaround for the box collision detection.

/**
     * Check if there's a box collision between two nodes
     * The given nodes needs to have the anchor point at 0,0
     *
     * @static
     * @param {*} nodeA
     * @param {*} nodeB
     * @param {*} [spaceRefNode=null] The space reference node (default is Canvas)
     * @returns Boolean
     */
    static getCollision(nodeA, nodeB, spaceRefNode = null) {
        if (spaceRefNode == null)
            spaceRefNode = cc.find('Canvas');
        var nodeRect = new cc.Rect(Helper.getPostionInOtherNode(spaceRefNode, nodeA).x,
            Helper.getPostionInOtherNode(spaceRefNode, nodeA).y,
            nodeA.width,
            nodeA.height);
        var targetRect = new cc.Rect(Helper.getPostionInOtherNode(spaceRefNode, nodeB).x,
            Helper.getPostionInOtherNode(spaceRefNode, nodeB).y,
            nodeB.width,
            nodeB.height);
        return cc.Intersection.rectRect(nodeRect, targetRect)
    }

/**
     * Get the position of the targetNode in the spaceNode space
     *
     * @static
     * @param {*} spaceNode
     * @param {*} targetNode
     * @returns cc.Vec2
     */
    static getPostionInOtherNode(spaceNode, targetNode) {
        if (targetNode.parent == null) {
            return null;
        }
        let pos = targetNode.parent.convertToWorldSpaceAR(targetNode.getPosition());
        return spaceNode.convertToNodeSpaceAR(pos);
    }
1 Like

hey thanks for the response. i will test it in a bit but this looks promising im not sure if i wouldve been able to write something like this myself but i feel like i understand it.

1 Like

so first getCollision methods will return boolean meaning true being collision between specified nodes occured right? also second method what exactly is the returned cc.vec2 used for?

The second method is needed to compare positions because Cocos will give you the local position of that node, so you need to get that position on the “space” on the other node to check if they are coliding.

1 Like

ohhh i didnt see it being used inside first method. thanks man

i get an error saying static is a reserved word in strict mode? ive never used static honestly before :smile:

Keep in mind that that is a JS file.

The JS file looks like this:

export class Helper {
/**
     * Check if there's a box collision between two nodes
     * The given nodes needs to have the anchor point at 0,0
     *
     * @static
     * @param {*} nodeA
     * @param {*} nodeB
     * @param {*} [spaceRefNode=null] The space reference node (default is Canvas)
     * @returns Boolean
     */
    static getCollision(nodeA, nodeB, spaceRefNode = null) {
        if (spaceRefNode == null)
            spaceRefNode = cc.find('Canvas');
        var nodeRect = new cc.Rect(Helper.getPostionInOtherNode(spaceRefNode, nodeA).x,
            Helper.getPostionInOtherNode(spaceRefNode, nodeA).y,
            nodeA.width,
            nodeA.height);
        var targetRect = new cc.Rect(Helper.getPostionInOtherNode(spaceRefNode, nodeB).x,
            Helper.getPostionInOtherNode(spaceRefNode, nodeB).y,
            nodeB.width,
            nodeB.height);
        return cc.Intersection.rectRect(nodeRect, targetRect)
    }

}

Then in another file you can import it like:

import { Helper } from "../general/Helper.js";

And call that methods like:
Helper.method();

1 Like

thanks again. it worked

1 Like