Coder Social home page Coder Social logo

legends2k / 2d-fov Goto Github PK

View Code? Open in Web Editor NEW
67.0 4.0 9.0 64 KB

Field of View and Line of Sight in 2D

Home Page: http://legends2k.github.io/2d-fov

License: BSD 3-Clause "New" or "Revised" License

HTML 100.00%
sight artificial-intelligence game-ai 2d-game 2d-lighting vision collision-detection linear-algebra field-of-view

2d-fov's Introduction

Field of View and Line of Sight in 2D

View it in action in your browser!

The idea is to find the field of view from a given point in a 2D world which has buildings. Many resources on the web doing 360° FoV exists; these have their FoV's distance and angle unbound. This is simpler to design; arriving at a fast implementation is easier since -- if rightly done -- there would be no intersection queries every iteration of the loop to eat up performance. However, the FoV here is different; it's limited both by angle and distance. Care is taken to have an optimised solution that avoids unnecessary intersection tests so that it's usable in a game where multiple agents would need their FoV computed every frame.

This implementation is done in HTML5 Canvas/JavaScript for easy viewing but the idea is mathematical and can be implemented in any language. The only library it uses is glMatrix for vector calculations. The code is heavily commented and includes references to articles and books for someone to learn from it.

The design document has all the gory details, again with references for further study.

2d-fov's People

Contributors

legends2k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

2d-fov's Issues

incorrect hit point intersection, when segment.center x is the same as polygon edge

Hi! I really liked your article and the source codes for that 2d-fov. I've found a bug in your code, I spend 2 days finding and fix, but I can't find a solution. Please help.
A test scene is:

scene = {
         polygons: [
             { coords: [ 96,72,120,72,120,96,96,96 ],
               colour: "128, 128, 128",
               stroke: 1,
               fill: true },

        ],
         observer: { loc: vec2.fromValues(50, 96),
                     dir: vec2.fromValues(-0.707106781186, 0.707106781186),
                     colour: observerColour },
         target: { loc: vec2.fromValues(500, 100),
                   dir: vec2.fromValues(-1, 0),
                   colour: targetColour }
     };

As you see, fov calculation is incorrect

Any field of view greater than 180° fails to render properly

Not sure if you're actually interested in this anymore, but thought I'd bring it up just in case. I'm actually trying to do something similar to what you already have, but with a full 360° range. I could use your code directly and just run it twice for those cases where the total FoV is greater than 180° (splitting the range between the two runs) and union the polygons, but that's likely way more inefficient than it could be with a real solution.

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.