Coder Social home page Coder Social logo

selimanac / defold-tile-raycast Goto Github PK

View Code? Open in Web Editor NEW
11.0 1.0 0.0 862 KB

Ray Casting in tiled worlds using DDA algorithm.

License: MIT License

Lua 74.05% C++ 25.95%
defold defold-engine defold-game-engine defold-library defold-native-extension game-development gamedev

defold-tile-raycast's Introduction

Tile Raycast

Tile Raycast

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

Installation

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

Example

https://github.com/selimanac/defold-tile-raycast-platformer

API

raycast.init (tile_width, tile_height, tilemap_width, tilemap_height, tiles, target_tiles)

Initial setup for raycast.

PARAMETERS

  • tile_width (int) - Single tile width
  • tile_height (int) - Single tile height
  • tilemap_width (int) - Tilemap width
  • tilemap_height (int) - Tilemap height
  • tiles (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)

raycast.cast (ray_from, ray_to)

Perform raycast on tilemap. Returns only first successful hit.

PARAMETERS

  • ray_from (Vector3) - Start position of ray
  • ray_to (Vector3) - End position of ray

RETURN

  • hit (boolean) - If hit or not
  • tile_x (int) - ile x position
  • tile_y (int) - Tile y position
  • array_id (int) - ID of the array for Tilemap array
  • tile_id (int) - ID of the tile from tilesource for Tilemap
  • intersection_x (number) - Ray intersection point x
  • intersection_y (number) - Ray intersection point y
  • side (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

raycast.reset ()

Clear all tile and timemap data.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.