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

Buggy collision detection when ConvexPolygonShape2D is given a concave polygon with set_points() #8611

Closed
efirdc opened this issue May 1, 2017 · 8 comments

Comments

@efirdc
Copy link

efirdc commented May 1, 2017

Operating system or device - Godot version:
Windows 7 - Godot 2.1.3 win64

Issue description:
I have a Polygon2D, and I am trying to create a static rigidbody with the polygons shape through code:

extends Polygon2D
func _ready():
	
	var shape = ConvexPolygonShape2D.new()
	shape.set_points(get_polygon())
	
	var body = RigidBody2D.new()
	body.set_mode(RigidBody2D.MODE_STATIC)
	body.add_shape(shape)
	add_child(body)

This works fine if the polygon is actually convex, but for concave polygons the collision detection breaks:

https://gfycat.com/WideNaiveDwarfrabbit

The left and right polygons are polygon2D's with the above script. The bottom polygon was created in the editor and has the expected behavior. It looks like ConvexPolygonShape2D doesn't decompose concave polygons when using set_points(). Is there a class that can do this?

Link to minimal example project:
Example project from the gif: https://drive.google.com/open?id=0B_tU7BWC-5CkY0JGNWpuU1ZVWFk

Drive link because:
image
When I drop in a zip.

@ret80
Copy link

ret80 commented May 3, 2017

@reduz
Copy link
Member

reduz commented May 3, 2017 via email

@efirdc
Copy link
Author

efirdc commented May 3, 2017

@ret80 Aware of this. The docs say ConcavePolygonShape2D's are not recommended for rigidbodies. Right now it seems like it is only possible to use the concave polygon decomposer by creating polygons in the editor. I think it would be good if it could be accessed through scripting as well.

@ret80
Copy link

ret80 commented May 4, 2017

@Smellyhobo101

func _ready():
	var polygon = get_polygon()
	var shape = ConcavePolygonShape2D.new()
	
	var segments = Vector2Array()
	segments.resize(polygon.size() * 2);

	for i in range(polygon.size()):
		segments[(i << 1) + 0] = polygon[i]
		segments[(i << 1) + 1] = polygon[(i + 1) % polygon.size()]
	
	shape.set_segments(segments)
	var body = RigidBody2D.new()
	body.set_mode(RigidBody2D.MODE_STATIC)
	body.add_shape(shape)
	add_child(body)

@efirdc
Copy link
Author

efirdc commented May 4, 2017

@ret80
I know how to make concave polygons. I want to decompose a concave polygon into multiple convex polygons.

https://github.com/godotengine/godot/blob/de7eba887e9fe940dac0958836fa8fb778628d2a/drivers/convex_decomp/b2d_decompose.cpp

@ret80
Copy link

ret80 commented May 4, 2017

@Smellyhobo101 As I understand it, the problem was connected with the concave polygon created in the script. If so, the above code solves this problem. If you wanted to break a concave polygon into convex then it's likely your link fits better.

The docs say ConcavePolygonShape2D's are not recommended for rigidbodies.

If you have static objects and there are a few of them that can be used and do not worry

@efirdc
Copy link
Author

efirdc commented May 4, 2017

@ret80

That's what I've been doing for large static polygons. The problem I was trying to bring attention to is that the function to decompose concave polygons into convex polygons is not exposed to gdscript, and can only be accessed with a CollisionPolygon2D in the editor. In cases where you need to make small dynamic rigidbodies from code with concave bits it would be nice to have.

@ret80
Copy link

ret80 commented May 4, 2017

@Smellyhobo101 I support. Only this is not a bug but an improvement

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

No branches or pull requests

5 participants