Interaction between RigidBody2D and Area2D

Godot Version

4.3

Question

Hello,
I’m working on a physics based game where a ball (RigidBody2D) is falling pulled by gravity and the player has to keep it up by blowing with a fan (the fan’s air is an Area2D).
I have been able to modify the gravity so the ball can go up and down, but now I want to apply a small horizontal force to the ball if it enters the Area2D from a side; I’ve been looking around on ways to detect from where the RigidBody2D enters the Area2D, but I have not found a solution yet.

If anyone can provide any pointers, I would be grateful.

PS. The scenes look like in the attached pictures

image
image

There is no built-in way to do that, you have to check it through code. E.g.

var _on_body_entered(body: Node2D) -> void:
    if body.global_position.x > global_position.x:
        # do something

Thanks!
As the post neede to be approved I tried that and it worked, I’m not using global_position tough, I just chose to use position; any drawbacks?

If your fan or a ball will ever be a child of another node that has its position moved, your code will not behave the way you expect it, because its position will depend on the parent’s position.
I would suggest you to use global_position instead, it will work regardless of how your Nodes are parented and moved.

Thanks so much!
I found out when adding a couple additional Area2D and it didn’t behave as expected. global_position is the way to go

1 Like