Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions e2e/case/litePhysics-collision-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* @title LitePhysics Collision Group
* @category Physics
*/
import {
WebGLEngine,
SphereColliderShape,
DynamicCollider,
BoxColliderShape,
Vector3,
MeshRenderer,
PointLight,
PrimitiveMesh,
Camera,
Script,
StaticCollider,
ColliderShape,
PBRMaterial,
Entity,
Layer
} from "@galacean/engine";

Check warning on line 21 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L21

Added line #L21 was not covered by tests

import { LitePhysics } from "@galacean/engine-physics-lite";
import { initScreenshot, updateForE2E } from "./.mockForE2E";
class MoveScript extends Script {
onUpdate() {
this.entity.transform.position.y -= 0.1;
}

Check warning on line 28 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L23-L28

Added lines #L23 - L28 were not covered by tests

onTriggerEnter(other: ColliderShape) {

Check warning on line 30 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L30

Added line #L30 was not covered by tests
// Change color to green when collision occurs
(this.entity.getComponent(MeshRenderer).getMaterial() as PBRMaterial).baseColor.set(0, 1, 0, 1);
}
}

Check warning on line 34 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L32-L34

Added lines #L32 - L34 were not covered by tests
// Create a sphere with physics
function createPhysicsSphere(
rootEntity: Entity,
name: string,
position: Vector3,
radius: number,
color: Vector3,
collisionGroup: number
) {
const sphereEntity = rootEntity.createChild(name);
sphereEntity.transform.setPosition(position.x, position.y, position.z);
sphereEntity.addComponent(MoveScript);

Check warning on line 46 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L36-L46

Added lines #L36 - L46 were not covered by tests

// Add visual representation
const sphereMtl = new PBRMaterial(rootEntity.engine);
const sphereRenderer = sphereEntity.addComponent(MeshRenderer);
sphereMtl.baseColor.set(color.x, color.y, color.z, 1.0);
sphereMtl.metallic = 0.0;
sphereMtl.roughness = 0.5;
sphereRenderer.mesh = PrimitiveMesh.createSphere(rootEntity.engine, radius);
sphereRenderer.setMaterial(sphereMtl);

Check warning on line 55 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L49-L55

Added lines #L49 - L55 were not covered by tests

// Add physics
const physicsSphere = new SphereColliderShape();
physicsSphere.radius = radius;

Check warning on line 59 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L58-L59

Added lines #L58 - L59 were not covered by tests

const sphereCollider = sphereEntity.addComponent(DynamicCollider);
sphereCollider.collisionGroup = collisionGroup;
sphereCollider.addShape(physicsSphere);

Check warning on line 63 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L61-L63

Added lines #L61 - L63 were not covered by tests

return sphereEntity;
}

Check warning on line 66 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L65-L66

Added lines #L65 - L66 were not covered by tests

WebGLEngine.create({ canvas: "canvas", physics: new LitePhysics() }).then((engine) => {
engine.canvas.resizeByClientSize();
const scene = engine.sceneManager.activeScene;
const rootEntity = scene.createRootEntity("root");

Check warning on line 71 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L68-L71

Added lines #L68 - L71 were not covered by tests

// Set up ambient lighting
scene.ambientLight.diffuseSolidColor.set(1, 1, 1, 1);
scene.ambientLight.diffuseIntensity = 1.2;

Check warning on line 75 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L74-L75

Added lines #L74 - L75 were not covered by tests

// Set up camera
const cameraEntity = rootEntity.createChild("camera");
const camera = cameraEntity.addComponent(Camera);
cameraEntity.transform.setPosition(0, 3, 15);
cameraEntity.transform.lookAt(new Vector3(0, 0, 0));

Check warning on line 81 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L78-L81

Added lines #L78 - L81 were not covered by tests

// Add point light
const light = rootEntity.createChild("light");
light.transform.setPosition(0, 10, 0);
const pointLight = light.addComponent(PointLight);
pointLight.intensity = 1.5;

Check warning on line 87 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L84-L87

Added lines #L84 - L87 were not covered by tests

const groundEntity = rootEntity.createChild("ground");

Check warning on line 89 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L89

Added line #L89 was not covered by tests

// 设置立方体的位置和大小
groundEntity.transform.setPosition(0, 1, 0);

Check warning on line 92 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L92

Added line #L92 was not covered by tests
// groundEntity.isActive = false;

// Visual representation of the ground cube
const groundMtl = new PBRMaterial(engine);
groundMtl.baseColor.set(0.5, 0.5, 0.5, 1.0);
groundMtl.roughness = 0.7;

Check warning on line 98 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L96-L98

Added lines #L96 - L98 were not covered by tests

const cubeSize = new Vector3(10, 0.2, 10);
const groundRenderer = groundEntity.addComponent(MeshRenderer);
groundRenderer.mesh = PrimitiveMesh.createCuboid(engine, cubeSize.x, cubeSize.y, cubeSize.z);
groundRenderer.setMaterial(groundMtl);

Check warning on line 103 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L100-L103

Added lines #L100 - L103 were not covered by tests

// Physics for the ground cube
const groundCollider = groundEntity.addComponent(StaticCollider);
const groundShape = new BoxColliderShape();
groundShape.size = cubeSize;
groundCollider.addShape(groundShape);

Check warning on line 109 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L106-L109

Added lines #L106 - L109 were not covered by tests

groundCollider.collisionGroup = 3;

Check warning on line 111 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L111

Added line #L111 was not covered by tests

const sphere1 = createPhysicsSphere(rootEntity, "RedSphere", new Vector3(-2, 5, 0), 0.5, new Vector3(1, 0, 0), 1);

Check warning on line 113 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L113

Added line #L113 was not covered by tests

const sphere2 = createPhysicsSphere(rootEntity, "BlueSphere", new Vector3(2, 5, 0), 0.5, new Vector3(0, 0, 1), 2);

Check warning on line 115 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L115

Added line #L115 was not covered by tests

scene.physics.setColliderGroupCollision(2, 3, false);
updateForE2E(engine, 1000, 38);
initScreenshot(engine, camera);
});

Check warning on line 120 in e2e/case/litePhysics-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L117-L120

Added lines #L117 - L120 were not covered by tests
132 changes: 132 additions & 0 deletions e2e/case/physx-collision-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* @title Physx Collision Group
* @category Physics
*/
import {
WebGLEngine,
SphereColliderShape,
DynamicCollider,
BoxColliderShape,
Vector3,
MeshRenderer,
PointLight,
PrimitiveMesh,
Camera,
Script,
StaticCollider,
ColliderShape,
PBRMaterial,
Entity,
Layer
} from "@galacean/engine";

Check warning on line 21 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L21

Added line #L21 was not covered by tests

import { PhysXPhysics } from "@galacean/engine-physics-physx";
import { initScreenshot, updateForE2E } from "./.mockForE2E";

Check warning on line 24 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L23-L24

Added lines #L23 - L24 were not covered by tests

class CheckScript extends Script {
onTriggerEnter(other: ColliderShape) {
console.log("onTriggerEnter", other);

Check warning on line 28 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L26-L28

Added lines #L26 - L28 were not covered by tests
// Change color to green when collision occurs
(this.entity.getComponent(MeshRenderer).getMaterial() as PBRMaterial).baseColor.set(0, 1, 0, 1);
}

Check warning on line 31 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L30-L31

Added lines #L30 - L31 were not covered by tests

onContactEnter(other: ColliderShape) {
console.log("onContactEnter", other);

Check warning on line 34 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L33-L34

Added lines #L33 - L34 were not covered by tests
// Change color to green when collision occurs
(this.entity.getComponent(MeshRenderer).getMaterial() as PBRMaterial).baseColor.set(0, 1, 0, 1);
}
}

Check warning on line 38 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L36-L38

Added lines #L36 - L38 were not covered by tests

// Create a sphere with physics
function createPhysicsSphere(
rootEntity: Entity,
name: string,
position: Vector3,
radius: number,
color: Vector3,
collisionGroup: number
) {
const sphereEntity = rootEntity.createChild(name);
sphereEntity.transform.setPosition(position.x, position.y, position.z);

Check warning on line 50 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L41-L50

Added lines #L41 - L50 were not covered by tests

// Add visual representation
const sphereMtl = new PBRMaterial(rootEntity.engine);
const sphereRenderer = sphereEntity.addComponent(MeshRenderer);
sphereMtl.baseColor.set(color.x, color.y, color.z, 1.0);
sphereMtl.metallic = 0.0;
sphereMtl.roughness = 0.5;
sphereRenderer.mesh = PrimitiveMesh.createSphere(rootEntity.engine, radius);
sphereRenderer.setMaterial(sphereMtl);

Check warning on line 59 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L53-L59

Added lines #L53 - L59 were not covered by tests

// Add physics
const physicsSphere = new SphereColliderShape();
physicsSphere.radius = radius;
physicsSphere.material.bounciness = 0.8;

Check warning on line 64 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L62-L64

Added lines #L62 - L64 were not covered by tests

const sphereCollider = sphereEntity.addComponent(DynamicCollider);
sphereCollider.collisionGroup = collisionGroup;
sphereEntity.addComponent(CheckScript);
sphereCollider.addShape(physicsSphere);

Check warning on line 69 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L66-L69

Added lines #L66 - L69 were not covered by tests

return sphereEntity;
}

Check warning on line 72 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L71-L72

Added lines #L71 - L72 were not covered by tests

WebGLEngine.create({ canvas: "canvas", physics: new PhysXPhysics() }).then((engine) => {
engine.canvas.resizeByClientSize();
const scene = engine.sceneManager.activeScene;
const rootEntity = scene.createRootEntity("root");

Check warning on line 77 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L74-L77

Added lines #L74 - L77 were not covered by tests

// Set up ambient lighting
scene.ambientLight.diffuseSolidColor.set(1, 1, 1, 1);
scene.ambientLight.diffuseIntensity = 1.2;

Check warning on line 81 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L80-L81

Added lines #L80 - L81 were not covered by tests

// Set up camera
const cameraEntity = rootEntity.createChild("camera");
const camera = cameraEntity.addComponent(Camera);

Check warning on line 85 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L84-L85

Added lines #L84 - L85 were not covered by tests
// 调整相机位置以便更好地观察穿透效果
cameraEntity.transform.setPosition(0, 3, 15);
cameraEntity.transform.lookAt(new Vector3(0, 0, 0));

Check warning on line 88 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L87-L88

Added lines #L87 - L88 were not covered by tests

// Add point light
const light = rootEntity.createChild("light");
light.transform.setPosition(0, 10, 0);
const pointLight = light.addComponent(PointLight);
pointLight.intensity = 1.5;

Check warning on line 94 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L91-L94

Added lines #L91 - L94 were not covered by tests

// 创建立方体作为地面
const groundEntity = rootEntity.createChild("ground");

Check warning on line 97 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L97

Added line #L97 was not covered by tests

// 设置立方体的位置和大小
groundEntity.transform.setPosition(0, 1, 0);

Check warning on line 100 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L100

Added line #L100 was not covered by tests

// Visual representation of the ground cube
const groundMtl = new PBRMaterial(engine);
groundMtl.baseColor.set(0.5, 0.5, 0.5, 1.0);
groundMtl.roughness = 0.7;

Check warning on line 105 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L103-L105

Added lines #L103 - L105 were not covered by tests
// 设置半透明以便能看到穿透的球体
groundMtl.baseColor.a = 0.5;

Check warning on line 107 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L107

Added line #L107 was not covered by tests

const cubeSize = new Vector3(10, 0.2, 10);
const groundRenderer = groundEntity.addComponent(MeshRenderer);
groundRenderer.mesh = PrimitiveMesh.createCuboid(engine, cubeSize.x, cubeSize.y, cubeSize.z);
groundRenderer.setMaterial(groundMtl);

Check warning on line 112 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L109-L112

Added lines #L109 - L112 were not covered by tests

// Physics for the ground cube
const groundCollider = groundEntity.addComponent(StaticCollider);
const groundShape = new BoxColliderShape();
groundShape.size = cubeSize;
groundCollider.addShape(groundShape);

Check warning on line 118 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L115-L118

Added lines #L115 - L118 were not covered by tests

groundCollider.collisionGroup = 3;

Check warning on line 120 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L120

Added line #L120 was not covered by tests

// 创建可以碰撞的红色球体
const sphere1 = createPhysicsSphere(rootEntity, "RedSphere", new Vector3(-2, 5, 0), 0.5, new Vector3(1, 0, 0), 1);

Check warning on line 123 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L123

Added line #L123 was not covered by tests

// 创建可以穿透的蓝色球体
const sphere2 = createPhysicsSphere(rootEntity, "BlueSphere", new Vector3(2, 5, 0), 0.5, new Vector3(0, 0, 1), 2);

Check warning on line 126 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L126

Added line #L126 was not covered by tests

scene.physics.setColliderGroupCollision(2, 3, false);

Check warning on line 128 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L128

Added line #L128 was not covered by tests

updateForE2E(engine, 110);
initScreenshot(engine, camera);
});

Check warning on line 132 in e2e/case/physx-collision-group.ts

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L130-L132

Added lines #L130 - L132 were not covered by tests
10 changes: 10 additions & 0 deletions e2e/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@
category: "Physics",
caseFileName: "physx-collision",
threshold: 0.1
},
"LitePhysics Collision Group": {
category: "Physics",
caseFileName: "litePhysics-collision-group",
threshold: 0.1
},
"PhysXPhysics Collision Group": {
category: "Physics",
caseFileName: "physx-collision-group",
threshold: 0.1

Check warning on line 226 in e2e/config.ts

View check run for this annotation

Codecov / codecov/patch

e2e/config.ts#L217-L226

Added lines #L217 - L226 were not covered by tests
}
},
Particle: {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions e2e/fixtures/originImage/Physics_physx-collision-group.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion packages/core/src/physics/CharacterController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export class CharacterController extends Collider {
constructor(entity: Entity) {
super(entity);
(<ICharacterController>this._nativeCollider) = PhysicsScene._nativePhysics.createCharacterController();

this._setUpDirection = this._setUpDirection.bind(this);
//@ts-ignore
this._upDirection._onValueChanged = this._setUpDirection;
Expand Down
27 changes: 26 additions & 1 deletion packages/core/src/physics/Collider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import { deepClone, ignoreClone } from "../clone/CloneManager";
import { ColliderShape } from "./shape/ColliderShape";
import { ICustomClone } from "../clone/ComponentCloner";
import { EntityModifyFlags } from "../enums/EntityModifyFlags";
import { Layer } from "../Layer";

/**
* Base class for all colliders.
Expand All @@ -24,6 +26,7 @@
protected _updateFlag: BoolUpdateFlag;
@deepClone
protected _shapes: ColliderShape[] = [];
protected _collisionGroup: number = 0;

/**
* The shapes of this collider.
Expand All @@ -32,6 +35,21 @@
return this._shapes;
}

/**
* The collision group of this collider, only support 0-31.
*/
get collisionGroup(): number {
return this._collisionGroup;
}

Check warning on line 43 in packages/core/src/physics/Collider.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/physics/Collider.ts#L42-L43

Added lines #L42 - L43 were not covered by tests

set collisionGroup(value: number) {
if (value < 0 || value > 31) {
throw new Error("Collision group must be between 0 and 31");
}

Check warning on line 48 in packages/core/src/physics/Collider.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/physics/Collider.ts#L47-L48

Added lines #L47 - L48 were not covered by tests
this._collisionGroup = value;
this._nativeCollider.setCollisionGroup(value);
}

/**
* @internal
*/
Expand Down Expand Up @@ -129,12 +147,15 @@
/**
* @internal
*/
_handleShapesChanged(): void {}
_handleShapesChanged(): void {
this._setCollisionGroup();
}

protected _syncNative(): void {
for (let i = 0, n = this.shapes.length; i < n; i++) {
this._addNativeShape(this.shapes[i]);
}
this._setCollisionGroup();
}

/**
Expand All @@ -161,4 +182,8 @@
shape._collider = null;
this._nativeCollider.removeShape(shape._nativeShape);
}

protected _setCollisionGroup(): void {
this._nativeCollider.setCollisionGroup(this._collisionGroup);
}
}
2 changes: 2 additions & 0 deletions packages/core/src/physics/DynamicCollider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ export class DynamicCollider extends Collider {
* @internal
*/
override _handleShapesChanged(): void {
super._handleShapesChanged();

if (this._automaticCenterOfMass || this._automaticInertiaTensor) {
this._setMassAndUpdateInertia();
}
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/physics/PhysicsScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ export class PhysicsScene {
}
}

/**
* Set whether two colliders can collide with each other.
* @param group1 - The first collision group
* @param group2 - The second collision group
* @param collide - Whether the colliders should collide
*/
setColliderGroupCollision(group1: number, group2: number, collide: boolean): void {
PhysicsScene._nativePhysics.setColliderGroupCollision(group1, group2, collide);
}
Comment on lines +250 to +264
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix the bug in the conditional check

There's a bug in the validation condition where you check the same variable twice instead of checking both indices.

- if (!Number.isInteger(index1) || !Number.isInteger(index1)) {
+ if (!Number.isInteger(index1) || !Number.isInteger(index2)) {
    throw new Error("Collision layer must be a single layer (Layer.Layer0 to Layer.Layer31)");
  }

The condition should check both index1 and index2 to ensure they're both valid integer indices.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* Set whether two colliders can collide with each other.
* @param layer1 - The first collision layer
* @param layer2 - The second collision layer
* @param isCollide - Whether the colliders should collide
*/
setColliderLayerCollision(layer1: Layer, layer2: Layer, isCollide: boolean): void {
const index1 = Math.log2(layer1);
const index2 = Math.log2(layer2);
if (!Number.isInteger(index1) || !Number.isInteger(index1)) {
throw new Error("Collision layer must be a single layer (Layer.Layer0 to Layer.Layer31)");
}
PhysicsScene._nativePhysics.setColliderLayerCollision(index1, index2, isCollide);
}
/**
* Set whether two colliders can collide with each other.
* @param layer1 - The first collision layer
* @param layer2 - The second collision layer
* @param isCollide - Whether the colliders should collide
*/
setColliderLayerCollision(layer1: Layer, layer2: Layer, isCollide: boolean): void {
const index1 = Math.log2(layer1);
const index2 = Math.log2(layer2);
- if (!Number.isInteger(index1) || !Number.isInteger(index1)) {
+ if (!Number.isInteger(index1) || !Number.isInteger(index2)) {
throw new Error("Collision layer must be a single layer (Layer.Layer0 to Layer.Layer31)");
}
PhysicsScene._nativePhysics.setColliderLayerCollision(index1, index2, isCollide);
}


/**
* Casts a ray through the Scene and returns the first hit.
* @param ray - The ray
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/physics/shape/ColliderShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export abstract class ColliderShape implements ICustomClone {
}

/**
* Contact offset for this shape, the value must be greater than or equal to 0.
* Contact offset for this shape, the value must be greater than or equal to 0, default is 0.02.
*/
get contactOffset(): number {
return this._contactOffset;
Expand Down
6 changes: 6 additions & 0 deletions packages/design/src/physics/ICollider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export interface ICollider {
*/
removeShape(shape: IColliderShape): void;

/**
* Set the collision group of the collider.
* @param layer - The layer of the entity which the collider belongs to
*/
setCollisionGroup(layer: number): void;

/**
* Deletes the collider.
*/
Expand Down
Loading
Loading