How to add collision limits for maps?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JulioYagami

I don’t know a good way to add limits to a map. I tried staticbody2D blocks but it is too expensive for processing.

I’m currently using tilemaps.

JulioYagami | 2018-12-08 11:41

What exacty do you mean by limit. Is it for collision with moving entities, camera or disabling some functions? If it is for collision why you can’t use some tile wich is already obstacle in your tileset. Or you can add transparent tile with collision rectangle and add it on boreders of your level.

pospathos | 2018-12-08 12:45

:bust_in_silhouette: Reply From: Xrayez

It depends on how big is your map. I’m currently building world borders, using StaticBody2D with RectangleShape2D as collision shape. It’s not as expensive as you might think, the geometry of the shape is actually simple to check collision against (though I haven’t really tested this yet), especially for a static body. I think it’s possible to build limits/borders using TileMap, but I have little experience using it.

If you want to try building borders the way I do, take a look at this structure:

enter image description here

The sprite defines visible border area, set the texture there, and make sure to import the texture with Repeat flag in the import dock. The challenge here is to match the sprite with the actual shape’s area. Here’s the script that allows to texture the extents of the static body’s rectangular shape as well as to change the actual size:

extends StaticBody2D

var size setget set_size

func set_size(p_size):
    size = p_size
    $sprite.region_rect = Rect2(Vector2(), size)
    # CollisionShape2D is only used in editor! 
    # Use shape_owner to retrieve the actual shape.
    shape_owner_get_shape(0, 0).extents = size / 2

Make sure to set Region Enabled property for the sprite so that the pattern can repeat. If setting the size only resizes the region rect of the sprite and not the shape, you can try to set it on the next frame (I’ve had this issue when trying to initialize border sizes within a setter method of an exported property):

$border.call_deferred('set_size", Vector2(1000, 50)) # or ...
$border.set_deferred('size", Vector2(1000, 50)) # Godot 3.1

Most likely you’ll also want to instance many borders and set different sizes for each. But if you try to change the extents of RectangleShape2D, it will also resize the borders of all other instanced shapes! That’s because the resources are created and loaded once, unlike other objects that can have many instances. To solve this, make sure to check Local To Scene property so that the resource will be duplicated for each instanced border:

enter image description here

It’s a good idea, but I used tilemap collision (I really didn’t know I could use tiles with collision checks).

JulioYagami | 2018-12-08 13:11