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

Unable to set margin on CollisionPolygon resulting in noticeable gaps when using Bullet engine #45718

Closed
RobTheFiveNine opened this issue Feb 4, 2021 · 5 comments · Fixed by #45855

Comments

@RobTheFiveNine
Copy link
Contributor

RobTheFiveNine commented Feb 4, 2021

Godot version:
v3.2.3.stable.official

OS/device including version:
Ubuntu 18.04

Issue description:
When a RigidBody with a CollisionShape collides with a StaticBody that contains a CollisionPolygon, a noticeable gap can be seen which seems to be the default margin that is applied. Unlike other collision objects, it's not possible to set the margin of the CollisionPolygon though.

A potential work around was mentioned in This Similar Issue, but I was unable to get it to work.

An example of the gap can be seen in this screenshot:

Screenshot from 2021-02-04 22-46-34

Minimal reproduction project:
https://github.com/RobTheFiveNine/godot-collision-polygon-margin-example

@hoontee
Copy link
Contributor

hoontee commented Feb 9, 2021

Author of the workaround here. Are you sure this code doesn't work?:

func _ready() -> void:
	fix_collision_polygons($"/root/Game/World") # Your nodes to fix here

func fix_collision_polygons(parent: Node) -> void:
	for child in parent.get_children():
		if parent is PhysicsBody and child is CollisionPolygon and not child.disabled:
			var body := (parent as CollisionObject).get_rid()
			for i in PhysicsServer.body_get_shape_count(body):
				var shape := PhysicsServer.body_get_shape(body, i)
				if PhysicsServer.shape_get_type(shape) == PhysicsServer.SHAPE_CONVEX_POLYGON:
					var new_convex_polygon_shape := ConvexPolygonShape.new()
					new_convex_polygon_shape.margin = 0
					new_convex_polygon_shape.set_points(PhysicsServer.shape_get_data(shape))
					var new_collision_shape := CollisionShape.new()
					new_collision_shape.shape = new_convex_polygon_shape
					new_collision_shape.transform = child.transform
					parent.add_child(new_collision_shape)
			child.free()
		else:
			fix_collision_polygons(child)

@RobTheFiveNine
Copy link
Contributor Author

Author of the workaround here. Are you sure this code doesn't work?:

func _ready() -> void:
	fix_collision_polygons($"/root/Game/World") # Your nodes to fix here

func fix_collision_polygons(parent: Node) -> void:
	for child in parent.get_children():
		if parent is PhysicsBody and child is CollisionPolygon and not child.disabled:
			var body := (parent as CollisionObject).get_rid()
			for i in PhysicsServer.body_get_shape_count(body):
				var shape := PhysicsServer.body_get_shape(body, i)
				if PhysicsServer.shape_get_type(shape) == PhysicsServer.SHAPE_CONVEX_POLYGON:
					var new_convex_polygon_shape := ConvexPolygonShape.new()
					new_convex_polygon_shape.margin = 0
					new_convex_polygon_shape.set_points(PhysicsServer.shape_get_data(shape))
					var new_collision_shape := CollisionShape.new()
					new_collision_shape.shape = new_convex_polygon_shape
					new_collision_shape.transform = child.transform
					parent.add_child(new_collision_shape)
			child.free()
		else:
			fix_collision_polygons(child)

I must have made a typo somewhere when trying it last time, because I can't break it this time! Above example and the one from the original post seems to work

@hoontee
Copy link
Contributor

hoontee commented Feb 9, 2021

Glad we could get it to work. I do a lot of CSG / collision workarounds like this for my projects so let me know if you need any more help.

@RobTheFiveNine
Copy link
Contributor Author

Ahhh OK, this issue can probably be closed then and just added as a note to #27427 then. I didn't realise CollisionPolygons get turned into convex shapes under the hood.

Thanks for the fix too! I ended up scaling everything up in the project I ran into it with, which has actually helped fix some lighting issues too, but I'll definitely keep this solution bookmarked for future use 😃

@hoontee
Copy link
Contributor

hoontee commented Feb 9, 2021

I'm sorry, I take it back entirely - adding a margin property and setting it to zero will still have an effect if #27427 isn't fixed first...

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

Successfully merging a pull request may close this issue.

4 participants