Ray Casting in tiled worlds using DDA algorithm. It is very effective solution for tile based worlds like platformers or any top-down games.
This implementation based on https://lodev.org/cgtutor/raycasting.html and https://www.youtube.com/watch?v=NbSee-XM7WA
You can use Tiled Ray Cast in your own project by adding this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:
https://github.com/selimanac/defold-tile-raycast/archive/refs/heads/master.zip
https://github.com/selimanac/defold-tile-raycast-platformer
Initial setup for raycast.
PARAMETERS
tile_width
(int) - Single tile widthtile_height
(int) - Single tile heighttilemap_width
(int) - Tilemap widthtilemap_height
(int) - Tilemap heighttiles
(table) - Single dimensional tiles table generated from your tilemap (or from your source).target_tiles
(table) - Not passible tile IDs from your tilesource (walls, grounds etc...).
EXAMPLE
local tiles = {
4,2,2,2,0,2,2,2,2,2,
2,2,2,2,0,2,2,2,2,2,
2,2,2,2,0,2,2,2,2,2,
2,2,1,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,2,2,
1,2,1,2,2,2,1,1,1,1,
1,1,1,2,2,2,1,1,1,1,
1,1,1,2,1,2,1,3,1,1,
1,1,1,1,2,1,1,1,4,1,
1,1,1,2,2,2,2,1,1,3,
1,1,1,1,1,1,1,1,1,1
}
local tile_width = 32
local tile_height = 32
local target_tiles = {2, 3, 4}
local tilemap_width = 10
local tilemap_height = 11
raycast.init(tile_width, tile_height, tilemap_width, tilemap_height, tiles, target_tiles)
Perform raycast on tilemap. Returns only first successful hit.
PARAMETERS
ray_from
(Vector3) - Start position of rayray_to
(Vector3) - End position of ray
RETURN
hit
(boolean) - If hit or nottile_x
(int) - ile x positiontile_y
(int) - Tile y positionarray_id
(int) - ID of the array for Tilemap arraytile_id
(int) - ID of the tile from tilesource for Tilemapintersection_x
(number) - Ray intersection point xintersection_y
(number) - Ray intersection point yside
(int) - Which side hit. 0 for LEFT-RIGHT, 1 for TOP-BOTTOM
EXAMPLE
local ray_from = go.get_position(ray_start_url)
local ray_to = go.get_position(ray_end_url)
local hit, tile_x, tile_y, array_id, tile_id, intersection_x, intersection_y, side = raycast.cast(ray_from, ray_to)
if hit then
print("tile_x: " .. tile_x)
print("tile_y: " .. tile_y)
print("array_id " .. array_id)
print("tile_id " .. tile_id)
print("intersection_x " .. intersection_x)
print("intersection_y " .. intersection_y)
print("Side " .. side) -- 0 for LEFT-RIGHT, 1 for TOP-BOTTOM
else
print("No result found")
end
Clear all tile and timemap data.