4.24.0 • Published 10 months ago

@dill-pixel/plugin-crunch-physics v4.24.0

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

Crunch Physics Plugin

@dill-pixel/physics-crunch

A lightweight, grid-based AABB physics plugin for Dill Pixel games. Optimized for 2D platformers and games requiring precise, pixel-perfect collisions.

Features

  • 🎯 Pixel-perfect AABB collision detection
  • 📦 Grid-based spatial partitioning for efficient collision checks
  • 🎮 Optimized for 2D platformers and action games
  • 🧩 System supports Actors (Dynamic), Solids (Static), and Sensors (Trigger - Dynamic or Static)
  • 🎨 Debug visualization tools
  • 🔄 Object pooling support
  • 🎬 Scene-based integration

Installation

npm install @dill-pixel/physics-crunch

Quick Start

// in dill-pixel.config.ts
defineConfig({
  //... other config
  plugins: ['crunch-physics', { autoLoad: false }],
});

// in your scene file
import { Scene } from 'dill-pixel';

// exports
export const plugins = ['crunch-physics']; // loads the plugin for your scene

// in your scene class
export default class MyCrunchScene extends Scene {
  get physics() {
    return this.app.getPlugin('crunch-physics') as ICrunchPhysicsPlugin;
  }

  async initialize() {
    await this.physics.initialize({
      gridSize: 32,
      gravity: 980,
      maxVelocity: 1000,
      debug: true,
    });

    // Create a player
    const player = physics.createActor({
      type: 'Player',
      position: [100, 100],
      size: [32, 64],
    });

    // Create a platform
    const platform = physics.createSolid({
      type: 'Platform',
      position: [0, 500],
      size: [800, 32],
    });

    // Create a coin pickup
    const coin = physics.createSensor({
      type: 'Coin',
      position: [200, 400],
      size: [32, 32],
    });
  }
}

Core Components

Actors

Dynamic entities that can move and collide (players, enemies, projectiles).

class Player extends Actor {
  update(dt: number) {
    // Custom movement logic
    if (this.app.input.isKeyDown('ArrowRight')) {
      this.velocity.x = 200;
    }
  }

  onCollide(result: CollisionResult) {
    // Handle collisions
    if (result.solid.type === 'Spikes') {
      this.die();
    }
  }
}

Solids

Static or moving collision objects (platforms, walls, obstacles).

// Create a moving platform
const platform = physics.createSolid({
  type: 'Platform',
  position: [100, 300],
  size: [200, 32],
});

// Animate platform movement
gsap.to(platform, {
  x: 500,
  duration: 2,
  yoyo: true,
  repeat: -1,
});

Sensors

Trigger zones for detecting overlaps (collectibles, checkpoints, damage zones).

class Coin extends Sensor {
  onActorEnter(actor: Actor) {
    if (actor.type === 'Player') {
      increaseScore(10);
      this.physics.removeSensor(this);
    }
  }
}

Groups

Containers for managing collections of entities that move together.

// Create a moving platform with hazards
const group = physics.createGroup({
  type: 'MovingPlatform',
  position: [100, 300],
});

const platform = physics.createSolid({
  type: 'Platform',
  size: [200, 32],
});

const spikes = physics.createSensor({
  type: 'Spikes',
  position: [0, -32],
  size: [200, 32],
});

group.add(platform);
group.add(spikes);

Advanced Features

Spatial Partitioning

The plugin uses a grid-based spatial partitioning system to efficiently handle collision detection:

// Configure grid size for your game's scale
physics.system.gridSize = 32;

Debug Visualization

Enable debug rendering to visualize collision boxes and the spatial grid:

physics.system.debug = true;

Collision Resolution

Custom collision handling with type-based filtering:

physics.system.setCollisionResolver((collisions) => {
  for (const collision of collisions) {
    // Handle specific collision types
    if (collision.type === 'Player|Enemy') {
      handlePlayerEnemyCollision(collision);
    }
  }
});

Performance Tips

  • Use appropriate grid sizes for your game's scale
  • Enable culling for large levels
  • Utilize object pooling for frequently created/destroyed entities
  • Use sensor overlaps instead of continuous collision checks where possible

License

MIT © Dill Pixel

4.24.0

10 months ago

4.23.0

10 months ago

4.22.1

10 months ago

4.22.0

10 months ago

4.21.5

10 months ago

4.21.4

10 months ago

4.21.3

10 months ago

4.21.2

10 months ago

4.21.1

10 months ago

4.21.0

10 months ago

4.20.2

10 months ago

4.20.1

10 months ago

4.20.0

10 months ago

4.19.4

10 months ago

4.19.3

10 months ago

4.19.2

10 months ago

4.19.1

10 months ago

4.19.0

10 months ago

4.18.7

11 months ago

4.18.6

11 months ago

4.18.5

11 months ago

4.18.4

11 months ago

4.18.3

11 months ago

4.18.2

11 months ago

4.18.1

11 months ago

4.18.0

11 months ago

4.17.0

11 months ago

4.16.1

11 months ago

4.16.0

11 months ago

4.15.12

11 months ago

4.15.11

11 months ago

4.15.10

12 months ago

4.15.9

12 months ago

4.15.8

12 months ago

4.15.7

12 months ago

4.15.6

12 months ago

4.15.5

12 months ago

4.15.4

12 months ago

4.15.3

12 months ago

4.15.2

12 months ago

4.15.1

12 months ago

4.15.0

1 year ago

4.14.0

1 year ago

4.13.0

1 year ago

4.12.0

1 year ago

4.11.0

1 year ago

4.10.2

1 year ago

4.10.1

1 year ago