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
feat(examples): physics-playground showcase for the new joints + one-way platform
yhh7a2e3c1749ad
diff --git a/examples/physics-playground/assets/scenes/showcase.esscene b/examples/physics-playground/assets/scenes/showcase.esscenenew file mode 100644index 000000000..30d9dd95a--- /dev/null+++ b/examples/physics-playground/assets/scenes/showcase.esscene@@ -0,0 +1,120 @@+{+ "version": "1.0",+ "name": "Showcase",+ "entities": [+ {+ "id": 0,+ "name": "Camera",+ "parent": null,+ "children": [],+ "components": [+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 10 } } },+ { "type": "Camera", "data": { "projectionType": 1, "orthoSize": 300, "isActive": true, "priority": 0 } }+ ],+ "visible": true+ },+ {+ "id": 1,+ "name": "Ground",+ "parent": null,+ "children": [],+ "components": [+ { "type": "Transform", "data": { "position": { "x": 0, "y": -260, "z": 0 } } },+ { "type": "Sprite", "data": { "size": { "x": 780, "y": 20 }, "color": { "r": 0.4, "g": 0.4, "b": 0.45, "a": 1 } } },+ { "type": "RigidBody", "data": { "bodyType": 0 } },+ { "type": "BoxCollider", "data": { "halfExtents": { "x": 3.9, "y": 0.1 } } }+ ],+ "visible": true+ },+ {+ "id": 2,+ "name": "LeftWall",+ "parent": null,+ "children": [],+ "components": [+ { "type": "Transform", "data": { "position": { "x": -390, "y": -20, "z": 0 } } },+ { "type": "Sprite", "data": { "size": { "x": 20, "y": 520 }, "color": { "r": 0.4, "g": 0.4, "b": 0.45, "a": 1 } } },+ { "type": "RigidBody", "data": { "bodyType": 0 } },+ { "type": "BoxCollider", "data": { "halfExtents": { "x": 0.1, "y": 2.6 } } }+ ],+ "visible": true+ },+ {+ "id": 3,+ "name": "RightWall",+ "parent": null,+ "children": [],+ "components": [+ { "type": "Transform", "data": { "position": { "x": 390, "y": -20, "z": 0 } } },+ { "type": "Sprite", "data": { "size": { "x": 20, "y": 520 }, "color": { "r": 0.4, "g": 0.4, "b": 0.45, "a": 1 } } },+ { "type": "RigidBody", "data": { "bodyType": 0 } },+ { "type": "BoxCollider", "data": { "halfExtents": { "x": 0.1, "y": 2.6 } } }+ ],+ "visible": true+ },+ {+ "id": 4,+ "name": "OneWayLedge",+ "parent": null,+ "children": [],+ "components": [+ { "type": "Transform", "data": { "position": { "x": -170, "y": 30, "z": 0 } } },+ { "type": "Sprite", "data": { "size": { "x": 300, "y": 16 }, "color": { "r": 0.3, "g": 0.8, "b": 0.9, "a": 0.55 } } },+ { "type": "RigidBody", "data": { "bodyType": 0 } },+ { "type": "BoxCollider", "data": { "halfExtents": { "x": 1.5, "y": 0.08 } } },+ { "type": "OneWayPlatform", "data": { "normal": { "x": 0, "y": 1 }, "enabled": true } }+ ],+ "visible": true+ },+ {+ "id": 5,+ "name": "ShuttleAnchor",+ "parent": null,+ "children": [],+ "components": [+ { "type": "Transform", "data": { "position": { "x": 150, "y": -90, "z": 0 } } },+ { "type": "RigidBody", "data": { "bodyType": 0 } }+ ],+ "visible": true+ },+ {+ "id": 6,+ "name": "Shuttle",+ "parent": null,+ "children": [],+ "components": [+ { "type": "Transform", "data": { "position": { "x": 150, "y": -90, "z": 0 } } },+ { "type": "Sprite", "data": { "size": { "x": 160, "y": 24 }, "color": { "r": 0.95, "g": 0.6, "b": 0.2, "a": 1 } } },+ { "type": "RigidBody", "data": { "bodyType": 2, "fixedRotation": true } },+ { "type": "BoxCollider", "data": { "halfExtents": { "x": 0.8, "y": 0.12 }, "friction": 0.8 } },+ { "type": "MotorJoint", "data": {+ "connectedEntity": 5,+ "linearVelocity": { "x": 180, "y": 0 },+ "maxVelocityForce": 3000,+ "angularVelocity": 0,+ "maxVelocityTorque": 0,+ "linearHertz": 0,+ "linearDampingRatio": 0,+ "maxSpringForce": 0,+ "angularHertz": 0,+ "angularDampingRatio": 0,+ "maxSpringTorque": 0,+ "collideConnected": false,+ "enabled": true+ } }+ ],+ "visible": true+ },+ {+ "id": 7,+ "name": "SpawnTimer",+ "parent": null,+ "children": [],+ "components": [+ { "type": "Transform", "data": {} },+ { "type": "SpawnTimer", "data": { "interval": 0.9, "timer": 0 } }+ ],+ "visible": true+ }+ ]+}diff --git a/examples/physics-playground/assets/scenes/showcase.esscene.meta b/examples/physics-playground/assets/scenes/showcase.esscene.metanew file mode 100644index 000000000..c152970b1--- /dev/null+++ b/examples/physics-playground/assets/scenes/showcase.esscene.meta@@ -0,0 +1,8 @@+{+ "uuid": "b7f3c2a1-0d47-4e8b-9c25-1a3f6b8d240e",+ "version": "2.0",+ "type": "scene",+ "importer": {+ "autoMigrate": true+ }+}diff --git a/examples/physics-playground/project.esproject b/examples/physics-playground/project.esprojectindex 4f0644492..04aff0a88 100644--- a/examples/physics-playground/project.esproject+++ b/examples/physics-playground/project.esproject@@ -1,10 +1,10 @@{"formatVersion": "1","name": "Physics Playground",- "description": "Drop balls and crates into a rigid-body sandbox.",+ "description": "A rigid-body sandbox: drop balls and crates, drag them with the mouse, watch a motor-driven shuttle ferry them, and jump bodies up through a one-way platform.","tag": "Physics","version": "0.1.0",- "defaultScene": "assets/scenes/main.esscene",+ "defaultScene": "assets/scenes/showcase.esscene","designResolution": {"width": 800,"height": 600diff --git a/examples/physics-playground/src/main.ts b/examples/physics-playground/src/main.tsindex b021f4937..6992ae1e8 100644--- a/examples/physics-playground/src/main.ts+++ b/examples/physics-playground/src/main.ts@@ -2,5 +2,8 @@ import { addSystemToSchedule, Schedule } from 'esengine';import './components';import { spawnSystem } from './systems/spawn';+import { dragSystem, shuttleSystem } from './systems/interact';addSystemToSchedule(Schedule.Update, spawnSystem);+addSystemToSchedule(Schedule.Update, dragSystem);+addSystemToSchedule(Schedule.Update, shuttleSystem);diff --git a/examples/physics-playground/src/systems/interact.ts b/examples/physics-playground/src/systems/interact.tsnew file mode 100644index 000000000..6d5645a3d--- /dev/null+++ b/examples/physics-playground/src/systems/interact.ts@@ -0,0 +1,59 @@+import {+ defineSystem, Query, Res, Input, UICameraInfo,+ Transform, RigidBody, BodyType,+} from 'esengine';+import type { UICameraData, RigidBodyData, TransformData } from 'esengine';+import { PhysicsAPI, MotorJoint } from 'esengine/physics';+import type { Physics } from 'esengine/physics';++const DRAG_BUTTON = 0;+const PICK_RADIUS = 6; // world px around the cursor to grab a body++// Click-and-drag any dynamic body with a mouse joint: pick on press, follow the+// cursor while held, release on up. Only one drag is live at a time.+export const dragSystem = defineSystem(+ [Res(Input), Res(UICameraInfo), Res(PhysicsAPI), Query(RigidBody)],+ (input, camera: UICameraData, physics: Physics, bodies) => {+ if (!camera.valid) return;+ const target = { x: camera.worldMouseX, y: camera.worldMouseY };++ if (input.isMouseButtonPressed(DRAG_BUTTON) && !physics.hasMouseJoint()) {+ const dynamic = new Set<number>();+ for (const [entity, rb] of bodies as Iterable<[number, RigidBodyData]>) {+ if (rb.bodyType === BodyType.Dynamic) dynamic.add(entity);+ }+ const hit = physics.overlapCircle(target, PICK_RADIUS).find((e) => dynamic.has(e));+ if (hit !== undefined) physics.createMouseJoint(hit, target);+ } else if (input.isMouseButtonDown(DRAG_BUTTON)) {+ if (physics.hasMouseJoint()) physics.setMouseTarget(target);+ } else if (physics.hasMouseJoint()) {+ physics.destroyMouseJoint();+ }+ },+ { name: 'DragSystem' }+);++// The motor-driven shuttle sweeps back and forth: flip its motor velocity at the+// travel bounds. Reverses once per edge (tracked per entity).+const SHUTTLE_SPEED = 180; // world px/s+const SHUTTLE_MIN_X = 20;+const SHUTTLE_MAX_X = 290;+const shuttleDir = new Map<number, number>();++export const shuttleSystem = defineSystem(+ [Res(PhysicsAPI), Query(Transform, MotorJoint)],+ (physics: Physics, shuttles) => {+ for (const [entity, transform] of shuttles as Iterable<[number, TransformData]>) {+ let dir = shuttleDir.get(entity) ?? 1;+ if (transform.position.x > SHUTTLE_MAX_X && dir > 0) {+ dir = -1;+ physics.setMotorJointLinearVelocity(entity, { x: -SHUTTLE_SPEED, y: 0 });+ } else if (transform.position.x < SHUTTLE_MIN_X && dir < 0) {+ dir = 1;+ physics.setMotorJointLinearVelocity(entity, { x: SHUTTLE_SPEED, y: 0 });+ }+ shuttleDir.set(entity, dir);+ }+ },+ { name: 'ShuttleSystem' }+);diff --git a/examples/physics-playground/tsconfig.json b/examples/physics-playground/tsconfig.jsonindex 0a8528b29..7421b075a 100644--- a/examples/physics-playground/tsconfig.json+++ b/examples/physics-playground/tsconfig.json@@ -14,6 +14,7 @@"paths": {"esengine": ["./.esengine/sdk/index.d.ts"],"esengine/wasm": ["./.esengine/sdk/wasm.d.ts"],+ "esengine/physics": ["./.esengine/sdk/physics/index.d.ts"],"@esengine/editor": ["./.esengine/editor/index.d.ts"]}},