Help with getting all objects at position.

Is there a way to get all objects at a position via there area or collision shapes? would have thought there would be something like:
Var new = objects_at_position(vector2(x,y))


  • MegalomaniakMegalomaniak Posts: 3,244Admin

    Sounds like you might want to look into using a raycast?

  • UnknownUserUnknownUser Posts: 1,389

    I’m not sure it will work with what I have in mind.
    I have an object that you can customize the shape of the collision_polygon, the idea is to find out the total area of the polygon using a 2d array and check each 64 x 64 square. I have done this by spawning detector objects which works good on small to large objects but becomes problematic when making anything overly large.

    Ideally I’d like to be able to check every 64 x 64 square for collision_polygon without creating instances.

  • MegalomaniakMegalomaniak Posts: 3,244Admin

    the square dots in each grid cell are the detector objects? Seems to me like they could just as well be raycasts. Key to performance would be to perhaps run them in their own thread and que the raycasts over multiple frames. Or do you absolutely need it to run the detection in every cell on every frame?

  • TwistedTwiglegTwistedTwigleg Posts: 3,856Admin

    You could use an Area2D with the correct collision size, and then use the get_overlapping_bodies function (documentation) to get all of the overlapping collision bodies.

  • UnknownUserUnknownUser Posts: 1,389

    There is a detector in each square in the grid. Each detector has 8 collision shapes, these are the small squares in each grid square. Then if enough collision shapes overlap the ship it shows a larger square in the grid indicating a valid place to put a turret.
    And this only needs to run once every time the shape changes.

    I think I might have found a solution but have run out of time to test it so i'll update this post later, but this is what I found:


  • UnknownUserUnknownUser Posts: 1,389

    Thanks guys, I was able to find what i needed to get my "Death Star" operational while looking into your suggestions.

    This is what I used to get it to work:

    extends Node2D
    var start_x
    var start_y
    var end_x
    var end_y
    var running = false
    func _physics_process(delta):
        if running == true:
            running = false
            for x in range(start_x,end_x):
                for y in range(start_y,end_y):
                    position = Vector2(x*64+32, y*64+32)
                    var get_all_objects = get_world_2d().direct_space_state.intersect_point(position,32,[],0x7FFFFFFF,true,true)
                    for i in range(get_all_objects.size()):
                        if get_all_objects [i].collider.get_parent() == System.Blue_Print:
                            System.Blue_Print.size += .125
    func _on_Timer_timeout():
        running = true

    I ended up needing to have a delay timer to get it to work, I just set it to 1 and it ran fine.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file