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

Add one-way collision to tile-set/tile-map (2.1) #8558

Merged

Conversation

RandomShaper
Copy link
Member

Now you can set one_way_collision_direction/_max_depth on a tile's static bodies. During conversion to a TileSet it will be checked whether all static bodies for a given tile have the same values for those. If that's not met, a warning will be printed and the relevant tile will be converted with one-way collision disabled (all zeroes for those properties).

At runtime multiple physics bodies will be created for TileMap's quadrants as needed. That is, one physics body per quadrant per different set of one-way collision parameters. I've optimized the code as much as possible to recycle physics bodies so to avoid as much creation/freeing as possible at runtime in case tiles are changed dynamically.

This deserves some testing. I will be testing it heavily myself.

Not sure what to do about master. @reduz said in 3.0 those properties would be probably added to the collision shapes instead, so this implementation would be meaningless for it.

Closes #2585 (but only for 2.1).

@reduz
Copy link
Member

reduz commented Apr 27, 2017 via email

@RandomShaper RandomShaper force-pushed the tilemap-one-way-collision-2.1 branch from 31e42c6 to 7b5b63f Compare April 27, 2017 18:20
@RandomShaper
Copy link
Member Author

RandomShaper commented Apr 27, 2017

Fixed compilation error (I was using static to force internal linkage for an ad hoc struct; MSVC happily accepted that, but that's not standard so using an anonymous unnamed namespace now instead).

Clang also seems to like it; only GCC complains, but I can confirm that such allowance is an extension.

Just telling this in case someone is curious.

@RandomShaper RandomShaper force-pushed the tilemap-one-way-collision-2.1 branch from 7b5b63f to 74d2e12 Compare April 27, 2017 23:14
@akien-mga akien-mga added this to the 2.1 milestone May 2, 2017
@RandomShaper
Copy link
Member Author

In my tests this is working fine.

It may be a good time for merging so "early adopters" can start testing it easily.

@pixelpicosean
Copy link
Contributor

@RandomShaper I found that the converted tilesets may become invalid if no StaticBody exist as child of tiles, and the reason is one_way_collision_max_depth not properly initialized during Data construction.

Here's a simple solution for that:

struct Data {

	String name;
	Ref<Texture> texture;
	Vector2 offset;
	Vector2 shape_offset;
	Rect2i region;
	Vector<Ref<Shape2D> > shapes;
	Vector2 one_way_collision_direction;
	float one_way_collision_max_depth;
	Vector2 occluder_offset;
	Ref<OccluderPolygon2D> occluder;
	Vector2 navigation_polygon_offset;
	Ref<NavigationPolygon> navigation_polygon;
	Ref<CanvasItemMaterial> material;
	Color modulate;

	// Default modulate for back-compat
	explicit Data()
		: one_way_collision_max_depth(0.0f)
		, modulate(1, 1, 1) {}
};

@pixelpicosean
Copy link
Contributor

@akien-mga I've merged this PR into my own game and it works pretty great so far, and the only issue I've met is the converting operation with its solution posted above, you may considering merge it so developers will be able to use this great feature :D

@akien-mga
Copy link
Member

@pixelpicosean Thanks for the feedback, for such changes it's always best to have Godot users testing the PR before it's merged and report back. Will wait for @RandomShaper's answer regarding the problem you found and merge once it's addressed.

@RandomShaper
Copy link
Member Author

@pixelpicosean, thank you for having tried it out and reporting your experience! I'm really happy to know you find it useful.

I'll add the missing initializer and push.

@RandomShaper RandomShaper force-pushed the tilemap-one-way-collision-2.1 branch from 74d2e12 to 417b6c5 Compare June 11, 2017 19:14
@RandomShaper
Copy link
Member Author

Pushed!

By the way, I'm not sure if the way clang-format is formatting initializer lists is too convenient. As they aren't extensively used throughout Godot, maybe it could be changed to split them vertically. @akien-mga, what do you think?

@RandomShaper RandomShaper force-pushed the tilemap-one-way-collision-2.1 branch from 417b6c5 to 2018db0 Compare June 17, 2017 20:27
@akien-mga akien-mga merged commit c5a44ee into godotengine:2.1 Jun 25, 2017
@RandomShaper RandomShaper deleted the tilemap-one-way-collision-2.1 branch July 5, 2017 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants