Godot Version
4.3
Question
I’m extremely new to Godot and want to get the size of my player in order to clamp its edges to the screen. I found the proper code for GDScript:
player_size = $CollisionShape2D.shape.get_rect().size
position = position.clamp(Vector2.ZERO+player_size/2, screen_size-player_size/2)
I was trying to use this as a guideline for my own purposes and translate it to C# myself, but I’m running into a different issue than I expected: my player uses CollisionPolygon2D instead of CollisionShape2D, and the docs tell me that CollisionPolygon2D doesn’t have anything to get the size like the .shape.get_rect().size part of the above code. The only solution I can then think of would be to somehow get the locations of the vertices, get the highest and lowest X and Y values for them, and determine the size that way. How would I go about doing this in proper syntax? Or is there a better solution that I don’t know?
Why do you want to get this size via code? The only thing I can think of is that you are constantly changing the size of the collision shape via code. And then what are you going to do with this size?
If the shape remains the same size throughout the game it would be far better to just use a constant.
However, here is a GDScript solution that is just iterating the polygons PackedVector2Array looking for lowest and highest values and subtracting.
I kind of think this is an inelegant solution and that there is likely a math-ier way of doing this.
If you are doing this in a _process()
function this solution might be too slow.
func get_pvector_extents(p:PackedVector2Array)->Vector4:
var minX:float = p[0].x
var maxX:float = p[0].x
var minY:float = p[0].y
var maxY:float = p[0].y
for v in p:
if v.x < minX:
minX = v.x
if v.x > maxX:
maxX = v.x
if v.y < minY:
minY = v.y
if v.y > maxY:
maxY = v.y
return Vector4(minX, minY, maxX, maxY)
func get_polygon_size(p:PackedVector2Array)->Vector2:
var v:Vector4 = get_pvector_extents(p)
var r:Rect2 = Rect2(v.x, v.y, abs(v.z - v.x), abs(v.w - v.y))
return r.size
func _ready() -> void:
print(get_polygon_size(collision_polygon_2d.polygon))
Edit: I redid this since it was wonky. I must have been in a state of brain freeze.
But what about in C#? That was one of my main points, was that I wanted the C# solution for this problem. Although I’ve been debating if its even worth it to try and use C# for Godot. I wanted to learn it because C# is more general purpose, its still just almost entirely using a Godot library for syntax anyways. GDScript is also just documented way better, so should I just learn GDScript instead?
Also, that solution is pretty much the one I was thinking would be possible but wanted to avoid if possible, but thank you for showing me how it would be done anyways. Again, I am extremely new to all of this so I appreciate any help I can get so again, thank you.
1 Like
I was under the impression that you could transcribe it to C#.
I haven’t used C# in many years and the language has changed considerably.
I’ll give it a shot though if no one else comes through for you in a couple of days.