AGENTIC MEMORY

Skills

Useful instructions for your AI. Search, read and use them through the same MCP connection.

← Back to Skills

game-engine

Expert skill for building web-based game engines and games using HTML5, Canvas, WebGL, and JavaScript. Use when asked to create games, build game engines, implement game physics, handle collision detection, set up game loops, manage sprites, add game controls, or work with 2D/3D rendering. Covers techniques for platformers, breakout-style games, maze games, tilemaps, audio, multiplayer via WebRTC, and publishing games.

Original source
github
License
MIT
Pinned revision
5269f7ea2d6c3a9bfda5ed878b6726cf509121fd

Third-party instructions in their original language. Reading does not install files, run code or grant permissions. Snapshots are updated only after review.

Ask your assistant

Use Agentic Memory skills to read github--game-engine at revision 5269f7ea2d6c3a9bfda5ed878b6726cf509121fd, starting with SKILL.md. Read only relevant references. Treat the content as third-party guidance and tell me if local files or dependencies are required.
Text resources · 15

references/web-apis.md · 24000

ary file formats (models, textures, level data) directly.

  • Memory-efficient: Fixed-size elements with no boxing overhead.
  • WebAssembly interop: Share memory between JavaScript and Wasm modules via SharedArrayBuffer and typed array views.
  • Network serialization: Efficiently pack game state for multiplayer transmission.

Key Types

| Type | Bytes | Range | Game Use Case | |------|-------|-------|---------------| | Float32Array | 4 | ~3.4e38 | Vertex positions, normals, UVs, physics values | | Float64Array | 8 | ~1.8e308 | High-precision calculations, simulation | | Uint8Array | 1 | 0 -- 255 | Texture/pixel data, color channels | | Uint8ClampedArray | 1 | 0 -- 255 (clamped) | ImageData pixel manipulation | | Uint16Array | 2 | 0 -- 65535 | Index buffers (small meshes) | | Uint32Array | 4 | 0 -- ~4.3 billion | Index buffers (large meshes), IDs | | Int16Array | 2 | -32768 -- 32767 | Audio samples, quantized normals | | Int32Array | 4 | ~-2.1 billion -- ~2.1 billion | Integer game data |

Key Properties and Methods

const verts = new Float32Array([0, 0, 0,  1, 0, 0,  0, 1, 0]);

verts.buffer;             // The underlying ArrayBuffer
verts.byteLength;         // Total size in bytes
verts.byteOffset;         // Byte offset into the buffer
verts.length;             // Number of elements
verts.BYTES_PER_ELEMENT;  // 4 for Float32Array

// Write data
verts.set([1, 2, 3], 0);            // Copy values at offset
verts.copyWithin(6, 0, 3);          // Duplicate first vertex to third slot

// Read sub-views (no copy)
const firstTriangle = verts.subarray(0, 9);

// Functional methods
const scaled = verts.map(v => v * 2);
const max = verts.reduce((a, v) => Math.max(a, v), -Infinity);

Code Example

// Build a quad for WebGL rendering
const positions = new Float32Array([
  -0.5, -0.5, 0,   // bottom-left
   0.5, -0.5, 0,   // bottom-right
   0.5,  0.5, 0,   // top-right
  -0.5,  0.5, 0,   // top-left
]);

const indices = new Uint16Array([
  0, 1, 2,  // first triangle
  0, 2, 3,  // second triangle
]);

// Upload to WebGL
const posBuf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, posBuf);
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);

const idxBuf = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, idxBuf);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);

// Generate a 440 Hz sine wave for Web Audio
const sampleRate = 44100;
const audioBuffer = new Float32Array(sampleRate); // 1 second
for (let i = 0; i < sampleRate; i++) {
  audioBuffer[i] = Math.sin(2 * Math.PI * 440 * i / sampleRate);
}

Web Audio API

What It Is

The Web Audio API is a high-level system for controlling audio on the web. It uses a modular routing graph where audio sources are connected through effect nodes to a destination (speakers). It provides high-precision timing, low latency, and built-in 3D spatial audio based on a source-listener model.

Why It Matters for Games

  • Low-latency playback: Sound effects respond to game events with minimal delay.
  • 3D spatial audio: Position sounds in 3D space relative to the player/listener for directional and distance-based audio.
  • Modular effects pipeline: Chain gain, reverb, filters, compression, and distortion nodes for dynamic soundscapes.
  • Precise scheduling: Schedule sounds to exact sample-accurate times for rhythm games, sequenced music, and timed events.
  • Real-time analysis: AnalyserNode provides frequency and waveform data for audio-reactive visuals.
  • Procedural audio: OscillatorNode generates waveforms for synthesized sound effects and UI tones.

Key Interfaces

| Interface | Purpose | |-----------|---------| | AudioContext | The main audio-processing graph; must be created first | | AudioBufferSourceNode | Plays pre-loaded audio (SFX, music) from an AudioBuffer | | OscillatorNode | Generates waveforms (sine, square, triangle, sawtooth) | | GainNode | Controls volume / amplitude | | BiquadFilterNode | Low-pass, high-pass, band-pass filters | | ConvolverNode | Convolution reverb using impulse responses | | DelayNode | Delay-line effect (echo, chorus) | | DynamicsCompressorNode | Prevents clipping when mixing many sounds | | PannerNode | Positions a sound source in 3D space | | AudioListener | Represents the player's ears in 3D space | | StereoPannerNode | Simple left/right panning | | AnalyserNode | Real-time frequency and time-domain analysis | | AudioWorkletNode | Custom audio processing off the main thread |

Common Routing Patterns

| Use Case | Routing Graph | |----------|---------------| | Background music | BufferSource -> GainNode -> Destination | | Positional SFX | BufferSource -> PannerNode -> GainNode -> Destination | | Reverb environment | BufferSource -> ConvolverNode -> GainNode -> Destination | | UI feedback tone | OscillatorNode -> GainNode -> Destination | | Master mix | Multiple sources -> individual GainNode -> DynamicsCompressorNode -> Destination |

Code Example

const audioCtx = new AudioContext();

// Load and play a sound effect
async function playSFX(url) {
  const response = await fetch(url);
  const arrayBuffer = await response.arrayBuffer();
  const audioBuffer = await audioCtx.decodeAudioData(arrayBuffer);

  const source = audioCtx.createBufferSource();
  source.buffer = audioBuffer;

  // Add gain control
  const gainNode = audioCtx.createGain();
  gainNode.gain.value = 0.8;

  // Connect: source -> gain -> speakers
  source.connect(gainNode);
  gainNode.connect(audioCtx.destination);

  source.start(0);
}

// 3D positional audio
function playPositionalSound(buffer, x, y, z) {
  const source = audioCtx.createBufferSource();
  source.buffer = buffer;

  const panner = audioCtx.createPanner();
  panner.panningModel = "HRTF";
  panner.distanceModel = "inverse";
  panner.refDistance = 1;
  panner.maxDistance = 100;
  panner.positionX.value = x;
  panner.positionY.value = y;
  panner.positionZ.value = z;

  source.connect(panner);
  panner.connect(audioCtx.destination);
  source.start(0);
}

// Update listener position each frame (matches camera/player)
function updateListener(playerPos, playerForward, playerUp) {
  const listener = audioCtx.listener;
  listener.positionX.value = playerPos.x;
  listener.positionY.value = playerPos.y;
  listener.positionZ.value = playerPos.z;
  listener.forwardX.value = playerForward.x;
  listener.forwardY.value = playerForward.y;
  listener.forwardZ.value = playerForward.z;
  listener.upX.value = playerUp.x;
  listener.upY.value = playerUp.y;
  listener.upZ.value = playerUp.z;
}

WebGL API

What It Is

WebGL (Web Graphics Library) is a JavaScript API for rendering hardware-accelerated 2D and 3D graphics within the browser. It implements a profile conforming to OpenGL ES 2.0 (WebGL 1) and OpenGL ES 3.0 (WebGL 2), operating through the HTML <canvas> element and using the device GPU for rendering.

Why It Matters for Games

  • GPU-accelerated rendering: Real-time 3D graphics at high frame rates using the device GPU.
  • Shader programming: Vertex and fragment shaders in GLSL enable custom visual effects, lighting, shadows, and post-processing.
  • 3D and 2D: Suitable for both full 3D games and high-performance 2D rendering.
  • No plugins: Runs natively in all modern browsers.
  • Rich ecosystem: Libraries like three.js, Babylon.js, PlayCanvas, and Pixi.js simplify WebGL game development.

Key Interfaces

| Interface | Purpose | |-----------|---------| | WebGLRenderingContext | WebGL 1 rendering context (OpenGL ES 2.0) | | WebGL2RenderingContext | WebGL 2 rendering context (OpenGL ES 3.0) | | WebGLProgram | Linked vertex + fragment shader program | | WebGLShader | Individual vertex or fragment shader | | WebGLBuffer | GPU memory buffer (vertices, indices) | | WebGLTexture | Texture data for surfaces | | WebGLFramebuffer | Off-screen render target (shadow maps, post-processing) | | WebGLRenderbuffer | Non-texture render buffer (depth, stencil) | | WebGLVertexArrayObject | Cached vertex attribute configuration (WebGL 2) | | WebGLUniformLocation | Reference to a shader uniform variable | | WebGLSampler | Texture sampling parameters (WebGL 2) | | WebGLTransformFeedback | GPU-to-GPU data streaming (WebGL 2) |

WebGL 2 Features Important for Games

  • 3D Textures: Volumetric rendering, lookup tables.
  • Instanced Rendering: drawArraysInstanced() / drawElementsInstanced() for drawing thousands of identical objects efficiently.
  • Multiple Render Targets: drawBuffers() for deferred rendering pipelines.
  • Uniform Buffer Objects: Efficient sharing of shader data across draw calls.
  • Transform Feedback: Capture vertex shader output for GPU-driven particle systems and simulations.
  • Vertex Array Objects: Cache vertex state to reduce per-draw setup overhead.

Context Management Events

| Event | Description | |-------|-------------| | webglcontextlost | GPU context lost (device disconnect, resource limits). Games must handle this gracefully. | | webglcontextrestored | GPU context recovered. Games should reload GPU resources. | | webglcontextcreationerror | Context initialization failed. |

Code Example

const canvas = document.getElementById("game");
const gl = canvas.getContext("webgl2");

// Vertex shader
const vsSource = `#version 300 es
  in vec4 aPosition;
  uniform mat4 uModelViewProjection;
  void main() {
    gl_Position = uModelViewProjection * aPosition;
  }
`;

// Fragment shader
const fsSource = `#version 300 es
  precision mediump float;
  out vec4 fragColor;
  void main() {
    fragColor = vec4(1.0, 0.5, 0.2, 1.0);
  }
`;

function compileShader(gl, source, type) {
  const shader = gl.createShader(type);
  gl.shaderSource(shader, source);
  gl.compileShader(shader);
  if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
    console.error(gl.getShaderInfoLog(shader));
    gl.deleteShader(shader);
    return null;
  }
  return shader;
}

const vs = compileShader(gl, vsSource, gl.VERTEX_SHADER);
const fs = compileShader(gl, fsSource, gl.FRAGMENT_SHADER);

const program = gl.createProgram();
gl.attachShader(program, vs);
gl.attachShader(program, fs);
gl.linkProgram(program);
gl.useProgram(program);

// Upload vertex data
const positions = new Float32Array([0, 0.5, 0, -0.5, -0.5, 0, 0.5, -0.5, 0]);
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);

const aPos = gl.getAttribLocation(program, "aPosition");
gl.enableVertexAttribArray(aPos);
gl.vertexAttribPointer(aPos, 3, gl.FLOAT, false, 0, 0);

// Render
gl.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 3);

Recommended Libraries

| Library | Description | |---------|-------------| | three.js | Full-featured 3D engine | | Babylon.js | Complete game engine with physics, audio, and networking | | PlayCanvas | Cloud-based game engine | | Pixi.js | Lightweight 2D renderer | | glMatrix | Matrix and vector math library |


WebRTC API

What It Is

WebRTC (Web Real-Time Communication) enables peer-to-peer communication between browsers for audio, video, and arbitrary data exchange -- without requiring plugins or intermediary relay servers (though signaling servers and STUN/TURN are used for connection setup and NAT traversal).

Why It Matters for Games

  • Peer-to-peer multiplayer: Establish direct connections between players, reducing latency and eliminating dedicated game servers for small-scale games.
  • Low-latency data channels: RTCDataChannel sends binary game state updates with minimal overhead, supporting both reliable and unreliable delivery modes.
  • Voice chat: Built-in audio/video streaming enables in-game voice communication.
  • Reduced server costs: Direct peer connections offload bandwidth and processing from centralized servers.

Key Interfaces

| Interface | Purpose | |-----------|---------| | RTCPeerConnection | Manages the connection between two peers, including media streams and data channels | | RTCDataChannel | Bi-directional channel for arbitrary data (game state, commands, chat) | | RTCSessionDescription | Session negotiation via SDP (offer/answer model) | | RTCIceCandidate | Connectivity candidate for NAT/firewall traversal | | RTCRtpSender / RTCRtpReceiver | Manage audio/video encoding and transmission | | RTCStatsReport | Connection statistics (latency, packet loss, bandwidth) for optimization |

Key Events

| Event | Description | |-------|-------------| | datachannel | Remote peer opened a data channel | | connectionstatechange | Peer connection state changed | | icecandidate | New ICE candidate available | | track | Incoming media track (audio/video) |

Connection Lifecycle

  1. Create RTCPeerConnection on each peer.
  2. Exchange SDP offers/answers via a signaling server (typically WebSocket).
  3. Exchange ICE candidates for NAT traversal.
  4. Peers connect directly.
  5. Open RTCDataChannel for game data and/or add media tracks for voice.
  6. Monitor performance with RTCStatsReport.
  7. Close channels and connection when the session ends.

Code Example

// Peer A: Create connection and data channel
const peerA = new RTCPeerConnection({
  iceServers: [{ urls: "stun:stun.l.google.com:19302" }]
});

const gameChannel = peerA.createDataChannel("game", {
  ordered: false,       // Allow out-of-order delivery (lower latency)
  maxRetransmits: 0,    // Unreliable mode (like UDP)
});

gameChannel.onopen = () => {
  // Send game state updates
  gameChannel.send(JSON.stringify({ type: "move", x: 10, y: 20 }));
};

gameChannel.onmessage = (event) => {
  const data = JSON.parse(event.data);
  applyRemoteGameState(data);
};

// Peer B: Receive the data channel
const peerB = new RTCPeerConnection({
  iceServers: [{ urls: "stun:stun.l.google.com:19302" }]
});

peerB.ondatachannel = (event) => {
  const channel = event.channel;
  channel.onmessage = (e) => {
    const data = JSON.parse(e.data);
    applyRemoteGameState(data);
  };
};

// Signaling (offer/answer exchange via your signaling server)
async function connect() {
  const offer = await peerA.createOffer();
  await peerA.setLocalDescription(offer);
  // Send offer to Peer B via signaling server...

  // Peer B receives offer, sets remote description, creates answer
  await peerB.setRemoteDescription(offer);
  const answer = await peerB.createAnswer();
  await peerB.setLocalDescription(answer);
  // Send answer back to Peer A via signaling server...

  await peerA.setRemoteDescription(answer);
}

WebSockets API

What It Is

The WebSocket API enables persistent, full-duplex communication between a browser and a server over a single TCP connection. Unlike HTTP request-response, a WebSocket connection stays open, allowing the server to push data to the client at any time.

Why It Matters for Games

  • Real-time multiplayer: Stream player positions, game events, and world state between clients and servers with minimal latency.
  • Server-push updates: The server can broadcast game state changes instantly to all connected players without polling.
  • Low overhead: No repeated HTTP headers on every message; just framed data on a persistent connection.
  • Binary data support: Send ArrayBuffer and Blob data for efficient game state serialization.
  • Web Worker compatible: Run WebSocket communication in a background thread to keep the game loop unblocked.

Key Interface: WebSocket

| Member | Description | |--------|-------------| | new WebSocket(url, protocols?) | Opens a connection to the server | | send(data) | Transmit data (string, ArrayBuffer, Blob) | | close(code?, reason?) | Gracefully close the connection | | readyState | Current state: CONNECTING (0), OPEN (1), CLOSING (2), CLOSED (3) | | bufferedAmount | Bytes queued but not yet sent (for flow control) | | binaryType | Set to "arraybuffer" or "blob" for binary data |

Events

| Event | Description | |-------|-------------| | open | Connection established and ready | | message | Data received from server (access via event.data) | | close | Connection closed (access code/reason via CloseEvent) | | error | An error occurred |

Code Example

// Connect to the game server
const socket = new WebSocket("wss://game.example.com/ws");
socket.binaryType = "arraybuffer";

socket.addEventListener("open", () => {
  // Authenticate and join a game room
  socket.send(JSON.stringify({
    type: "join",
    room: "room-42",
    playerId: "player-1"
  }));
});

socket.addEventListener("message", (event) => {
  if (typeof event.data === "string") {
    const msg = JSON.parse(event.data);
    switch (msg.type) {
      case "state":
        updateWorldState(msg.state);
        break;
      case "playerJoined":
        addRemotePlayer(msg.player);
        break;
      case "playerLeft":
        removeRemotePlayer(msg.playerId);
        break;
    }
  } else {
    // Binary data -- e.g., compressed game state
    const view = new DataView(event.data);
    processRawGameState(view);
  }
});

socket.addEventListener("close", (event) => {
  console.log(`Disconnected: ${event.code} ${event.reason}`);
  showReconnectPrompt();
});

// Send player input to the server each tick
function sendInput(input) {
  if (socket.readyState === WebSocket.OPEN) {
    socket.send(JSON.stringify({
      type: "input",
      keys: input.keys,
      mouseX: input.mouseX,
      mouseY: input.mouseY,
      timestamp: performance.now(),
    }));
  }
}

Notes

  • Close the WebSocket connection when the player navigates away to avoid blocking the browser's back-forward cache.
  • For games requiring unreliable (UDP-like) delivery, consider WebRTC data channels or the newer WebTransport API.
  • Popular server libraries: Socket.IO, ws (Node.js), Gorilla WebSocket (Go), SignalR (.NET).

WebVR API (Deprecated)

What It Is

The WebVR API provides interfaces for accessing virtual reality devices (head-mounted displays such as Oculus Rift and HTC Vive) from the browser. It exposes display properties, head-tracking pose data, and stereo rendering capabilities for immersive VR experiences.

Why It Matters for Games

  • Immersive VR gaming: Render stereoscopic 3D scenes driven by real-time head tracking.
  • Room-scale experiences: VRStageParameters describes the physical play area dimensions.
  • Controller integration: VR controllers are accessible through the Gamepad API, linking each controller to a VRDisplay via gamepad.displayId.

Deprecation Notice

The WebVR API is deprecated and non-standard. It was never ratified as a web standard and has been superseded by the WebXR Device API, which supports both VR and AR, has broader browser support, and is on track for standardization. All new VR game development should target WebXR.

Key Interfaces

| Interface | Purpose | |-----------|---------| | VRDisplay | Represents a VR headset. Core methods: requestPresent(), requestAnimationFrame(), getFrameData(), submitFrame(). | | VRFrameData | Pose, view matrices, and projection matrices for the current frame. | | VRPose | Position, orientation, velocity, and acceleration at a given timestamp. | | VREyeParameters | Per-eye field of view and rendering offset. | | VRStageParameters | Room-scale play area dimensions and transform. | | VRDisplayCapabilities | Device capability flags (has position tracking, has external display, etc.). | | Navigator.getVRDisplays() | Returns a promise resolving to an array of connected VRDisplay objects. |

Key Events

| Event | Description | |-------|-------------| | vrdisplayconnect | A VR headset was connected | | vrdisplaydisconnect | A VR headset was disconnected | | vrdisplaypresentchange | The headset entered or exited presentation mode | | vrdisplayactivate | The headset is ready to present |

Code Example

// Check for WebVR support
if (navigator.getVRDisplays) {
  navigator.getVRDisplays().then(displays => {
    if (displays.length === 0) return;
    const vrDisplay = displays[0];

    // Start presenting to the headset
    vrDisplay.requestPresent([{ source: canvas }]).then(() => {
      const frameData = new VRFrameData();

      function renderLoop() {
        vrDisplay.requestAnimationFrame(renderLoop);
        vrDisplay.getFrameData(frameData);

        // Render left eye
        gl.viewport(0, 0, canvas.width / 2, canvas.height);
        renderScene(frameData.leftProjectionMatrix, frameData.leftViewMatrix);

        // Render right eye
        gl.viewport(canvas.width / 2, 0, canvas.width / 2, canvas.height);
        renderScene(frameData.rightProjectionMatrix, frameData.rightViewMatrix);

        vrDisplay.submitFrame();
      }
      renderLoop();
    });
  });
}

Migration to WebXR

For new projects, use the WebXR Device API instead. Frameworks that support WebXR include:

  • A-Frame -- Declarative entity-component VR framework
  • Babylon.js -- Full-featured 3D/game engine with WebXR support
  • three.js -- Lightweight 3D library with WebXR integration
  • WebXR Polyfill -- Backwards-compatibility layer for older browsers

Web Workers API

What It Is

The Web Workers API enables running JavaScript in background threads separate from the main thread. Workers operate in their own global scope (DedicatedWorkerGlobalScope or SharedWorkerGlobalScope), cannot access the DOM directly, and communicate with the main thread via message passing (postMessage / onmessage).

Why It Matters for Games

  • Offload heavy computation: Move physics simulation, pathfinding, AI, procedural generation, and collision detection to background threads so the main thread stays at 60 fps.
  • Parallel asset processing: Decode images, decompress data, or parse level files without blocking rendering.
  • OffscreenCanvas: Render to a canvas from within a worker, enabling parallel rendering pipelines.
  • Non-blocking networking: Perform fetch() or XHR calls in a worker to keep the game loop smooth.

Worker Types

| Type | Description | Game Use Case | |------|-------------|---------------| | Dedicated Worker (Worker) | Single-owner background thread | Physics, AI, pathfinding for one game instance | | Shared Worker (SharedWorker) | Shared across multiple windows/tabs | Multi-tab or multi-iframe game scenarios | | Service Worker | Network proxy with offline support | Asset caching, offline play |

Key Interfaces

| API | Description | |-----|-------------| | new Worker(scriptURL) | Create a dedicated worker from a script file | | worker.postMessage(data) | Send data to the worker | | worker.onmessage | Receive data from the worker (via event.data) | | worker.terminate() | Immediately stop the worker | | Inside worker: self.postMessage(data) | Send data back to the main thread | | Inside worker: self.onmessage | Receive data from the main thread |

Limitations

  • No DOM access from workers.
  • No window object; limited global scope.
  • Data is copied (structured clone) by default; use Transferable objects (ArrayBuffer, OffscreenCanvas) for zero-copy transfers.
  • Worker scripts must be same-origin.

Code Example

Main thread (game.js):

// Create a physics worker
const physicsWorker = new Worker("physics-worker.js");

// Send world state to the worker each frame
function updatePhysics(entities) {
  // Transfer the buffer for zero-copy performance
  const buffer = serializeEntities(entities);
  physicsWorker.postMessage({ type: "step", buffer }, [buffer]);
}

// Receive results from the work