K2Boy
k2boy is an open-source reimplementation of the Prince of Persia 3D engine for macOS, PlayStation Vita, and web.
- Type: PC port
- Game: Prince of Persia 3D
- Runs on macOS
- By xesf
- Latest release v0.3.0, 2026-11-08
- Source: https://github.com/xesf/k2boy
README
k2boy
An open-source reimplementation of the engine of Prince of Persia 3D (Red Orb Entertainment, 1999), rebuilt from the original binary with IDA Pro. It plays the retail game's own data on macOS and the PlayStation Vita, and in a browser.
The original's internal codename was K2 (C:\k2\K2boy\Source\...), hence the name.
You need your own copy of the game. No game data is in this repository: it is read at run time from a retail install that you provide. k2boy is not affiliated with or endorsed by Ubisoft, Broderbund, Red Orb Entertainment or Jordan Mechner.
Contents
- Status
- Getting started
- Controls
- PlayStation Vita
- Web runtime
- Developer tools
- How it works
- Repository layout
- Documentation
- Principles
- Credits and licence
Status
Playable from the title screen through the first level (the prison, prisonfix). It is a work in progress: some systems follow the binary exactly, and others are stand-ins until that part is reversed. The docs say which is which.
| Area | State |
|---|---|
| Boot | The original's module manager: Red Orb logo movie → title screen → level |
| Front end | Title screen, in-game menu, Options (sound volume, difficulty, controls), Load/Save, Cinematics, Credits, drawn from the original art with its own TrueType font |
| Levels | .lvl regions, every .K2A room, materials and textures, fog and clip planes |
| Prince | Skeleton and skills from .act/.skl, walking and running with the original's start, stop and turn steps, jumping (the original's launch velocities and gravity), falls that hurt, ledges (climb, hang, shimmy, pull-up), poles (climb, slide), bars (hang, shimmy, swing and launch), swinging ropes, crates (push, pull, climb on), riding moving platforms, swimming and diving, picking things up and drinking potions |
| Combat | Sword drawn and sheathed, attacks, blocks, feints, counters, follows, clashes, dying and finishing moves, taunts; guard AI and timing from fight.ini and enemy.dat; the bow and archers; health in bottles |
| Traps and scripts | The trap message language: doors, plates, areas, messengers, movers (moves, turns, loops), water raising and lowering, cutscene cameras, fixtures (prisoners, rats, levers, cranes, carts), pickups and potions, spikes and blades, dart and fireball launchers, flamethrowers, collapsing floors, abysses, crate and arrow targets, arrow chargers |
| Effects | NetImmerse 2.3 .nif particle effects (potions, SpecialEffect traps) |
| Audio | .sfx sounds, positional ambiences, skill sound keys, a software mixer |
| Cutscenes | QuickTime .mov with Sorenson Video 1 and QDesign Music 2, all decoded in-tree |
| Saves | Our own text format under ~/.k2boy, with the level's state (traps, moved crates, pickups, dead guards); not yet the original's .pop |
Every level loads, and the Ivory Tower plays through its opening; the later levels are not yet played end to end. Not yet: the original's save format, graphics options and key rebinding, fountains and light switches, and the special arrows' possession and swarm.
Getting started
Requirements
- The game data: a retail Prince of Persia 3D install, copied to
data/at the repository root (or anywhere, passed asDATA=). Paths inside it are matched case-insensitively. - macOS: the Xcode command line tools and CMake 3.16 or later. Nothing else: there are no third-party libraries.
Build and play
make play # optimised build, then the game from the Red Orb logo
make play MENU=0 # straight into the level, no front end
make play CONTROLS=classic # the original's tank controls
make play REGION=1 # start in another region of the level
make play TRACE=1 # print every trap message
make play ARGS="--mute" # pass extra flags
make on its own builds the Debug configuration; make play-debug plays it. make help lists
every target.
Saves, settings and logs go to ~/.k2boy/.
Controls
Two schemes, switched with Tab in game or in Options:
- Modern: movement is relative to the camera, and the mouse or the right stick turns the camera. A full stick push runs.
- Classic: the original's tank controls. W and S walk along his facing, A and D turn him, and the camera stays behind.
| Action | Keyboard | Gamepad (Xbox / PlayStation / MFi) |
|---|---|---|
| Move (runs, as the original's NumPad 8 does) | WASD / arrows | left stick, d-pad (a part push walks) |
| Step: careful walk, step back, sidestep, keeping his facing (the original's Walk key) | Shift + direction | LT + direction |
| Camera | mouse drag | right stick |
| Jump | Space | A |
| Action: grab a crate, climb, talk, pull levers, pick up | E | X |
| Climb onto a crate | E while pushing toward it | X + forward |
| Crouch (hold); move to crawl through low gaps | C | B, with the sword away |
| Draw / sheathe sword | R | Y |
| Attack (left / right / centre by the stick) | F | B |
| Feint (a swing that stops short) | Shift + F | RB + B |
| Block (it turns to meet the swing coming) | G | LB |
| With the sword drawn: fighting steps / dodge | direction / tap it twice | the same |
| Bow: ready / put away; hold attack to draw, let go to loose | B, then F | L3, then B |
| Game menu | Esc | Start |
| Motion camera (pads with a gyroscope) | — | Select |
| Free-fly camera / trigger volumes | V / T | — |
Sword moves, as the original has them: block a swing from the left or right and attack the same side at once to counter; attack the next side (left, centre, right, left) just as a blow lands to follow it; a fighter brought to his last point is dying, and the next attack is a finishing move.
Ledges: E at a ledge in reach climbs it. Jump under a higher one to catch it, then press up to pull up, down to let go, and left or right to shimmy. Swimming: in deep water, move to swim, E dives, Space rises. The bottle at the bottom left is your air.
PlayStation Vita
A native port: a GXM renderer, both touch panels, the gyroscope, and work spread across three CPU cores.
make vita # → build-vita/k2boy.vpk (VitaSDK at $VITASDK or ~/vitasdk)
- Install
k2boy.vpkwith VitaShell. - Copy the game data to
ux0:data/k2boy/data/. - The first launch compiles the two shaders with the system's
ur0:data/libshacccg.suprxand saves them toux0:data/k2boy/shaders/. Packages built after those files are added toplatforms/vita/shaders/carry them, and need no compiler.
| Vita | Game |
|---|---|
| Left stick / d-pad, right stick | move, camera |
| Cross / Circle / Square / Triangle | jump / attack (crouch with the sword away) / action / draw sword |
| L / R | block / run |
| Start / Select | game menu / motion camera |
| Front touch | tap menus, drag to look |
| Rear touch | drag to look |
Logs go to ux0:data/k2boy/log.txt and err.txt. Details: docs/plan/07-vita.md.
Web runtime
web/ holds a second runtime for the same data, in vanilla JavaScript and WebGPU, with no build
step and no dependencies:
make web # then open http://localhost:8080/web/
See web/README.md and docs/plan/06-web-experiment.md.
Developer tools
The binary doubles as a toolbox. Most of these run headless:
make check # mount the VFS, decode every image, parse every level and .K2A
make dump F=Actors/PICK_Gem/PICK_Gem.K2A
make skl F=Actors/Prince/Skills/Running_L.skl
make room R=geometry/rooms/prisonfix/prison1.k2a
make actor A=geometry/prince/master.dat
make levels | skills | actors | spawns | materials | skeletons | modules
Scripted, deterministic runs, used to check gameplay changes with screenshots:
./build/k2boy --boot --notrig --place -66.07,20.25,29.25,0 \
--hold _@0.1-0.2 --hold W@1.8-2.1 --fixeddt 0.05 --frames 100 --shot out.bmp data
| Flag | |
|---|---|
--place x,y,z,yaw | put the Prince somewhere |
--hold KEYS@from-to | hold keys between two times (_ is Space, ^ is Shift) |
--send "target,message" | send a trap message, as a script would |
--fixeddt S --frames N --shot F.bmp | step N frames of S seconds, then save a screenshot |
--movie NAME | play a cutscene |
--trapmsg | log every trap message |
--noclip, --noanim, --mute, --notrig | collision, animation, sound and trigger volumes off |
docs/reference/command-line.md covers the original game's
own flags.
How it works
Prince of Persia 3D is not a bespoke engine. It is three middleware stacks glued together by Red Orb's K2 layer:
| Layer | Original | Here |
|---|---|---|
| Scene graph and renderer | NetImmerse 2.3 (NDL) on Direct3D | our scene graph; OpenGL 3.3 or GXM behind gfx.h |
| Characters | Motion Factory Motivate (actors, skills, planner) | .act / .skl readers, skeleton posing, the Prince's states |
| Game logic | Red Orb K2: traps, PrinceObject, CameraLogic | engine/ and main.cpp, ported function by function |
| Video | QuickTime: Sorenson Video 1, QDesign Music 2 | in-tree demuxer and decoders |
| Audio | Aureal A3D 2, DirectSound | a software mixer behind platform.h |
The retail binary is a Visual C++ 6 build with full RTTI, so 818 class names survive and almost every vtable can be named. The work happens in IDA through an MCP server: find the original function, port it, rename it in the IDA database to match, and compare the behaviour in game. Where the binary has not been traced yet, the code says that what it does is our own.
The engine is C++11, split behind two small interfaces:
src/platform/platform.h: window, input, time, audio, files, threads. Implementations: macOS (Cocoa) and Vita.src/gfx/gfx.h: rendering, modelled on the NetImmerse property set. Implementations: OpenGL 3.3 and GXM.
A new platform means another implementation of each, with nothing above them changed.
Repository layout
src/
main.cpp the game: boot, scenes, the Prince, traps, combat, camera, HUD
engine/ scene graph, collision, trap scripts, mixer, fight tuning, job pool
assets/ .K2A, .act, .skl, .lvl, master.dat, .sfx, .nif, BMP/TGA readers
media/ QuickTime demuxer, inflate, SVQ1 and QDM2 decoders
gfx/ gfx.h, OpenGL and GXM backends, TrueType rasterizer
ui/ 2D canvas, GameText, front-end screens
platform/ platform.h, macOS and Vita backends, case-insensitive VFS
platforms/vita/ Vita CMake project, LiveArea template, packaged shaders
web/ the WebGPU runtime
docs/ reverse-engineering notes (below)
tools/ helper scripts
data/ your retail install (git-ignored)
Documentation
docs/README.md indexes everything. The main entry points:
docs/ARCHITECTURE.md: binary inventory, subsystem map, data flowdocs/formats/: every file format (.K2A,.act,.skl,.lvl,.nif,.sfx,.mov, saves, text and tuning files)docs/reference/: recovered systems: class hierarchy, collision, trap scripts, crates, jumps, combat, the trap factory, the scripting VMdocs/plan/: the roadmap, the IDA setup and the platform plans
Principles
- The binary is the spec. Behaviour is taken from the disassembly. Where it has not been traced yet, the code says so.
- No third-party dependencies. Codecs, formats and the font rasterizer are written in-tree or ported in-tree from GPL-3 sources. Only the platform's own system libraries are used.
- No game data in the repository. Everything comes from the user's install at run time.
The Vita's LiveArea art (
platforms/vita/sce_sys) is promotional artwork.
Credits and licence
k2boy is licensed under the GNU General Public License v3.0: see LICENSE.
Code ported in-tree from other GPL-3 projects:
- the Sorenson Video 1 and QDesign Music 2 decoders, adapted from ScummVM;
- the QuickTime demuxer, inflate and TrueType rasterizer, from the author's own X-Files engine reimplementation.
Prince of Persia is a trademark of Ubisoft Entertainment. The game, its data and its art belong to their respective owners.