游戏客户端架构 / 系统课时
剧情镜头怎样接管玩家镜头
剧情镜头怎样接管玩家镜头
剧情结束时直接恢复开场前相机位置,角色已经走远就会出现大幅回跳。本课把接管权限与玩家镜头偏好分开保存,在退出时依据当前关注点重建轨道状态,并用身份令牌阻止过期剧情回调错误释放控制权。
学习目标
- 为剧情开始、取消和正常结束设计对称的接管流程。
- 区分应恢复的玩家偏好与必须重新求解的世界姿态。
- 验证过期回调、目标丢失和零长度轨道不会导致错误恢复。
保存偏好而不是保存整帧世界
接管前保存玩家选择的肩位、期望距离、输入灵敏度或模式请求身份,这些属于持续偏好。相机旧世界位置、旧目标坐标和旧阻尼速度则具有时效性,不能在剧情结束后原样恢复。角色移动、重生或目标死亡都可能使这些数据失效。
剧情接管镜头不必等于冻结角色移动。镜头输入、角色移动、界面交互和鼠标捕获是不同权限,应分别申请和释放。多个系统都需要禁止输入时,用各自的锁令牌,不能由剧情结束直接把一个全局布尔值设为允许。

接管和退出都要有身份
开始剧情时生成递增令牌,只有当前令牌才能更新或结束接管。如果剧情 A 被剧情 B 替换,A 的异步结束回调必须被拒绝,否则它会释放 B 的输入锁或提前恢复玩家相机。正常结束、跳过、取消和场景卸载应共享同一条幂等释放路径。
新剧情覆盖旧剧情时,保存的玩家偏好应继续指向最初的玩家状态,不应把剧情 A 的参数当成玩家偏好再保存一次。层叠演出较复杂时可以使用请求栈或租约集合,但仍要明确谁拥有当前输出及哪些退出只释放自身资源。
从当前空间关系重建轨道
剧情结束时先取当前有效 Focus 与当前安全 Eye,用差向量反求距离、水平角和仰角,再作为玩家轨道的初始状态。之后把期望距离恢复为玩家偏好,通过过渡层逐渐接近,避免当帧从剧情距离跳到偏好距离。
这里的仰角定义为 Eye 相对 Focus 的高度角,正值表示相机在目标上方;它不同于“视线向上为正”的观察 Pitch。反求和正向重建必须使用同一套约定,接入引擎时再换算旋转正负号。
若 Eye 与 Focus 重合,差向量没有可靠方向,不应调用归一化或随意得到零角度。若目标已丢失,也不能默认 Focus 为世界原点。两者都先保持经安全验证的当前镜头,等待可用目标或明确的应急构图。
Eye 位于 Focus 正上方或正下方时,距离仍非零,但水平角未定。不能把 atan2(0,0) 的零值当成恢复结果;应显式传入保存的玩家轨道水平角作为 fallbackYaw,保留该自由度,并标记本次使用了回退角。
运行可校验的接管模型
保存为 cinematic-handover.cjs,执行 node cinematic-handover.cjs。它只管理数据所有权与轨道反求,不执行真实输入锁、剧情轨道或摄像机 API;这些行为需要在现有引擎项目适配并验收。
const assert = require('node:assert/strict');
const valid = v => Array.isArray(v) && v.length === 3 && v.every(Number.isFinite);
function orbitFrom(eye, focus, fallbackYaw) {
assert(valid(eye) && valid(focus), 'invalid position');
assert(Number.isFinite(fallbackYaw), 'invalid fallback yaw');
const v = eye.map((x, i) => x - focus[i]), distance = Math.hypot(...v);
if (distance < 1e-9) return null;
const yawFromFallback = Math.hypot(v[0], v[2]) < 1e-9;
return { yaw: yawFromFallback ? fallbackYaw : Math.atan2(v[0], v[2]) * 180 / Math.PI,
elevation: Math.asin(Math.max(-1, Math.min(1, v[1] / distance))) * 180 / Math.PI,
distance, yawFromFallback };
}
function rebuild(focus, orbit) {
const y = orbit.yaw * Math.PI / 180, p = orbit.elevation * Math.PI / 180;
return [focus[0] + orbit.distance * Math.sin(y) * Math.cos(p),
focus[1] + orbit.distance * Math.sin(p),
focus[2] + orbit.distance * Math.cos(y) * Math.cos(p)];
}
class Handover {
constructor(preferences) {
assert(Number.isFinite(preferences.yaw), 'invalid fallback yaw');
this.preferences = { ...preferences }; this.serial = 0; this.active = null;
}
begin() { this.active = ++this.serial; return this.active; }
finish(token, eye, focus) {
if (token !== this.active || this.active === null) return false;
assert(valid(eye), 'invalid eye');
const orbit = focus === null ? null : orbitFrom(eye, focus, this.preferences.yaw);
// 释放当前接管身份,恢复方案只使用当前目标和当前姿态。
this.active = null;
return { mode: orbit ? 'player' : 'hold', orbit,
eye: [...eye], desiredDistance: this.preferences.distance };
}
}
const handover = new Handover({ distance: 4, shoulder: 'right', yaw: 37 });
const old = handover.begin(), current = handover.begin();
const eye = [3, 2, 4], focus = [1, 1, 1];
assert.equal(handover.finish(old, eye, focus), false);
assert.equal(handover.active, current);
const resumed = handover.finish(current, eye, focus);
assert.equal(resumed.desiredDistance, 4);
assert(rebuild(focus, resumed.orbit).every((v, i) => Math.abs(v - eye[i]) < 1e-9));
assert.equal(handover.finish(current, eye, focus), false);
const missing = handover.finish(handover.begin(), eye, null);
assert.equal(missing.mode, 'hold'); assert.equal(handover.active, null);
assert.equal(orbitFrom(focus, focus, 37), null);
assert.throws(() => orbitFrom([NaN, 0, 0], focus, 37), /invalid position/);
for (const height of [5, -5]) {
const pole = orbitFrom([0, height, 0], [0, 0, 0], 37);
assert.equal(pole.yaw, 37); assert(pole.yawFromFallback);
assert(rebuild([0, 0, 0], pole).every((v, i) => Math.abs(v - [0, height, 0][i]) < 1e-9));
}
console.log(`resume yaw=${resumed.orbit.yaw.toFixed(2)} elevation=${resumed.orbit.elevation.toFixed(2)}`);
console.log(`current distance=${resumed.orbit.distance.toFixed(3)} desired=${resumed.desiredDistance.toFixed(1)}`);
console.log('stale token, repeated finish, missing target, zero vector, poles: passed');预期输出:
resume yaw=33.69 elevation=15.50
current distance=3.742 desired=4.0
stale token, repeated finish, missing target, zero vector, poles: passed恢复过程继续服从当前规则
示例反求位置可精确重建 Eye,但完整姿态还包含剧情相机朝向与 FOV。若剧情并未看向玩家 Focus,轨道朝向不能当帧直接覆盖它;应把当前旋转交给过渡层,再逐渐恢复玩家的目标朝向和投影。
轨道反求也不能证明回程安全。剧情相机可能位于不同房间,直线返回会穿墙,应使用预设安全轨道或持续避障。输入何时恢复也应明确:可以在过渡结束时恢复,或提前恢复并让输入修改目标,但不能让新输入仍被旧剧情回调吞掉。
练习在剧情中移动角色并替换一次剧情请求,再依次触发旧结束回调、新结束回调和重复结束回调。记录当前拥有者、输入锁令牌与恢复姿态,确认只有新请求能够释放自身接管,玩家回到当前世界关系,而不是过去的摄像机位置。