ShapeDraw2D - jhlothamer/godot_helper_pack GitHub Wiki

Overview

The ShapeDraw2D node draws the shape of a parent CollisionShape2D or CollisionPolygon2D. It does this both in the editor as well as when your game is running. It is meant to help with prototyping when you want to see your collision shapes and you don't yet have art for your game.

Why Not Just Rely On the "Visible Collision Shapes" Option?

In the Godot Engine editor, under the Debug menu, there's an option called "Visible Collision Shapes". Whenever this is checked, you will see the collision shapes drawn when the game runs. Shapes are drawn in a partially transparent shade of blue, but you can change this in the Project Settings. (See Project Settings -> Debug -> Shapes -> Shape Color)

This makes the ShapeDraw2D node redundant. And if that's all you need, then it is. However, there are a few differences to using the ShapeDraw2D node.

  • You can have different colors for different shapes.
  • You can use textures rather than just colors.
  • What you see in the editor is what you'll see in the game.
  • If you have multiple hit-boxes or areas in a scene that overlap (think for the player or enemy scene), enabling visible collision shapes can obscure things. However, this little issue can be fixed by hiding the collision shapes for things you don't want to see when the option is set. It all depends on if having to do this is bothersome for you or not.

Usage

To use the ShapeDraw2D, just add it as a child to any CollisionShape2D or CollsionPolygon2D. You can set it's color or texture. If the shape for a CollisionShape2D is a RectangleShape2D, then you can treat the texture as a nine patch rectangle texture. Otherwise you'll have the option to set the texture offset and scale. Of course, for shapes with no area (ray, line, segment) the texture will be ignored.

Demo

There are two demo scenes in the project. One is to demo the color setting and all possible collision shapes. The other is to demo textures and the shapes that can support textures. To see the demos, just clone the repository and open the project. Searching the File System for "shape" and you'll easily see the demo scenes.

Properties

Name Description limits
Color color Ignored if texture set
Texture texture Not for line, ray or segment shapes
Texture Offset texture offset Not for RectangleShape2D
Texture Scale texture scale Not for RectangleShape2D
Draw Center same as option for NinePatchRect Only for RectangleShape2D
Region Rect same as option for NinePatchRect Only for RectangleShape2D
Patch Left same as option for NinePatchRect Only for RectangleShape2D
Patch right same as option for NinePatchRect Only for RectangleShape2D
Patch Top same as option for NinePatchRect Only for RectangleShape2D
Patch Bottom same as option for NinePatchRect Only for RectangleShape2D
Axis Stretch Horizontal same as option for NinePatchRect Only for RectangleShape2D
Axis Stretch Vetical same as option for NinePatchRect Only for RectangleShape2D