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

Physics2D weird CollisionPolygon2D interaction with 3 consecutive points on the same line #38780

Open
Tracked by #45334
ChibiDenDen opened this issue May 16, 2020 · 1 comment

Comments

@ChibiDenDen
Copy link
Contributor

Godot version:
tested on 3.2.1 and 3.2.2 beta 2

OS/device including version:
tested on OSX

Issue description:
When having a CollisionPolygon2D with 3 consecutive points on the same line, interaction with rigid-bodies becomes unstable and janky.

When enabling collision shapes, the middle point on the collision polygon is visible although the colliding object is nowhere near it. (see image)
image

The most apparent effect is that applying torque in that direction will cause the object not to spin (most of the time), while applying torque in the other direction will behave normally

Steps to reproduce:
Create a floor from static body with CollisionBodyPolygon2D
Create the floor collision shape as a rectangle from a polygon using 5 points (1 extra point), for example:
PoolVector2Array( 64, 384, 554, 384, 832, 384, 832, 448, 64, 448 )

Create a box with controllable torque that lays on the floor, and try to apply torque in each direction, the box is expected to move, but sometimes it gets "stuck".
make sure to:

  • give enough torque (100 torque for 1 weight works for me)
  • disable "can sleep" on the box

Minimal reproduction project:
attached a minimal reproduction.

A and D keys apply torque to the box
deleting point 1 in the CollisionPolygon2D for floor fixes the issue
Archive.zip

@tom-jk
Copy link

tom-jk commented Jul 8, 2020

Can reproduce in 3.2.2.stable on Linux Mint 18.3.

It looks like it only affects rectangular polygons (including rotated ones, eg. a diamond shape, though they're more likely to be "rescued" by lack of precision). For sure, 3 collinear points is not an issue on polygons that deviate in any way from being a rectangle:

Godot38780_Screen2

left, rotated rectangle (points arranged in a rotated rectangle that is, the node is unrotated); right, the same rectangle with 2 more points, one moved out a little. The whole polygon is now properly convexified.


For those arriving here with this issue requiring a quick fix, a workaround that works in game is to add the following script (in the minimal project's case, on the Floor static body):

extends StaticBody2D

func _ready():
	for i in get_shape_owners():
		for j in shape_owner_get_shape_count(i):
			var shape = shape_owner_get_shape(i, j)
			if shape.is_class("ConvexPolygonShape2D"):
				shape.points = Geometry.convex_hull_2d(shape.points)

this will remove collinear vertices, and so the bug can't occur. This of course won't help if you still needed those vertices (if, say, you were planning to deform the polygon into other shapes).

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

4 participants