Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | PopeRigby |
I have this bit of code for a subdivision function that is applied on a square:
extends CollisionShape2D
func _process(_delta):
if Input.is_action_just_pressed("subdivide"):
var oldExtents = shape.get_extents()
var oldPosition = get_position()
queue_free() # removes the original block
var topLeft = duplicate()
get_parent().add_child(topLeft) # adds topLeft as a child of StaticBody2D
topLeft.shape.extents = Vector2(oldExtents.x/2,oldExtents.y/2)
topLeft.get_node("Sprite").scale = topLeft.shape.extents * 0.0316
topLeft.set_position(oldPosition + Vector2(-oldExtents.x*2,-oldExtents.y*2))
# set_pos(get_pos() + Vector2(-new_size/2, -new_size/2))
var topRight = duplicate()
get_parent().add_child(topRight)
topRight.shape.extents = Vector2(oldExtents.x/2,oldExtents.y/2)
topRight.get_node("Sprite").scale = topRight.shape.extents * 0.0316
topRight.set_position(oldPosition + Vector2(oldExtents.x*2,-oldExtents.y*2))
var bottomLeft = duplicate()
get_parent().add_child(bottomLeft)
bottomLeft.shape.extents = Vector2(oldExtents.x/2,oldExtents.y/2)
bottomLeft.get_node("Sprite").scale = bottomLeft.shape.extents * 0.0316
bottomLeft.set_position(oldPosition + Vector2(-oldExtents.x*2,oldExtents.y*2))
var bottomRight = duplicate()
get_parent().add_child(bottomRight)
bottomRight.shape.extents = Vector2(oldExtents.x/2,oldExtents.y/2)
bottomRight.get_node("Sprite").scale = bottomRight.shape.extents * 0.0316
bottomRight.set_position(oldPosition + Vector2(oldExtents.x*2,oldExtents.y*2))
print(get_position())
Here’s what currently happens when I click subdivide:
https://watch.haddock.cc/videos/watch/b4cd3847-ac42-4cee-916e-e32bbe446523
As you can see, it works fine the first division, but on the second division it only works on the top left corner. All the other corners get gradually smaller. On the third division I have no idea what’s happening. So what can I do to get this to subdivide nicely?