Skip to content

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.

fix(camera): a camera looks from where its parent puts it

esenginec42f012ae451
diff --git a/desktop/public/scenes/camera-parent.esscene b/desktop/public/scenes/camera-parent.esscene
new file mode 100644
index 000000000..88e45db5d
--- /dev/null
+++ b/desktop/public/scenes/camera-parent.esscene
@@ -0,0 +1,121 @@
+{
+ "version": 1,
+ "name": "CameraParent",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Rig",
+ "parent": null,
+ "children": [1],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 200,
+ "y": 0,
+ "z": 0
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 1,
+ "name": "Camera",
+ "parent": 0,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 10
+ }
+ }
+ },
+ {
+ "type": "Camera",
+ "data": {
+ "projectionType": 1,
+ "orthoSize": 300,
+ "isActive": true,
+ "priority": 0
+ }
+ }
+ ]
+ },
+ {
+ "id": 2,
+ "name": "AtRig",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 200,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 120,
+ "y": 120
+ },
+ "color": {
+ "r": 1,
+ "g": 0,
+ "b": 0,
+ "a": 1
+ }
+ }
+ }
+ ]
+ },
+ {
+ "id": 3,
+ "name": "AtOrigin",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 120,
+ "y": 120
+ },
+ "color": {
+ "r": 0,
+ "g": 1,
+ "b": 0,
+ "a": 1
+ }
+ }
+ }
+ ]
+ }
+ ]
+}
diff --git a/desktop/public/scenes/camera-parent.textures.json b/desktop/public/scenes/camera-parent.textures.json
new file mode 100644
index 000000000..0967ef424
--- /dev/null
+++ b/desktop/public/scenes/camera-parent.textures.json
@@ -0,0 +1 @@
+{}
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index ea9f6045e..34ed03679 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -1570,8 +1570,8 @@ viewportPixels: Viewport
## CameraTransformFields — interface @beta
```
-position: { x: number; y: number; z: number; }
-rotation: { x: number; y: number; z: number; w: number; }
+worldPosition: { x: number; y: number; z: number; }
+worldRotation: { x: number; y: number; z: number; w: number; }
```
## CameraView — const @experimental
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index b44e55d14..c165df2aa 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -1570,8 +1570,8 @@ viewportPixels: Viewport
## CameraTransformFields — interface @beta
```
-position: { x: number; y: number; z: number; }
-rotation: { x: number; y: number; z: number; w: number; }
+worldPosition: { x: number; y: number; z: number; }
+worldRotation: { x: number; y: number; z: number; w: number; }
```
## CameraView — const @experimental
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index 9690d17ba..173335564 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -1565,8 +1565,8 @@ viewportPixels: Viewport
## CameraTransformFields — interface @beta
```
-position: { x: number; y: number; z: number; }
-rotation: { x: number; y: number; z: number; w: number; }
+worldPosition: { x: number; y: number; z: number; }
+worldRotation: { x: number; y: number; z: number; w: number; }
```
## CameraView — const @experimental
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index 3cd184e7f..baa447b21 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -1565,8 +1565,8 @@ viewportPixels: Viewport
## CameraTransformFields — interface @beta
```
-position: { x: number; y: number; z: number; }
-rotation: { x: number; y: number; z: number; w: number; }
+worldPosition: { x: number; y: number; z: number; }
+worldRotation: { x: number; y: number; z: number; w: number; }
```
## CameraView — const @experimental
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index 2836a6122..31394790d 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -1565,8 +1565,8 @@ viewportPixels: Viewport
## CameraTransformFields — interface @beta
```
-position: { x: number; y: number; z: number; }
-rotation: { x: number; y: number; z: number; w: number; }
+worldPosition: { x: number; y: number; z: number; }
+worldRotation: { x: number; y: number; z: number; w: number; }
```
## CameraView — const @experimental
diff --git a/sdk/src/camera/CameraPlugin.ts b/sdk/src/camera/CameraPlugin.ts
index 0e6059886..1285c9bc2 100644
--- a/sdk/src/camera/CameraPlugin.ts
+++ b/sdk/src/camera/CameraPlugin.ts
@@ -207,10 +207,16 @@ export interface CameraFields {
cullingMask?: number;
}
-/** The placement a POV is read from — see {@link CameraFields}. @beta */
+/**
+ * The placement a POV is read from — see {@link CameraFields}. WORLD, because a
+ * camera's view is its own transform inverted, and a camera parented to anything
+ * (a rig, the player) sits where the hierarchy puts it.
+ *
+ * @beta
+ */
export interface CameraTransformFields {
- position: { x: number; y: number; z: number };
- rotation: { x: number; y: number; z: number; w: number };
+ worldPosition: { x: number; y: number; z: number };
+ worldRotation: { x: number; y: number; z: number; w: number };
}
function readCameraPOV(
@@ -218,13 +224,14 @@ function readCameraPOV(
camera: CameraFields,
transform: CameraTransformFields,
): CameraPOV {
- const q = transform.rotation;
+ const q = transform.worldRotation;
+ const p = transform.worldPosition;
return {
entity,
isActive: camera.isActive,
- x: transform.position.x,
- y: transform.position.y,
- z: transform.position.z,
+ x: p.x,
+ y: p.y,
+ z: p.z,
rotation: 2 * Math.atan2(q.z, q.w), // quaternion → Z angle (2D convention)
// Kept whole only when it says something the Z angle cannot: a camera
// that looks at 3D content from anywhere but straight down -Z.
diff --git a/sdk/tests/camera-pov.test.ts b/sdk/tests/camera-pov.test.ts
index 881b10459..59015d721 100644
--- a/sdk/tests/camera-pov.test.ts
+++ b/sdk/tests/camera-pov.test.ts
@@ -104,8 +104,8 @@ describe('cameraFrustumCorners', () => {
...over,
});
const transform = (over: Record<string, unknown> = {}) => ({
- position: { x: 0, y: 0, z: 10 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
+ worldPosition: { x: 0, y: 0, z: 10 },
+ worldRotation: { x: 0, y: 0, z: 0, w: 1 },
...over,
});
type CamArg = Parameters<typeof cameraFrustumCorners>[0];
@@ -125,13 +125,13 @@ describe('cameraFrustumCorners', () => {
});
it('follows the camera, and its tilt with it', () => {
- const moved = corners(camera(), transform({ position: { x: 100, y: 50, z: 10 } }));
+ const moved = corners(camera(), transform({ worldPosition: { x: 100, y: 50, z: 10 } }));
expect(moved[0]).toBeCloseTo(-300, 3);
expect(moved[1]).toBeCloseTo(-250, 3);
// Pitched 90° about X the camera looks along -Y, so its frame leaves the
// z = 0 plane entirely — the corners' z spread rather than their x.
const s = Math.sin(Math.PI / 4);
- const pitched = corners(camera(), transform({ rotation: { x: s, y: 0, z: 0, w: s } }));
+ const pitched = corners(camera(), transform({ worldRotation: { x: s, y: 0, z: 0, w: s } }));
const zs = [pitched[2]!, pitched[5]!, pitched[8]!, pitched[11]!];
expect(Math.max(...zs) - Math.min(...zs)).toBeGreaterThan(500);
});
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index 900fa7987..067eb2113 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -183,6 +183,10 @@ export const SCENES = [
// real import wrote. Ambient light only: the black quad's lit half can only be
// emission, the white quad's right half is occluded away, the cutout discards.
{ id: "model-material", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PREFAB: "/scenes/model-material.esprefab", ESTELLA_VERIFY_SCENE: "/scenes/model-material.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/model-material.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.05,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":40},{\"x\":0.2167,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":30},{\"x\":0.4167,\"y\":0.5,\"rgb\":[255,255,255],\"tol\":40},{\"x\":0.5833,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":30},{\"x\":0.8667,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":30}]" } },
+ // A camera parented to something looks from where the hierarchy puts it: the red
+ // quad on the rig is dead centre, the green one at the origin 200 to its left,
+ // and reading the LOCAL position swaps them. One step, so a stale world is zero.
+ { id: "camera-parent", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/camera-parent.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/camera-parent.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "1", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[255,0,0],\"tol\":40},{\"x\":0.1667,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":40}]" } },
// Takes the GPU away for real and captures AFTER the cycle: the four points
// sample the atlas, so they fail unless the content came back. A vertex-colour
// scene passed with the re-upload deleted. No webgpu — no GL extension there.