/** * Choose paperdoll view (yaw/pitch) and animation frame. * Yaw 0 = character front (Paperdoll/glTF convention). */ var PpdaViewFrame = { /** * Relative viewing yaw in degrees [0,360): direction from NPC to player, * minus the NPC's facing yaw (degrees, 0 = faces +X/east in world… mapped * so that when the player looks at the NPC's front, relative yaw ≈ 0). * * World: +X east, +Y south (legacy raycaster). NPC facingYawDeg: 0 looks * toward +X. Paperdoll yaw 0 is front. We map world facing so that the * camera looking opposite to facing sees front. */ relativeYawDeg: function (playerX, playerY, npcX, npcY, npcFacingDeg) { var dx = playerX - npcX; var dy = playerY - npcY; // Angle of vector NPC→player in world degrees (0=+X, 90=+Y). var toPlayer = Math.atan2(dy, dx) * 180 / Math.PI; if (toPlayer < 0) { toPlayer += 360; } // When player stands in front of NPC, toPlayer ≈ npcFacing. // Paperdoll wants yaw 0 for front → relative = toPlayer - facing. var rel = toPlayer - (npcFacingDeg || 0); rel = ((rel % 360) + 360) % 360; return rel; }, nearestInList: function (value, list, circular360) { if (!list || !list.length) { return 0; } var best = list[0]; var bestD = Infinity; for (var i = 0; i < list.length; i++) { var v = list[i]; var d = Math.abs(value - v); if (circular360) { d = Math.min(d, 360 - d); } if (d < bestD) { bestD = d; best = v; } } return best; }, pickYaw: function (relativeYawDeg, yaws) { return this.nearestInList(((relativeYawDeg % 360) + 360) % 360, yaws || [0], true); }, pickPitch: function (playerPitchRad, pitches) { // player pitch: 0 = horizon; positive looks down in this game. // Paperdoll: positive pitch = from above. Invert sign for mapping. var deg = -(playerPitchRad || 0) * 180 / Math.PI; return this.nearestInList(deg, pitches || [0]); }, frameIndex: function (timeSec, fps, frameCount) { var n = Math.max(1, frameCount | 0); var f = Math.max(1, fps | 0); var idx = Math.floor((timeSec || 0) * f); idx %= n; if (idx < 0) { idx += n; } return idx; }, findClip: function (ppda, clipTokenOrName) { var clips = ppda && ppda.clips || []; for (var i = 0; i < clips.length; i++) { if (clips[i].token === clipTokenOrName || clips[i].name === clipTokenOrName) { return clips[i]; } } return clips[0] || null; }, findView: function (frame, pitch, yaw) { var views = frame && frame.views || []; for (var i = 0; i < views.length; i++) { if (views[i].pitch === pitch && views[i].yaw === yaw) { return views[i]; } } return null; }, uvRect: function (view, atlasW, atlasH) { if (!view) { return { u0: 0, v0: 0, u1: 1, v1: 1, x: 0, y: 0, w: 0, h: 0 }; } var w = atlasW || 1; var h = atlasH || 1; return { x: view.x, y: view.y, w: view.w, h: view.h, u0: view.x / w, v0: view.y / h, u1: (view.x + view.w) / w, v1: (view.y + view.h) / h }; } }; if (typeof module !== "undefined" && module.exports) { module.exports = PpdaViewFrame; }