-
-
Notifications
You must be signed in to change notification settings - Fork 397
Support collision group and fix the position of characterController synchronization late issue #2614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Support collision group and fix the position of characterController synchronization late issue #2614
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0e1cc65
feat: support collision group
luzhuang f9df061
refactor: opt code
luzhuang 2c53eef
refactor: opt code
luzhuang fc9e0ac
refactor: opt code
luzhuang 5291a5f
refactor: opt code
luzhuang 092f26f
refactor: opt code
luzhuang 6b06408
refactor: opt code
luzhuang c0c6faf
refactor: opt code
luzhuang 9cde2f7
Merge branch 'dev/1.5' of github.com:oasis-engine/engine into feat/co…
luzhuang 2d6e210
refactor: remove syncLayer
luzhuang 9156ff2
refactor: fix e2e
luzhuang f190cb6
refactor: opt code
luzhuang fa46851
refactor: opt code
luzhuang 84b5030
refactor: opt code
luzhuang c71e392
refactor: opt code
luzhuang 8fcfcff
refactor: opt code
GuoLei1990 6347462
refactor: fix charactor
GuoLei1990 f899d28
refactor: opt code
luzhuang afd8f2c
refactor: opt code
luzhuang f04ed3b
refactor: opt code
luzhuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||
|
|
||
| import { LitePhysics } from "@galacean/engine-physics-lite"; | ||
| import { initScreenshot, updateForE2E } from "./.mockForE2E"; | ||
| class MoveScript extends Script { | ||
| onUpdate() { | ||
| this.entity.transform.position.y -= 0.1; | ||
| } | ||
|
|
||
| onTriggerEnter(other: ColliderShape) { | ||
| // Change color to green when collision occurs | ||
| (this.entity.getComponent(MeshRenderer).getMaterial() as PBRMaterial).baseColor.set(0, 1, 0, 1); | ||
| } | ||
| } | ||
| // 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); | ||
|
|
||
| // 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); | ||
|
|
||
| // Add physics | ||
| const physicsSphere = new SphereColliderShape(); | ||
| physicsSphere.radius = radius; | ||
|
|
||
| const sphereCollider = sphereEntity.addComponent(DynamicCollider); | ||
| sphereCollider.collisionLayer = collisionLayer; | ||
| sphereCollider.addShape(physicsSphere); | ||
|
|
||
| return sphereEntity; | ||
| } | ||
|
|
||
| WebGLEngine.create({ canvas: "canvas", physics: new LitePhysics() }).then((engine) => { | ||
| engine.canvas.resizeByClientSize(); | ||
| const scene = engine.sceneManager.activeScene; | ||
| const rootEntity = scene.createRootEntity("root"); | ||
|
|
||
| // Set up ambient lighting | ||
| scene.ambientLight.diffuseSolidColor.set(1, 1, 1, 1); | ||
| scene.ambientLight.diffuseIntensity = 1.2; | ||
|
|
||
| // 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)); | ||
|
|
||
| // Add point light | ||
| const light = rootEntity.createChild("light"); | ||
| light.transform.setPosition(0, 10, 0); | ||
| const pointLight = light.addComponent(PointLight); | ||
| pointLight.intensity = 1.5; | ||
|
|
||
| const groundEntity = rootEntity.createChild("ground"); | ||
|
|
||
| // 设置立方体的位置和大小 | ||
| groundEntity.transform.setPosition(0, 1, 0); | ||
| // 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; | ||
|
|
||
| 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); | ||
|
|
||
| // Physics for the ground cube | ||
| const groundCollider = groundEntity.addComponent(StaticCollider); | ||
| const groundShape = new BoxColliderShape(); | ||
| groundShape.size = cubeSize; | ||
| groundCollider.addShape(groundShape); | ||
|
|
||
| groundCollider.collisionLayer = Layer.Layer3; | ||
|
|
||
| const sphere1 = createPhysicsSphere( | ||
| rootEntity, | ||
| "RedSphere", | ||
| new Vector3(-2, 5, 0), | ||
| 0.5, | ||
| new Vector3(1, 0, 0), | ||
| Layer.Layer1 | ||
| ); | ||
|
|
||
| const sphere2 = createPhysicsSphere( | ||
| rootEntity, | ||
| "BlueSphere", | ||
| new Vector3(2, 5, 0), | ||
| 0.5, | ||
| new Vector3(0, 0, 1), | ||
| Layer.Layer2 | ||
| ); | ||
|
|
||
| scene.physics.setColliderLayerCollision(Layer.Layer2, Layer.Layer3, false); | ||
| updateForE2E(engine, 1000, 38); | ||
| initScreenshot(engine, camera); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||
|
|
||
| import { PhysXPhysics } from "@galacean/engine-physics-physx"; | ||
| import { initScreenshot, updateForE2E } from "./.mockForE2E"; | ||
|
|
||
| class CheckScript extends Script { | ||
| onTriggerEnter(other: ColliderShape) { | ||
| console.log("onTriggerEnter", other); | ||
| // Change color to green when collision occurs | ||
| (this.entity.getComponent(MeshRenderer).getMaterial() as PBRMaterial).baseColor.set(0, 1, 0, 1); | ||
luzhuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| onContactEnter(other: ColliderShape) { | ||
| console.log("onContactEnter", other); | ||
| // Change color to green when collision occurs | ||
| (this.entity.getComponent(MeshRenderer).getMaterial() as PBRMaterial).baseColor.set(0, 1, 0, 1); | ||
| } | ||
| } | ||
|
|
||
| // 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); | ||
|
|
||
| // 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); | ||
|
|
||
| // Add physics | ||
| const physicsSphere = new SphereColliderShape(); | ||
| physicsSphere.radius = radius; | ||
| physicsSphere.material.bounciness = 0.8; | ||
|
|
||
| const sphereCollider = sphereEntity.addComponent(DynamicCollider); | ||
| sphereCollider.collisionLayer = collisionLayer; | ||
| sphereEntity.addComponent(CheckScript); | ||
| sphereCollider.addShape(physicsSphere); | ||
|
|
||
| return sphereEntity; | ||
| } | ||
|
|
||
| WebGLEngine.create({ canvas: "canvas", physics: new PhysXPhysics() }).then((engine) => { | ||
| engine.canvas.resizeByClientSize(); | ||
| const scene = engine.sceneManager.activeScene; | ||
| const rootEntity = scene.createRootEntity("root"); | ||
|
|
||
| // Set up ambient lighting | ||
| scene.ambientLight.diffuseSolidColor.set(1, 1, 1, 1); | ||
| scene.ambientLight.diffuseIntensity = 1.2; | ||
|
|
||
| // 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)); | ||
|
|
||
| // Add point light | ||
| const light = rootEntity.createChild("light"); | ||
| light.transform.setPosition(0, 10, 0); | ||
| const pointLight = light.addComponent(PointLight); | ||
| pointLight.intensity = 1.5; | ||
|
|
||
| // 创建立方体作为地面 | ||
| const groundEntity = rootEntity.createChild("ground"); | ||
|
|
||
| // 设置立方体的位置和大小 | ||
| groundEntity.transform.setPosition(0, 1, 0); | ||
|
|
||
| // 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; | ||
| // 设置半透明以便能看到穿透的球体 | ||
| groundMtl.baseColor.a = 0.5; | ||
|
|
||
| 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); | ||
|
|
||
| // Physics for the ground cube | ||
| const groundCollider = groundEntity.addComponent(StaticCollider); | ||
| const groundShape = new BoxColliderShape(); | ||
| groundShape.size = cubeSize; | ||
| groundCollider.addShape(groundShape); | ||
|
|
||
| groundCollider.collisionLayer = Layer.Layer3; | ||
|
|
||
| // 创建可以碰撞的红色球体 | ||
| const sphere1 = createPhysicsSphere( | ||
| rootEntity, | ||
| "RedSphere", | ||
| new Vector3(-2, 5, 0), | ||
| 0.5, | ||
| new Vector3(1, 0, 0), | ||
| Layer.Layer1 | ||
| ); | ||
|
|
||
| // 创建可以穿透的蓝色球体 | ||
| const sphere2 = createPhysicsSphere( | ||
| rootEntity, | ||
| "BlueSphere", | ||
| new Vector3(2, 5, 0), | ||
| 0.5, | ||
| new Vector3(0, 0, 1), | ||
| Layer.Layer2 | ||
| ); | ||
|
|
||
| scene.physics.setColliderLayerCollision(Layer.Layer2, Layer.Layer3, false); | ||
|
|
||
| updateForE2E(engine, 110); | ||
| initScreenshot(engine, camera); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
e2e/fixtures/originImage/Physics_litePhysics-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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.