07.colliding-with-the-environment

Colliding with the environment

In this video, we set up collisions around our character to see how the KinematicBody2D.move_and_slide() function allows us to collide with the environment.

We also take this opportunity to test the CollisionPolygon2D node.

New concepts

The CollisionPolygon2D node

The CollisionPolygon2D node allows you to draw collision polygons. Unlike the CollisionShape2D node, you’re not limited to predefined geometric shapes. Instead, you create your own shapes.

This freedom comes at a potential performance cost, so we recommend using the CollisionShape2D node whenever you don’t need a CollisionPolygon2D.

Adding, moving, and deleting points

To change your polygon, select the CollisionPolygon2D node and, in the toolbar at the top, click the icons to change the drawing tool.

There are three tools:

  1. The green plus icon draws a new polygon. This tool will replace the existing polygon if you have already drawn a shape.
  2. The middle icon lets you move and add points. Use this tool to edit an existing shape. With this tool, you can also delete points. To do so, right-click on an existing point.
  3. The red cross icon deletes points. When it’s active, left-click on existing points to delete them.

Snapping points to the grid

You can use the 2D grid and snapping to draw horizontal and vertical lines with your collision polygon. To turn on grid snapping, press Shift+G or click the icon with a magnet in the toolbar.

To configure the grid’s spacing, click the vertical three dots in the toolbar, then Configure Snap…

Practice: Bumping in walls

Open the practice Bumping in walls.

In the next series, we’ll learn about Procedural Content Generation (PCG).

Best of luck with the practice, and we’ll see you in the next chapter.