filbs111 / box2d-learning

0 stars 0 forks source link

collision with arbitrary/convex level polygons #12

Closed filbs111 closed 7 years ago

filbs111 commented 7 years ago

box2d body fixtures can be convex polygons. concave polygons can be made using multiple convex fixture polygons, but this should be done separately from box2d and fed in.

libraries exist to create polygons using boolean operations - basically 2D CSG. eg https://sourceforge.net/projects/jsclipper/

can use this in level creation, and when cutting explosion shapes out (see #11). the output is a general polygon, which should be processed into a) something box2d can use for collision and b) something suitable for rendering (AFAIK polygons without holes for canvas, triangles for webgl)

decomposition into convex polygons can be done via https://github.com/schteppe/poly-decomp.js triangulation can be done using https://github.com/mapbox/earcut , though this is trivial if going from convex polygons.

box2d can use for static collision i) triangles ii) edges (1 fixture per edge AFAIK) iii) convex polygons.

anecdotally, dynamic collision won't work with ii), and iii) should be more performant than i)

eventually, might wish to edit levels as triangles (eg using 3d modelling software), in which case would want some way to get from triangles to polygon data, but expect should be relatively trivial to do this - basically join up unshared edges.

plan: make/find some typical level test data. implement and compare i), ii), iii) i expect iii) to be fastest, but might be buggy.

filbs111 commented 7 years ago

doesn't seem to be an issue with limited number of verts in a poly body (see test ellipse). also works for a dynamic body ok. sets of edge fixtures work ok.

filbs111 commented 7 years ago

done, just with edges. maybe worth updating to a newer box2d version so can use edge paths (rather than a series of lines, which bodies can "trip up" on) edges gives emergent gameplay- if bomb builds landscape, can use it to pop inside it!