melonjs / melonJS

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

How to get collision map grid? #993

Closed sameernyaupane closed 4 years ago

sameernyaupane commented 4 years ago

melonJS version

I'm trying to implement A* algorithm for path finding for isometric grid. I'm using this example as base: http://melonjs.github.io/melonJS/examples/isometric_rpg/

I checked the code for a plugin I found ( https://github.com/melonjs/plugins/blob/master/pathfinding/melonjs-astar/melonjs-astar.js#L323 ), and it seems to depend on this attribute: me.game.collisionMap

which does not seem to exist on v7.1.1.

How would I go about generating a grid like below for my isometric world? Using AStar code from: https://github.com/bgrins/javascript-astar

var graph = new Graph([ [1,1,1,1], [0,1,1,0], [0,0,1,1] ], { diagonal: true }); var start = graph.grid[0][0]; var end = graph.grid[1][2]; var result = astar.search(graph, start, end);

Any guidance or help would be appreciated, thanks :)

obiot commented 4 years ago

the collisionMap was an early collision implementation from melonJS early days, since then (version 3.x or something) we replaced that with a SAT based collision detection and a quad tree.

To partition the collision layer into a grid, you should search the QuadTree using the grid cell boundaries as the lookup rectangle; Iterate over an imaginary grid with nested for-loops, setting the pos.x, pos.y, width and height properties of a rectangle to the boundary of each cell as you iterate. Pass that rectangle to me.collision.quadTree.retrieve() and it will return all shapes that intersect with that cell. You can then determine if any of the shapes are solid by inspecting its properties.

If you are using collision shapes aligned to the tile grid, create your imaginary grid using the same size as the tiles. But for arbitrary collision shapes, a smaller grid will provide better granularity at the cost of more CPU time to compute a path.

Other way is to use the Line of Sight / raycast function : http://melonjs.github.io/melonJS/docs/me.collision.html#.rayCast live example here : https://melonjs.github.io/melonJS/examples/lineofsight/

(Closing this one as this is not a bug report)