Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
134 changes: 134 additions & 0 deletions e2e/case/litePhysics-collision-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* @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,
collisionLayer: Layer
) {
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.collisionLayer = collisionLayer;
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.collisionLayer = Layer.Layer3;

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),
Layer.Layer1
);

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#L113-L120

Added lines #L113 - L120 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L122-L129

Added lines #L122 - L129 were not covered by tests

scene.physics.setColliderLayerCollision(Layer.Layer2, Layer.Layer3, false);
updateForE2E(engine, 1000, 38);
initScreenshot(engine, camera);
});

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

View check run for this annotation

Codecov / codecov/patch

e2e/case/litePhysics-collision-group.ts#L131-L134

Added lines #L131 - L134 were not covered by tests
146 changes: 146 additions & 0 deletions e2e/case/physx-collision-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/**
* @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,
collisionLayer: 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.collisionLayer = collisionLayer;
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.collisionLayer = Layer.Layer3;

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),
Layer.Layer1
);

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L123 - L130 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L133-L140

Added lines #L133 - L140 were not covered by tests

scene.physics.setColliderLayerCollision(Layer.Layer2, Layer.Layer3, false);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L142 was not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

e2e/case/physx-collision-group.ts#L144-L146

Added lines #L144 - L146 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.
5 changes: 2 additions & 3 deletions packages/core/src/physics/CharacterController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,12 @@ 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;

// sync world position to physical space
this._onUpdate();
// Sync world position to physical space
(<ICharacterController>this._nativeCollider).setWorldPosition(this.entity.transform.worldPosition);
}

/**
Expand Down
Loading
Loading