Skip to content

Pixel Collision Mapping

The mapPixelToCollisionType function maps RGBA pixel values to specific collision types. It is useful in game development for interpreting pixel-based collision maps.

Function Definition

export function mapPixelToCollisionType(r: number, g: number, b: number, a: number): number;

Parameters

  • r: number - Red component of the pixel (0-255).
  • g: number - Green component of the pixel (0-255).
  • b: number - Blue component of the pixel (0-255).
  • a: number - Alpha (transparency) component of the pixel (0-255).

Return Value

Returns a numerical collision type based on the pixel color:

  • 0 - Transparent (no collision)
  • 1 - Wall (red pixels)
  • 2 - Grass (green pixels)
  • 3 - Water (blue pixels)
  • 4 - Default collision (other colors)

Example Usage

const collisionType = mapPixelToCollisionType(255, 0, 0, 255);
console.log(collisionType); // Output: 1 (Wall)