melonjs / melonJS

a fresh, modern & lightweight HTML5 game engine
https://melonjs.org
MIT License
5.94k stars 643 forks source link

Line of Sight (Ray Tracer) #285

Closed aaschmitz closed 6 years ago

aaschmitz commented 11 years ago

Given a point x and a point y in the map, we can see if it has a direct view between them without colliding with other objects or solid tiles.

Something like this article:

http://www.codeproject.com/Articles/15604/Ray-casting-in-a-2D-tile-based-environment

This is a fine complement to be used in conjunction with the new collision system #103 ;)

Thanks!

obiot commented 11 years ago

Thank you very much for the link !

It’s effectively a nice feature to add, however as part of the next 1.0.0 release we do however plan to rewrite the whole collision mechanism using a SAT algorithm, and to use polygon shape object to design the collision layer, which means that we won’t have anymore the current tile based collision as use in this same article.

Which mean raytracing can be added but with a slight different technique :)

aaschmitz commented 11 years ago

Hi Olivier

Some related algorithms:

http://www.redblobgames.com/articles/visibility/

http://blogs.msdn.com/b/ericlippert/archive/2011/12/12/shadowcasting-in-c-part-one.aspx

Thanks!

parasyte commented 11 years ago

I did a lot of this work for the dynamic lighting in Alienauts : http://blipjoy.github.io/alienauts/public/index.html#debug Specifically, I used the vector methods provided by Chipmunk-js.

Anyway, I am going to need this functionality in Sprung Fever, which does not use Chipmunk-js. So I can't just piggyback off its vector math support.

I don't know if an API can be specified for these things without being too restrictive. Providing the math functions and allowing experienced developers to make use of them is the best approach I know of, as far as interface.

agmcleod commented 11 years ago

http://deepnight.net/bresenham-magic-raycasting-line-of-sight-pathfinding/?utm_content=buffer01e81&utm_source=buffer&utm_medium=twitter&utm_campaign=Buffer

hope that helps, i follow the guy on twitter: https://twitter.com/deepnightfr he's won a couple LD's and such :)

aaschmitz commented 10 years ago

Hi @parasyte

The idea is implement a "generic" function with check for object collide between Point A and Point B (returning a array of objects maybe). It is up to the developer to implement a logic to see if the single object that collided is Point B, which would mean a direct target.

In you game Sprung Fever, if one of the girls is on the other side of the wall, the indicator increases, ie you only checks the distance between the player and enemies, ignoring obstacles between them ;)

parasyte commented 10 years ago

I wrote a bresenham plugin for my LD48 game, live on Twitch. :confounded: https://github.com/blipjoy/kungfustar/blob/gh-pages/public/lib/plugins/melonham.js The video shows how I used it. Not the way I imagined in the final game (I'm not going to have time for the rope collisions with the world. Maybe after compo)

obiot commented 10 years ago

Nice, and I love the name of the plugin :)

aaschmitz commented 10 years ago

@parasyte Nice plugin, works well for trace collisions with Tiled Collision Layer. But if instead of using the Tiled Collision Layer I need to use Tiled objects, how to check? Or how to get which ObjectEntity is in a certain coordinate of the Tiled map (i.e detect dynamic entities that is not defined in the collision layer)?

aaschmitz commented 10 years ago

http://gamemechanicexplorer.com/ex/raycasting/1

obiot commented 10 years ago

i guess this one should become easier to implement with the improvements in 1.1.0 (SAT/Quadtree) ?

parasyte commented 10 years ago

Well, it's still going to be kind of tricky. Assuming your shapes have a minimum width/height (like 10px, for example) you can use that as the step size for the bresenham line generator. That way you're not checking for collisions on every pixel (slow!) but you can still use the polygon hit detection and response vector from SAT to get pixel-perfect collisions along any straight line segment.

obiot commented 10 years ago

Hi Guys,

What do we do with this one now ? I mean that we show how this is possible using the new collision system, what shall we discuss here ? Add this as a "standard feature" in the engine ?

aaschmitz commented 10 years ago

Add it as a "standard feature" in the engine :+1:

parasyte commented 10 years ago

It's good to have, but this is how feature creep sets in. Make it a module add on! #212

aaschmitz commented 10 years ago

For tracking with a working example: http://aaschmitz.github.io/melonjs-improved-platformer/

@parasyte Yes, increase the melonJS file size. But, it's a nice feature, and the more features you have, the more complete and distinct is the engine, helping developers :)

parasyte commented 10 years ago

I agree in part. The point of the modular approach is to provide that completeness without sacrificing usability.

It's like a Swiss Army Knife compared to a Switch Modular Knife; The Swiss Army Knife has fixed utilities, and some of them are big and bulky. The Switch Modular Knife can be resized to fit your needs, and the attachments are interchangeable and configurable; you can even make it big and bulky if that's what you really want.

obiot commented 6 years ago

this one was basically coming for free after we added Quadtree + SAT collision implementation in melonJS, not sure why we did not do it back then, except probably because we have too many tickets, and not enough people closing them :P :P :P