Godot Version
4.3
Question
Runtime calculated CollisionPolygon2D breaks RigidBody2D?
I have a RigidBody2D with a 32x32 sprite, I calculate collisions on _ready() using the BitMap method “opaque_to_polygons”, the body then proceeds to move by its own as soon as it collides with a StaticBody (ground), accelerating and decelerating in random ways.
It moves the same way every time the scene is played
extends RigidBody2D
@onready var image = $Sprite2D.texture.get_image()
var width := 32
var height := 32
func _ready():
gen_polygon()
func gen_polygon():
for child in get_children():
if child.get_class() == "CollisionPolygon2D":
child.queue_free()
var bitmap = BitMap.new()
bitmap.create_from_image_alpha(image)
var polys = bitmap.opaque_to_polygons(Rect2(Vector2(), $Sprite2D.texture.get_size()), 0.6)
for poly in polys:
var collision_polygon = CollisionPolygon2D.new()
collision_polygon.polygon = poly
add_child(collision_polygon)
collision_polygon.position = -bitmap.get_size()/2
How to reproduce:
Create RigidBody2D node, give it a Sprite2D and use the same script as the one above.
Settings of the RigidBody are default ones
Also, removing the script does remove the unexpected movement