[feat] collider2d component works without rigidbody2d component (#15479) (#14102)

if rigid body is not attached to the same node of collider, this b2.shape is attached to the groundRigidBody(pos zero, rot zero)
This commit is contained in:
Ling Zhan
2023-02-07 10:47:26 +08:00
committed by GitHub
parent 4f9c1674b7
commit 1a121bbe65
5 changed files with 40 additions and 28 deletions
@@ -105,25 +105,29 @@ export class PhysicsContactListener extends b2.ContactListener {
private emit (contactType, contact: PhysicsContact) {
const colliderA = contact.colliderA;
const colliderB = contact.colliderB;
if (!colliderA || !colliderB || !colliderA.enabledInHierarchy || !colliderB.enabledInHierarchy) {
if (!colliderA || !colliderB) {
return;
}
const bodyA = colliderA.body;
const bodyB = colliderB.body;
if (!bodyA || !bodyB || !bodyA.enabledInHierarchy || !bodyB.enabledInHierarchy) {
//if rigid body doesn't exist, collider will be added to groundRigidbody automatically,
//hence it should emit event
if ((bodyA && !bodyA.enabledInHierarchy) || (bodyB && !bodyB.enabledInHierarchy) || (!bodyA && !bodyB)) {
return;
}
if (bodyA.enabledContactListener) {
//bodyA exists and enabledContactListner, or bodyA doesn't exist
if ((bodyA && bodyA.enabledContactListener) || (!bodyA)) {
colliderA?.emit(contactType, colliderA, colliderB, contact);
}
if (bodyB.enabledContactListener) {
//bodyB exists and enabledContactListner, or bodyB doesn't exist
if ((bodyB && bodyB.enabledContactListener) || (!bodyB)) {
colliderB?.emit(contactType, colliderB, colliderA, contact);
}
if (bodyA.enabledContactListener || bodyB.enabledContactListener) {
if ((bodyA && bodyA.enabledContactListener) || (bodyB && bodyB.enabledContactListener) || !bodyA || !bodyB) {
PhysicsSystem2D.instance.emit(contactType, colliderA, colliderB, contact);
}
@@ -54,7 +54,7 @@ export class b2BoxShape extends b2Shape2D implements IBoxShape {
return wps;
}
_createShapes (scaleX: number, scaleY: number) {
_createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number) {
scaleX = Math.abs(scaleX);
scaleY = Math.abs(scaleY);
@@ -62,8 +62,8 @@ export class b2BoxShape extends b2Shape2D implements IBoxShape {
const width = comp.size.width / 2 / PHYSICS_2D_PTM_RATIO * scaleX;
const height = comp.size.height / 2 / PHYSICS_2D_PTM_RATIO * scaleY;
const offsetX = comp.offset.x / PHYSICS_2D_PTM_RATIO * scaleX;
const offsetY = comp.offset.y / PHYSICS_2D_PTM_RATIO * scaleY;
const offsetX = (relativePositionX + comp.offset.x * scaleX) / PHYSICS_2D_PTM_RATIO;
const offsetY = (relativePositionY + comp.offset.y * scaleY) / PHYSICS_2D_PTM_RATIO;
const shape = new b2.PolygonShape();
shape.SetAsBox(width, height, new b2.Vec2(offsetX, offsetY), 0);
@@ -40,14 +40,14 @@ export class b2CircleShape extends b2Shape2D implements ICircleShape {
return this._worldPosition.set(p.x * PHYSICS_2D_PTM_RATIO, p.y * PHYSICS_2D_PTM_RATIO);
}
_createShapes (scaleX: number, scaleY: number) {
_createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number) {
scaleX = Math.abs(scaleX);
scaleY = Math.abs(scaleY);
const comp = this.collider as CircleCollider2D;
const offsetX = comp.offset.x / PHYSICS_2D_PTM_RATIO * scaleX;
const offsetY = comp.offset.y / PHYSICS_2D_PTM_RATIO * scaleY;
const offsetX = (relativePositionX + comp.offset.x * scaleX) / PHYSICS_2D_PTM_RATIO;
const offsetY = (relativePositionY + comp.offset.y * scaleY) / PHYSICS_2D_PTM_RATIO;
const shape = new b2.CircleShape();
shape.m_radius = comp.radius / PHYSICS_2D_PTM_RATIO * scaleX;
@@ -49,7 +49,7 @@ export class b2PolygonShape extends b2Shape2D implements IPolygonShape {
return this._worldPoints;
}
_createShapes (scaleX: number, scaleY: number) {
_createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number) {
const shapes: b2.PolygonShape[] = [];
const comp = this.collider as PolygonCollider2D;
@@ -79,8 +79,8 @@ export class b2PolygonShape extends b2Shape2D implements IPolygonShape {
shape = new b2.PolygonShape();
}
const p = poly[j];
const x = (p.x + offset.x) / PHYSICS_2D_PTM_RATIO * scaleX;
const y = (p.y + offset.y) / PHYSICS_2D_PTM_RATIO * scaleY;
const x = (relativePositionX + (p.x + offset.x) * scaleX) / PHYSICS_2D_PTM_RATIO;
const y = (relativePositionY + (p.y + offset.y) * scaleY) / PHYSICS_2D_PTM_RATIO;
const v = new b2.Vec2(x, y);
vertices.push(v);
+22 -14
View File
@@ -22,11 +22,11 @@
THE SOFTWARE.
*/
import b2 from '@cocos/box2d';
import b2, { Vec2 } from '@cocos/box2d';
import { IBaseShape } from '../../spec/i-physics-shape';
import { Collider2D, PhysicsSystem2D, RigidBody2D, PHYSICS_2D_PTM_RATIO } from '../../../../exports/physics-2d-framework';
import { Rect } from '../../../core';
import { Rect, Vec3 } from '../../../core';
import { b2PhysicsWorld } from '../physics-world';
import { PhysicsGroup } from '../../../physics/framework/physics-enum';
@@ -34,7 +34,11 @@ const tempFilter = new b2.Filter();
function getFilter (shape: b2Shape2D) {
const comp = shape.collider;
tempFilter.categoryBits = comp.group === PhysicsGroup.DEFAULT ? comp.body!.group : comp.group;
if (comp.body) {
tempFilter.categoryBits = comp.group === PhysicsGroup.DEFAULT ? comp.body.group : comp.group;
} else {
tempFilter.categoryBits = comp.group;
}
tempFilter.maskBits = PhysicsSystem2D.instance.collisionMatrix[tempFilter.categoryBits];
return tempFilter;
}
@@ -133,7 +137,8 @@ export class b2Shape2D implements IBaseShape {
return this._fixtures.indexOf(fixture);
}
_createShapes (scaleX: number, scaleY: number): b2.Shape[] {
//relativePositionX/Y : relative Position from shape to rigid body
_createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): b2.Shape[] {
return [];
}
@@ -141,16 +146,21 @@ export class b2Shape2D implements IBaseShape {
if (this._inited) return;
const comp = this.collider;
const scale = comp.node.worldScale;
// relative Position from shape to rigid body
let relativePosition = Vec3.ZERO;
const body = comp.getComponent(RigidBody2D);
if (!body) return;
const innerBody = body.impl?.impl as b2.Body;
if (!innerBody) return;
//if rigid body is not attached to the same node of collider, this b2.shape is attached
// to the groundRigidBody(pos zero, rot zero)
if (body && body.impl && body.impl.impl) {
this._body = body.impl.impl as b2.Body;
} else {
this._body = (PhysicsSystem2D.instance.physicsWorld as b2PhysicsWorld).groundBodyImpl;
relativePosition = comp.node.worldPosition;
}
const node = body.node;
const scale = node.worldScale;
const shapes = scale.x === 0 && scale.y === 0 ? [] : this._createShapes(scale.x, scale.y);
const shapes = scale.x === 0 && scale.y === 0 ? [] : this._createShapes(scale.x, scale.y, relativePosition.x, relativePosition.y);
const filter = getFilter(this);
@@ -167,15 +177,13 @@ export class b2Shape2D implements IBaseShape {
filter,
};
const fixture = innerBody.CreateFixture(fixDef);
const fixture = this._body.CreateFixture(fixDef);
fixture.m_userData = this;
this._shapes.push(shape);
this._fixtures.push(fixture);
}
this._body = innerBody;
this._inited = true;
}