Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Box2D: Recalculate collision polygon vertices inside tiles #212

Merged
merged 1 commit into from
Apr 9, 2019

Conversation

ane
Copy link
Contributor

@ane ane commented Apr 6, 2019

The polygons were being rendered with their original coordinates, this moves them to the relative location of the tile being drawn.

@karai17
Copy link
Owner

karai17 commented Apr 6, 2019

Can you show me a before and after screenshot?

@ane
Copy link
Contributor Author

ane commented Apr 6, 2019

Right. So here is a map with six tiles on a base tileset:

https://i.imgur.com/gC6gXgr.png

The dirt patch on the left has a rectangle object, the white circle, a collider body, is hanging on this. Next to the dirt patch is a brown "hole" with an ellipse object inside. On the upper right is an object layer polygon that is also collidable, which is automatically drawn by Box2D itself. The four evenly spaced tiles on the middle contain polygons that trace the non-grass parts of it.

This is the same thing but calling box2d_draw on the map:

https://i.imgur.com/X9yr5EK.png

The collision shapes are defined correctly for the ellipse and the rectangle, but not for the polygons. The top left contains some weird polygons that aren't supposed to be there. The polygons are defined on the four right-most tiles like this:

https://i.imgur.com/9iLAREQ.png

After applying the fix, the polygons are correctly placed upon the tiles:

https://i.imgur.com/dVpGcwF.png

The root cause, as it appeared to me, was that tile object ellipses and rectangles are drawn by the dx and dy coordinates of the tile objects, which are automatically calculated first here and then here. Later, o.x and o.y are used to draw the rectangle and ellipse tile objects.

On the other hand, the polygon inside a tile object is not drawn based on these coordinates, but is based on the vertices of the polygon itself directly, not the dx or dy values. While the dx and dy are correctly calculated for the polygon itself but not for the polygon vertices.

Going deeper, we find that for object layer polygons the vertex coordinates are adjusted during load here. This same calculation — or any calculation — is not applied to the tile objects. This is fine for rectangles and ellipses since those do not need recalculation, but for polygons this is necessary, since they retain the original "offset' coordinates of the tile object.

@karai17 karai17 merged commit 2ba879d into karai17:master Apr 9, 2019
@karai17
Copy link
Owner

karai17 commented Apr 9, 2019

Awesome, thanks for catching this. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants