melonJS icon indicating copy to clipboard operation
melonJS copied to clipboard

Line of Sight (Ray Tracer)

Open aaschmitz opened this issue 13 years ago • 17 comments

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!

aaschmitz avatar Oct 09 '13 02:10 aaschmitz

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 :)

obiot avatar Oct 14 '13 05:10 obiot

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!

aaschmitz avatar Oct 16 '13 02:10 aaschmitz

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.

parasyte avatar Nov 20 '13 22:11 parasyte

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 :)

agmcleod avatar Nov 20 '13 22:11 agmcleod

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 ;)

aaschmitz avatar Dec 08 '13 14:12 aaschmitz

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)

parasyte avatar Dec 15 '13 18:12 parasyte

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

obiot avatar Dec 16 '13 14:12 obiot

@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 avatar Mar 11 '14 16:03 aaschmitz

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

aaschmitz avatar Apr 28 '14 18:04 aaschmitz

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

obiot avatar Jul 23 '14 10:07 obiot

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.

parasyte avatar Oct 15 '14 00:10 parasyte

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 ?

obiot avatar Nov 21 '14 07:11 obiot

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

aaschmitz avatar Nov 21 '14 10:11 aaschmitz

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

parasyte avatar Nov 21 '14 11:11 parasyte

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 :)

aaschmitz avatar Nov 21 '14 21:11 aaschmitz

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.

parasyte avatar Nov 21 '14 23:11 parasyte

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

obiot avatar Jan 05 '18 04:01 obiot