Skip to content

Approximate ray/beam casting O(N³) algorithm for 2D tilemaps

License

Notifications You must be signed in to change notification settings

Aivean/efficient-2d-raycasting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Efficient Raycasting for 2d Tilemaps

Java implemetation of the approximate ray/beam casting algorithm for a discrete 2D tilemaps.

WUT?

For a square 2D map that consists of tiles (NxN tiles), where each tile can be either empty, or contain light source or obstacle, this algorithm calculates approximate brightness of each tile, taking into account positions of the light sources and occlusion from the obstacles.

result.png

(image was generated with LightingTest)

See also:

Properties

If the input field is NxN tiles:

  • worst case O(N³) (for the number of light sources between N and N²)
  • best case is O(N²) (for constant number of light sources)
  • CPU cache friednly (all data structures are accessed sequentially)

Why?

Naive ray/shadowcasting implementation is O(N⁴) in worst case (O(N²) for each of O(N²) light sources). This algorithm is O(N³) worst case (and it really matters when N >= 100).

Performance

See benchmarks.

Building and Running

Maven is used for building. Java 8 is a prerequisite.

  • Build and run tests: ./mvnw clean install assembly:single
  • Run Lighting benchmarks after build: java -jar target/raycasting-2d-java-1.0-SNAPSHOT-jar-with-dependencies.jar

Project Structure

License

Copyright © 2018 Ivan Zaitsev (https://github.com/Aivean/efficient-2d-raycasting)

MIT licensed

About

Approximate ray/beam casting O(N³) algorithm for 2D tilemaps

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published