A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(physics3d): two characters no longer occupy the same space
esenginec50e4069c902
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5e441ee5d..16f7e418d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -170,6 +170,15 @@ published separately; it ships inside the editor.than the box around them. Nothing waits for the solver any more: the geometry isin the scene from the first frame.+- **Two characters no longer walk through each other.** A `CharacterVirtual` is+ swept against the WORLD, and the world does not contain characters — so nothing+ was ever checking one against another, and two of them met by occupying the same+ space. Jolt's own character-vs-character check is registered now, which is the+ one pair of shapes a player is sure to notice interpenetrating. Two new+ behaviours in the 3D physics gate: they meet rather than pass, and they stop a+ body apart. Found while giving NavAgents avoidance — steering kept them from+ aiming at each other, and nothing underneath would have stopped them arriving.+- **Agents give way to each other.** A route is planned against a world that doesnot move; agents do, and a dozen sent to one place all planned the same routeand walked it as one body. An agent whose `radius` is greater than zero nowdiff --git a/sdk/tests/physics3d-smoke.mjs b/sdk/tests/physics3d-smoke.mjsindex 735ab7722..63aa99830 100644--- a/sdk/tests/physics3d-smoke.mjs+++ b/sdk/tests/physics3d-smoke.mjs@@ -236,6 +236,27 @@ check('a character shoves a crate out of its way', shoved && shoved.x > -38.0,check('and one with no push force does not', unmoved && unmoved.x < -38.35,`x=${unmoved?.x.toFixed(3)} from -38.5`);+// ★ A character is swept against the WORLD, and the world holds no characters —+// so without a character-vs-character check two of them pass straight through,+// which is the one pair of shapes a player is sure to notice interpenetrating.+const LEFT_WALKER = 16, RIGHT_WALKER = 17;+const leftWalker = m._physics3d_addCharacter(LEFT_WALKER, 0.3, 0.5, -45, 0.8, -30, 0.87, 70, 0, 0);+const rightWalker = m._physics3d_addCharacter(RIGHT_WALKER, 0.3, 0.5, -41, 0.8, -30, 0.87, 70, 0, 0);+for (let i = 0; i < 150; i++) {+ m._physics3d_moveCharacter(leftWalker, 2, 0, 0, 1 / 60, 0.4, 0.5);+ m._physics3d_moveCharacter(rightWalker, -2, 0, 0, 1 / 60, 0.4, 0.5);+}+const left = moveCharacter(leftWalker, 0, 0, 0, 1);+const right = moveCharacter(rightWalker, 0, 0, 0, 1);+check('two characters meet rather than pass through each other', right.x > left.x,+ `left=${left.x.toFixed(3)} right=${right.x.toFixed(3)}`);+// Two capsules of radius 0.3 touch at 0.6 apart; they walked at each other for+// two and a half seconds, so anything less is one standing inside the other.+check('and they stop a body apart', right.x - left.x > 0.5,+ `apart=${(right.x - left.x).toFixed(3)}`);+m._physics3d_removeCharacter(leftWalker);+m._physics3d_removeCharacter(rightWalker);+m._physics3d_removeCharacter(strongHero);m._physics3d_removeCharacter(weakHero);diff --git a/src/esengine/bindings/modules/physics3d/Physics3DContext.hpp b/src/esengine/bindings/modules/physics3d/Physics3DContext.hppindex 676787d7a..fdca62d14 100644--- a/src/esengine/bindings/modules/physics3d/Physics3DContext.hpp+++ b/src/esengine/bindings/modules/physics3d/Physics3DContext.hpp@@ -25,6 +25,7 @@#include <Jolt/Core/TempAllocator.h>#include <Jolt/Physics/PhysicsSystem.h>#include <Jolt/Physics/Constraints/TwoBodyConstraint.h>+#include <Jolt/Physics/Character/CharacterVirtual.h>#include <cstdint>#include <memory>@@ -32,8 +33,6 @@#include <unordered_set>#include <vector>-namespace JPH { class CharacterVirtual; }-#ifdef __EMSCRIPTEN__#include <emscripten/emscripten.h>#else@@ -158,6 +157,10 @@ struct Context {/// A character is not a body, so its layer is not stored in one.std::unordered_map<uint32_t, uint32_t> characterLayers;uint32_t nextCharacterId = 1;+ /// Characters are swept against the WORLD, and the world does not contain+ /// them: without this they walk through each other, which is the one pair of+ /// shapes a player notices passing through.+ JPH::CharacterVsCharacterCollisionSimple characterVsCharacter;bool isValid() const { return system != nullptr; }};diff --git a/src/esengine/bindings/modules/physics3d/Physics3DModuleEntry.cpp b/src/esengine/bindings/modules/physics3d/Physics3DModuleEntry.cppindex b4eb19bdc..de583318f 100644--- a/src/esengine/bindings/modules/physics3d/Physics3DModuleEntry.cpp+++ b/src/esengine/bindings/modules/physics3d/Physics3DModuleEntry.cpp@@ -220,6 +220,7 @@ void physics3d_shutdown() {g().system = nullptr;g().jobs = nullptr;g().temp = nullptr;+ g().characterVsCharacter.mCharacters.clear();g().characters.clear();g().sensorBodies.clear();g().contactEnterBuffer.clear();@@ -484,6 +485,10 @@ uint32_t physics3d_addCharacter(uint32_t entity, float radius, float halfHeight,settings->mSupportingVolume = Plane(Vec3::sAxisY(), -radius);auto character = std::make_unique<CharacterVirtual>(settings, RVec3(px, py, pz), Quat::sIdentity(), g().system);+ // Every character checks against every other one. Brute force, which is what+ // Jolt offers and what a crowd of the size a scene authors costs nothing at.+ character->SetCharacterVsCharacterCollision(&g().characterVsCharacter);+ g().characterVsCharacter.Add(character.get());const uint32_t id = g().nextCharacterId++;g().characterLayers[id] = layer;g().characters[id] = std::move(character);@@ -493,6 +498,8 @@ uint32_t physics3d_addCharacter(uint32_t entity, float radius, float halfHeight,EMSCRIPTEN_KEEPALIVEvoid physics3d_removeCharacter(uint32_t characterId) {+ auto found = g().characters.find(characterId);+ if (found != g().characters.end()) g().characterVsCharacter.Remove(found->second.get());g().characters.erase(characterId);g().characterLayers.erase(characterId);g().entityOf.erase(characterId | CHARACTER_ID_TAG);