0 votes

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()
        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()
        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()
        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))


Here's what currently happens when I click subdivide:

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?

asked Jun 5 in Engine by PopeRigby (24 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.