#==============================================================================
# Tileset pixel collision
#------------------------------------------------------------------------------
# Author: Timtrack
# date: 14/03/2026
# Requires:
# - Zeus81's bitmap export
# - Victor's Engine - Pixel Movement
#==============================================================================
$imported ||= {}
$imported["TIM-TilesetPixelCollision"] = true
#==============================================================================
# Description
#------------------------------------------------------------------------------
# Allows more accurate collisions of tilesets, similar to Hime's Collision map,
# it will use a picture associated to each tileset's picture such that
# red means the tile's pixel is passable while transparent means the tile's
# pixel isn't. This will only apply to tiles B-E that are impassable (X), other
# passability remains unchanged by the picture.
#
# Note that this script is not plug and play, you must design a collsion sheet
# for each tileset graphic file, this script can generate automatically a
# default collision sheet for each used tileset if you set Generate to true and
# uses Zeus81's bitmap export.
#==============================================================================
# Installation: put it below the required scripts, you must create a collision
# file for each tilesheet in your tileset folder (use Generate option to
# generate them quickly)
#==============================================================================
# Terms of use: free to use in any game, please give credit
#==============================================================================
#==============================================================================
# TIM::TilesetPixelCollision
#==============================================================================
module TIM
module TilesetPixelCollision
module Sheets
Generate = false #set to true to make new collision sheets for each
#graphic tileset in your database. (slow process)
Overwrite = false #if generate is true, do you want to overwrite former
#collision sheets?
SUFFIX = "_collision"
FOLDER = "Collision/" #in Graphics/Tilesets
end #Sheets
#color generated for impassable pixels, passsable color is (0,0,0)
IMPASSABLE_COLOR = Color.new(255,0,0)
#--------------------------------------------------------------------------
# collide_name -> name of the collision file associated to tileset graphic
#--------------------------------------------------------------------------
def self.collide_name(tilename)
Sheets::FOLDER + tilename + Sheets::SUFFIX
end
#--------------------------------------------------------------------------
# pixel_impassable? -> when generating collisions, what makes a color
# impassable?
#--------------------------------------------------------------------------
def self.pixel_impassable?(color)
color.alpha > 0
end
#--------------------------------------------------------------------------
# passable? -> when checking the collision, what makes a color passable?
#--------------------------------------------------------------------------
def self.passable?(color)
color.red == 0
end
PIXEL_STEP = 4 #accuracy of checks, based on Victor's script, do not change
end #TilesetPixelCollision
end #TIM
#==============================================================================
# TIM::TilesetPixelCollision
#==============================================================================
module DataManager
#--------------------------------------------------------------------------
# alias method: load_database -> generates collide files when option is true
#--------------------------------------------------------------------------
class << self; alias tim_tpc_load_database load_database; end
def self.load_database
tim_tpc_load_database
generate_tiles_collision if TIM::TilesetPixelCollision::Sheets::Generate
end
#--------------------------------------------------------------------------
# new method: generate_tiles_collision
#--------------------------------------------------------------------------
def self.generate_tiles_collision
handled_names = []
dirname = "Graphics/Tilesets/"+TIM::TilesetPixelCollision::Sheets::FOLDER
Dir.mkdir(dirname) unless FileTest.directory?(dirname)
$data_tilesets.each do |t|
next unless t
5.upto(8) do |i|
name = t.tileset_names[i]
next if name.empty? || handled_names.include?(name)
gen_tile_collision(name)
handled_names.push(name)
end
end
end
#--------------------------------------------------------------------------
# new method: gen_tile_collision
#--------------------------------------------------------------------------
def self.gen_tile_collision(tilename)
puts "Gen Collision of " + tilename
md = TIM::TilesetPixelCollision
output = md.collide_name(tilename)
bexist = true
Cache.tileset(output) rescue bexist = false
return if !md::Sheets::Overwrite && bexist
puts "Make new one..."
data = Cache.tileset(tilename)
return unless data
out = convert_to_collider(data)
out.export("Graphics/Tilesets/" + output)
out.dispose
puts "done!"
end
#--------------------------------------------------------------------------
# new method: convert_to_collider
#--------------------------------------------------------------------------
def self.convert_to_collider(tilesheet)
out = Bitmap.new(tilesheet.width, tilesheet.height)
0.upto(out.height-1) do |y|
0.upto(out.width-1) do |x|
c = tilesheet.get_pixel(x,y)
next unless TIM::TilesetPixelCollision.pixel_impassable?(c)
out.set_pixel(x,y,TIM::TilesetPixelCollision::IMPASSABLE_COLOR)
end
end
return out
end
end #DataManager
#==============================================================================
# Dir4
#==============================================================================
module Dir4
#--------------------------------------------------------------------------
# overwrite method: passable_tile?
#--------------------------------------------------------------------------
def self.dir_to_vec(d)
d -= 1
dx = (d % 3) - 1
dy = 1 - (d / 3)
return [dx, dy]
end
end #Dir4
#==============================================================================
# Game_CharacterBase
#==============================================================================
class Game_CharacterBase
PIXEL_STEP = TIM::TilesetPixelCollision::PIXEL_STEP
#--------------------------------------------------------------------------
# overwrite method: passable_tile? -> clearly performance heavy
#--------------------------------------------------------------------------
def passable_tile?(x, y, d)
p = Dir4.dir_to_vec(d)
l = []
x2 = x + (1 - self.bw) / 2.0 #actual x corner of hitbox
y2 = y + (1 - self.bh) / 2.0 #actual y corner of hitbox
if p[0] > 0 #going right
p[0] = 0 #fix to only check pixels right after
steps = (self.bh * (32 / PIXEL_STEP)).to_i
sx = x2 + self.bw
l = 0.upto(steps - 1).map{|i| [sx, y2 + i * PIXEL_STEP / 32.0]}
elsif p[0] < 0 #going left
steps = (self.bh * (32 / PIXEL_STEP)).to_i
l = 0.upto(steps - 1).map{|i| [x2, y2 + i * PIXEL_STEP / 32.0]}
end
if p[1] > 0 #going down
p[1] = 0 #fix to only check pixels right after
steps = (self.bw * (32 / PIXEL_STEP)).to_i
sy = y2 + self.bh
l += 0.upto(steps - 1).map{|i| [x2 + i * PIXEL_STEP / 32.0, sy]}
elsif p[1] < 0 #goind up
steps = (self.bw * (32 / PIXEL_STEP)).to_i
l += 0.upto(steps - 1).map{|i| [x2 + i * PIXEL_STEP / 32.0, y2]}
end
#you must check all listed pixels
l.all?{|pos| sub_pix_passable?(*pos, p, d)}
end
#--------------------------------------------------------------------------
# new method: sub_pix_passable? -> perform passability check for position x,y
# in float, p is the added coordinate based on d
#--------------------------------------------------------------------------
def sub_pix_passable?(x, y, p, d)
px = (x * 32).to_i + p[0] * PIXEL_STEP
py = (y * 32).to_i + p[1] * PIXEL_STEP
nx = $game_map.round_x(px / 32.0)
ny = $game_map.round_y(py / 32.0)
x = $game_map.round_x(x)
y = $game_map.round_y(y)
return false unless $game_map.valid?(nx, ny)
return true if @through || debug_through?
return false unless pixel_map_passable?(x, y, d)
return false unless pixel_map_passable?(nx, ny, reverse_dir(d))
return true
end
#--------------------------------------------------------------------------
# new method: pixel_map_passable? -> check if pixels at x,y (floats)
# can be crossed
#--------------------------------------------------------------------------
def pixel_map_passable?(x, y, d)
px = (x * 32).to_i % 32
py = (y * 32).to_i % 32
$game_map.tile_pixel_passable?(x.to_i, y.to_i, d, px, py)
end
end #Game_CharacterBase
#==============================================================================
# Game_Player
#==============================================================================
class Game_Player
#--------------------------------------------------------------------------
# override method: pixel_map_passable? -> use the original method for vehicles
#--------------------------------------------------------------------------
def pixel_map_passable?(x, y, d)
case @vehicle_type
when :boat, :ship, :airship
map_passable?(x.to_i, y.to_i, d)
else
super
end
end
end #Game_Player
#==============================================================================
# RPG::Tileset
#==============================================================================
class RPG::Tileset
#--------------------------------------------------------------------------
# new method: page_pixel_passable?
#--------------------------------------------------------------------------
def page_pixel_passable?(page_id, subtile_id, px, py)
tilename = TIM::TilesetPixelCollision.collide_name(tileset_names[page_id])
data = Cache.tileset(tilename)
return true unless data
x = subtile_x(subtile_id) + px
y = subtile_y(subtile_id) + py
c = data.get_pixel(x,y)
TIM::TilesetPixelCollision.passable?(c)
end
#--------------------------------------------------------------------------
# new method: subtile_x
#--------------------------------------------------------------------------
def subtile_x(subtile_id)
tid = subtile_id % 128 #get one side
x = (tid % 8) * 32
x += (subtile_id / 128) * 8 * 32
end
#--------------------------------------------------------------------------
# new method: subtile_y
#--------------------------------------------------------------------------
def subtile_y(subtile_id)
tid = subtile_id % 128 #get one side
y = (tid / 8) * 32
end
#--------------------------------------------------------------------------
# new method: pixel_passable?
#--------------------------------------------------------------------------
def pixel_passable?(tile_id, px, py)
return false if tile_id >= 1024
page_pixel_passable?(5 + tile_id / 256, tile_id % 256, px, py)
end
end #RPG::Tileset
#==============================================================================
# Game_Map
#==============================================================================
class Game_Map
#--------------------------------------------------------------------------
# new method: tile_pixel_passable?
#--------------------------------------------------------------------------
#x, y are in map coordinates, d is a direction, px, py are pixel accuracy
def tile_pixel_passable?(x, y, d, px, py)
bit = (1 << (d / 2 - 1)) & 0x0f
all_tiles(x, y).each do |tid|
flag = tileset.flags[tid]
next if flag & 0x10 != 0 # [☆]: No effect on passage
return true if flag & bit == 0 # [○] : Passable
return false if flag & bit == bit && !tileset.pixel_passable?(tid, px, py)
end
return false # Impassable
end
end #Game_Map