0 votes

I am creating a game like Tetris. I created the barriers/walls using Staticbody 2D and the blocks using Area 2D. When I run it the blocks pass through the Staticbody 2D without collission.How can I stop the falling blocks(Area 2D) when it touches the Staticbody 2D and stay at the top of the wall ?

in Engine by (141 points)

Did you try to use Layers and Masks?

Just so that we're on the same page: you do know that Area2D is for detecting collisions, and not really for stopping collisions?

You are totally right. My own mistake. Probably has to replace the Aread2D with KinematicBodies2D.

1 Answer

+2 votes
Best answer

Hi there! Area2D is basically what its name is, an AREA. It's mostly used for detecting collisions (detecting whether something entered an area, collided with an area, etc.) and while you can move this Area2D, it will not react to collisions but it may give you an output if you give it a script to do so.

You may want to replace the Area with a type of physics body (Kinematic, Rigid, Static)

Kinematic - this is used when you want an object to be solely controlled by you and its script.

Rigid - Rigid bodies are affected by physics whether they have scripts or not in them. Example, Angry Birds. You launch a bird to stones, wood, and pigs and they act like real physical objects in terms of falling, sliding, bouncing, rolling, and without scripts in them.

Static - this is commonly used for background and walls since it's not affected by physics hence its name "static". However, objects can still collide with this.

As for your game, I recommend using kinematic body since you want it to be controlled by a script (I assume moving 1 tile-space for a certain amount of time). However, you may want to add an area2d as a child of this kinematic body to detect collision so that when the area2d detects collision, it can tell its parent (the kinematic body) to stop moving either by setting the velocity, speed, or whatever variable you put in to 0, or you can just make a true/false flag like (Note, this is just a pseudo-code):

var can_move = true

func physicsprocess(delta):
if can_move == true:
*Insert your code of action here*

func onArea2Dbodyentered(body):
can_move = false

This means that in the beginning, the body can move but if another body (or area, depends on your game) touches it, the can_move will be set to false and will stop the body from moving.

by (104 points)
selected by
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.