The physics engine is a very important part of the game engine. The industry generally adopts PhysX to introduce related functions. However, for lightweight scenarios, PhysX makes the final application size very large, exceeding the limits of these projects. Galacean is designed based on multiple backends. On one hand, it compiles PhysX.js through WebAssembly; on the other hand, it also provides a lightweight physics engine. Both are consistent in API design. Users only need to choose a specific physics backend when initializing the engine. It can meet the needs of various scenarios such as lightweight applications and heavyweight games. For the overall design of the physics components, you can refer to the Wiki.
For scenarios that need to use various physics components and InputManager
that require Raycast picking, the physics engine needs to be initialized before use. Currently, the Galacean engine provides two built-in physics engine backend implementations:
Developers can set the physics backend in the Project Settings panel opened from the Main Menu interface.
If initializing the engine through a script, you only need to pass the physics backend object into the Engine
:
import {LitePhysics} from "@galacean/engine-physics-lite";
const engine = await WebGLEngine.create({
canvas: htmlCanvas,
physics: new LitePhysics(),
});
import { PhysXPhysics } from "@galacean/engine-physics-physx";
const engine = await WebGLEngine.create({
canvas: htmlCanvas,
physics: new PhysXPhysics(),
});
Choosing a physics backend needs to consider three factors: functionality, performance, and package size:
The physics engine is a crucial component of game engines, primarily responsible for the following functions:
To meet the needs of different application scenarios, Galacean adopts a multi-backend design:
Both backends implement a unified Physics System API, allowing developers to flexibly choose based on project requirements. For detailed design specifications of the physics system, please refer to the Design Document.
For scenarios requiring physics components or Raycast picking like InputManager
, the physics engine needs to be initialized before use.
Currently, Galacean Engine provides two built-in physics engine backend implementations:
When choosing a physics backend, three factors need to be considered: functionality, performance, and package size:
Feature | physics-lite | physics-physx |
---|---|---|
Collision Detection | ✓ | ✓ |
Physics Effects and Feedback | × | ✓ |
Continuous Collision Detection | × | ✓ |
Joint System | × | ✓ |
Developers can set the physics backend in the Project Settings panel accessed through the Main Menu.
When initializing the engine through scripts, simply pass the physics backend object to the Engine
:
import { WebGLEngine } from "@galacean/engine-rhi-webgl";
import { LitePhysics } from "@galacean/engine-physics-lite";
const engine = await WebGLEngine.create({ canvas: "canvas" });
engine.physicsManager.initialize(LitePhysics);
import { WebGLEngine } from "@galacean/engine-rhi-webgl";
import { PhysXPhysics } from "@galacean/engine-physics-physx";
const engine = await WebGLEngine.create({ canvas: "canvas" });
await engine.physicsManager.initialize(PhysXPhysics);
For detailed usage of the physics system, please refer to the following documentation: