如何从精灵区域获取 CollisionPolygon2D
我正在尝试从 Sprite
创建的 CollisionPolygon2D
同级中获取 polygon
数据(以便我可以将其添加到我的自定义节点)代码:
但我遇到了多个问题:
如何通过代码
创建 CollisionPolygon2D 同级
?当我选择 Region
Enabled
作为 true 然后创建时,它会给出一个奇怪的偏移量,如下所示:
- 必须首先创建一个
CollisionPolygon2D
同级,它是多边形然后删除它很耗电,有没有办法可以直接获取多边形?
为了避免 XY 问题,请允许我解释一下我想要实现的目标:
我的 Sprite
纹理在游戏中发生变化,因此在发生变化后我需要 CollisionPolygon2D
自动进行相应更改 &我正在寻找耗电最少的解决方案
编辑:
当我指的是精灵时,我指的是不规则透明图片,例如这 可以满足我的需要,但它无法通过 代码,那么还有其他方法可以实现这一点吗?
我什至尝试了在旧论坛上找到的这个脚本:
extends CollisionPolygon2D
tool
export (bool) var from_sprite= false setget set_from_sprite
func set_from_sprite(new_val):
var bitmap = BitMap.new()
var children=self.get_children();
for child in children:
if(child.get_class()=="Sprite"):
if(child.texture==null):
continue
bitmap.create_from_image_alpha(child.texture.get_data())
var rect = Rect2(child.position.x, child.position.y, child.texture.get_width(), child.texture.get_height())
var polygons = bitmap.opaque_to_polygons(rect,0.5);
print(polygons)
self.polygon=polygons[0]
self.position -= Vector2((child.texture.get_width() / 2), (child.texture.get_height() / 2)) * child.scale.x
self.scale = child.scale
break;
property_list_changed_notify();
但它不如Create CollisionPolygon2Dsibling
那么精确
I'm trying to get the polygon
data (so I can add it to my custom node) from a CollisionPolygon2D
sibling created from a Sprite
via Code:
but I'm running into multiple problems:
How do I
Create CollisionPolygon2D Sibling
via code?When I select Region
Enabled
as true and then create, it gives a weird offset like this:
- Having to create a
CollisionPolygon2D
sibling first, getting its polygon and then deleting it is power consuming, is there a way I can directly get the polygon?
To Avoid an XY problem allow me to explain what I'm trying to achieve:
My Sprite
texture changes in-game, so after it changes I need the CollisionPolygon2D
to automatically change accordingly & I'm looking for the least power consuming solution
Edit:
when I meant sprite I meant a irregular transparent image, for example this does what I need but it doesn't work via code, so is there some other way to achieve this?
I even tried this script I found on an old forum:
extends CollisionPolygon2D
tool
export (bool) var from_sprite= false setget set_from_sprite
func set_from_sprite(new_val):
var bitmap = BitMap.new()
var children=self.get_children();
for child in children:
if(child.get_class()=="Sprite"):
if(child.texture==null):
continue
bitmap.create_from_image_alpha(child.texture.get_data())
var rect = Rect2(child.position.x, child.position.y, child.texture.get_width(), child.texture.get_height())
var polygons = bitmap.opaque_to_polygons(rect,0.5);
print(polygons)
self.polygon=polygons[0]
self.position -= Vector2((child.texture.get_width() / 2), (child.texture.get_height() / 2)) * child.scale.x
self.scale = child.scale
break;
property_list_changed_notify();
but it isn't as precise as Create CollisionPolygon2D sibling
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信
Sprite
始终是矩形,您可以使用get_Rect
获得,从矩形获得多边形的最佳方法是自己制作。这样的事情:
我不知道您实际想做什么形状。但是,看看一个 geetryrer> geetryry 类。它可能很有用,特别是如果您可以通过进行简单多边形的布尔操作来创建多边形(请参阅
clip_polygons_2d
,explude_polygons_2d
,MERGE_POLYGONS_2D
)。 另请参见convex_hull_2d
方法。请注意,您可以将
transform2d
应用于polygon(pool> poolvector2Array
)。更准确地说xform
和xform_inv
可以在pool> pool vector2array
上使用。顺便说一句,
bitmap
具有opaque_to_polygons
方法可能也有用,具体取决于情况。它应该根据给定的bitmap
在给定rect2
上的bitmap
(epsilon
是分辨率)上的bitmap
的alpha提供一个多边形。 a href =“ https://en.wikipedia.org/wiki/marching_squares” rel =“ nofollow noreferrer”>行进squares ,因此,较小的epsilon
也会更加精确,但也将是更精确的在更多点上)……这可能对复杂形状有用,或者可能是过度杀伤,或者根本不方便。I believe the
Sprite
would always be a rectangle, which you can get withget_rect
, and the best way to get a polygon from a rectangle is to make it yourself.Something like this:
I don't know what shape you actually want to do. But have a look a the Geometry class. It might be useful, in particular if you can create your polygon by doing boolean operations of simpler polygons (see
clip_polygons_2d
,exclude_polygons_2d
,intersect_polygons_2d
, andmerge_polygons_2d
). See also theconvex_hull_2d
method.Be aware that you can apply
Transform2D
to a polygon (PoolVector2Array
). More preciselyxform
andxform_inv
can take and work onPoolVector2Array
.By the way,
Bitmap
has anopaque_to_polygons
method that might also be useful, depending on the situation. It should give you a polygon based on the alpha of the pixels of theBitmap
on the givenRect2
(epsilon
is the resolution, it uses marching squares, so a smallerepsilon
will be more precise but also result in more points)… Which might be useful for complex shapes, or might be overkill, or might not be convenient at all.