Appropriate uses for PhysicsBody2D and Area2D

Godot Version



I’ve been working on a top down 2d game for a bit now, and have been struggling to fully understand the physics system. Up until now, I’ve been using the physics simulation to detect when certain things should happen. After wrestling with interactions between RigidBody2Ds, CharacterBody2Ds, and StaticBody2Ds, it occurred to me that I may be mixing responsibilities here.

  1. Detecting when things collide for the purposes of game logic.
  2. Detection when things collide for the purposes of the physics simulation.

My most recent problem was that a rigid body was knocking a character body even if when I tried to force the character body’s velocity to zero. I suspect that this happens when the character body tried to separate itself from the collision. I could fix this by changing the collision mask on the character to ignore the rigid body, but then I lose the detection mechanism. But, anyway, this got my spidy sense tingling and it started to feel like I’m trying to hammer a square peg into a round hole. Hence, this question:

Is it better to approach use PhysicsBody2Ds solely to hook into the physics system and use Area2Ds to kick off responses to any other type of events?

For example, say, when a projectile hits a Mob, it should ricochet off the Mob and free itself after a certain time, and the Mob should take damage. Should these entities derive from the appropriate PhysicsBody2D so that the physics system can do its job, AND ALSO have an Area2D to kick off their game logic?
Or, is it better to respond to the physics events and only bring in an Area2D when you know physics isn’t required?

When following the same decision making process over a large code base, which would lead to the smaller mess of spaghetti?

1 Like

If you need physics for you object interactions then you may as just well stick with that for collisions, no point in doing that twice.

Simple run of thumb (for me anyway) if you need something to react realistically with physics then use one of the physics bodes for that. (So player for example and then a wall or floor)

If the object that hitting your player such as a bullet does not need physics in any way then just use Area2Ds instead.

If however you need gravity to affect a bullet or missile (think Worms for example) then a physics body would be better.

Just have a clear view of which objects fall into which category and you should be fine.

1 Like