game screen shot1
Godot Version
v4.3.stable.official
Question
I tried to make ‘Make collisionPolygon2d fit sprite to pixel perfectly and automaticaly’
ref : Bite-Sized Godot: Pixel-perfect collision polygons on sprites - The Shaggy Dev
https://www.youtube.com/watch?v=Btk8IzhvaDo
I think those ref source are old version so i should’ve fix some lines.
(ex: collsion_polygon.position -= bitmap…)
I’m not sure because that but collsion movement is weird
the problems are
smaller collision what i expect
- if i let sprite scale up(1 → 4) collsion doesn’t sizing up
with this line : bitmap.create_from_image_alpha(self.texture.get_image())
also i tried var polys = bitmap.opaque_to_polygons(Rect2(Vector2.ZERO, self.texture.get_size() *** 4**), 0.5)
game screen shot2 item stand up
- created smaller collision automaticaly from this code(what i don’t expect but made done)
even gravity movement so weird, if i drop those item it always stand up.
original node path
ball(rigidbody2d)
|-ball_sprite2d(sprite2d)
|-Area2D(area2d)
scripted or instaced path(after script run)
ball(rigidbody2d)
|-@collisionPolygon2d(same shape)
|-ball_sprite2d(sprite2d)
|-Area2D(area2d)
|-@collisionPolygon2d(same shape)
ball_sprite2d.gd
extends Sprite2D
@onready var rigid = get_parent()
@onready var area = get_parent().get_node("Area2D")
var parent_level
var texture_source
var img_path
func make_collision2d() -> void:
area.connect('mouse_entered', Callable(self, '_on_mouse_entered'))
area.connect('mouse_exited', Callable(self, '_on_mouse_exited'))
var bitmap = BitMap.new()
bitmap.create_from_image_alpha(self.texture.get_image())
var polys = bitmap.opaque_to_polygons(Rect2(Vector2.ZERO, self.texture.get_size()), 0.5)
for poly in polys:
var collision_polygon1 = CollisionPolygon2D.new()
var collision_polygon2 = CollisionPolygon2D.new()
collision_polygon1.polygon = poly
collision_polygon2.polygon = poly
rigid.add_child(collision_polygon1)
area.add_child(collision_polygon2)
# Generated polygon will not take into account the half-width and half-height offset
# of the image when "centered" is on. So move it backwards by this amount so it lines up.
if centered:
#collision_polygon1.position = collision_polygon1.position - bitmap.get_size()/2
collision_polygon1.position.x = collision_polygon1.position.x - bitmap.get_size().x/2
collision_polygon1.position.y = collision_polygon1.position.y - bitmap.get_size().y/2
collision_polygon2.position.x = collision_polygon2.position.x - bitmap.get_size().x/2
collision_polygon2.position.y = collision_polygon2.position.y - bitmap.get_size().y/2
func _on_mouse_entered() -> void:
modulate = Color.RED
func _on_mouse_exited() -> void:
modulate = Color.WHITE
func _on_ball_ball_parent_done(parent_level) -> void:
self.parent_level = parent_level
print(parent_level)
img_path = "res://assets/img/relics_img/"+ str(parent_level) +".png"
texture_source = load(img_path)
self.texture = texture_source
self.scale = Vector2(4, 4)
self.position = Vector2(0, 0)
self.centered = true
make_collision2d()