tilecollider

package module
v1.4.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2024 License: MIT Imports: 2 Imported by: 1

README

GoDoc

tilecollider

A simple 2D tile-based collision detection package for Go.

screen

Features

  • Fast tile-based collision detection
  • Easy integration with game engines like Ebitengine
  • Generic tile map support with any Integer type [y][x]T
  • Support for non-square tiles (different width and height values)
  • Adaptive iteration count based on movement speed (anti-tunneling)

Installation

go get github.com/setanarut/tilecollider

Usage

See the examples directory for usage example.

Run Ebitengine example on your machine

go run github.com/setanarut/tilecollider/examples/demo@latest

Documentation

Overview

Package tilecollide provides collision detection for rectangles and 2D tilemaps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collider added in v1.4.0

type Collider[T Integer] struct {
	Collisions     []CollisionInfo[T] // List of collisions from last check
	TileSize       [2]int             // Width and height of tiles
	TileMap        [][]T              // 2D grid of tile IDs
	NonSolidTileID T                  // Sets the ID of non-solid tiles. Defaults to 0.
}

Collider handles collision detection between rectangles and a 2D tilemap

func NewCollider added in v1.4.0

func NewCollider[T Integer](tileMap [][]T, tileWidth, tileHeight int) *Collider[T]

NewCollider creates a new tile collider with the given tilemap and tile dimensions

func (*Collider[T]) Collide added in v1.4.0

func (c *Collider[T]) Collide(rectX, rectY, rectW, rectH, moveX, moveY float64, onCollide CollisionCallback[T]) (float64, float64)

Collide checks for collisions when moving a rectangle and returns the allowed movement

type CollisionCallback added in v1.1.0

type CollisionCallback[T Integer] func([]CollisionInfo[T], float64, float64)

CollisionCallback is called when collisions occur, receiving collision info and final movement

type CollisionInfo added in v1.1.0

type CollisionInfo[T Integer] struct {
	TileID     T      // ID of the collided tile
	TileCoords [2]int // X,Y coordinates of the tile in the tilemap
	Normal     [2]int // Normal vector of the collision (-1/0/1)
}

CollisionInfo stores information about a collision with a tile

type Integer

type Integer = constraints.Integer

Integer is a constraint that matches any integer type.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL