这段代码我放进了每个物理节点的update里面,物理行为看上去是和不分组完全一样。但是看着profile显示出来的physics毫秒数差不多,难道给物理分组是不可行的?还是说为物理节点每帧重新设置分组本身占用了很多cpu?
setPhyGroup() {
if (this._rigidBody == null) this._rigidBody = this.node.getComponent(RigidBody);
let x = this.node.position.x; // 分组依据
let z = this.node.position.z;
let multiGroup = 0;
let multiMask = 0;
if (x < -this.sw + this.r) {
if (z < this.ySplit + this.r) {
multiGroup |= PHY_GROUP.COIN_0;
multiMask |= PHY_GROUP.COIN | PHY_GROUP.BORDER | PHY_GROUP.COIN_0 | PHY_GROUP.BORDER_BACK | PHY_GROUP.BORDER_LEFT;
}
if (this.ySplit - this.r < z) {
multiGroup |= PHY_GROUP.COIN_4;
multiMask |= PHY_GROUP.COIN | PHY_GROUP.BORDER | PHY_GROUP.COIN_4 | PHY_GROUP.BORDER_FRONT;
}
}
if (-this.sw - this.r < x && x < this.r) {
if (z < this.ySplit + this.r) {
multiGroup |= PHY_GROUP.COIN_1;
multiMask |= PHY_GROUP.COIN | PHY_GROUP.BORDER | PHY_GROUP.COIN_1 | PHY_GROUP.BORDER_BACK;
}
if (this.ySplit - this.r < z) {
multiGroup |= PHY_GROUP.COIN_5;
multiMask |= PHY_GROUP.COIN | PHY_GROUP.BORDER | PHY_GROUP.COIN_5 | PHY_GROUP.BORDER_FRONT;
}
}
if (-this.r < x && x < this.sw + this.r) {
if (z < this.ySplit + this.r) {
multiGroup |= PHY_GROUP.COIN_2;
multiMask |= PHY_GROUP.COIN | PHY_GROUP.BORDER | PHY_GROUP.COIN_2 | PHY_GROUP.BORDER_BACK;
}
if (this.ySplit - this.r < z) {
multiGroup |= PHY_GROUP.COIN_6;
multiMask |= PHY_GROUP.COIN | PHY_GROUP.BORDER | PHY_GROUP.COIN_6 | PHY_GROUP.BORDER_FRONT;
}
}
if (this.sw - this.r < x) {
if (z < this.ySplit + this.r) {
multiGroup |= PHY_GROUP.COIN_3;
multiMask |= PHY_GROUP.COIN | PHY_GROUP.BORDER | PHY_GROUP.COIN_3 | PHY_GROUP.BORDER_RIGHT | PHY_GROUP.BORDER_BACK;
}
if (this.ySplit - this.r < z) {
multiGroup |= PHY_GROUP.COIN_7;
multiMask |= PHY_GROUP.COIN | PHY_GROUP.BORDER | PHY_GROUP.COIN_7 | PHY_GROUP.BORDER_FRONT;
}
}
this._rigidBody.setGroup(multiGroup);
this._rigidBody.setMask(multiMask);
}
里面我确实把一个物体设置进入了多个分组,这是我预期之内的。同时属于多个分组,其中任意一个分组被mask,则都会参与计算,这是没问题的。
理论上真个场景中的物体被均分成的8份,即使分界线上同属多个分组,但应该会减少很多判断的。就是和预期不一致。