Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tilemap physics' World2D on reparenting #84968

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions scene/2d/tile_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,26 +686,30 @@ void TileMapLayer::_physics_update() {

void TileMapLayer::_physics_notify_tilemap_change(TileMapLayer::DirtyFlags p_what) {
Transform2D gl_transform = tile_map_node->get_global_transform();
PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
Copy link
Member

@AThousandShips AThousandShips Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know that storing the server in a variable for three uses is necessary and makes the code harder to read IMO (leftover from the original PR and was planning to comment the same there)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to agree it's not necessary, but it doesn't seem to be a bad change either, so I'm fine merging as is (and mostly in a hurry to get the master branch somewhat RC1 ready ;)).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know that storing the server in a variable for three uses is necessary and makes the code harder to read IMO (leftover from the original PR and was planning to comment the same there)

It's commonly done in the TileMap codebase (using ps, rs or ns for the different servers), so it's more or less consistent with what we already have I believe. I think anyone familiar with the TileMap-related code should be familiar with this, so it's does not hurt readability much I think. But yeah, that's debatable.


bool in_editor = false;
#ifdef TOOLS_ENABLED
in_editor = Engine::get_singleton()->is_editor_hint();
#endif

if (p_what == DIRTY_FLAGS_TILE_MAP_XFORM) {
if (tile_map_node->is_inside_tree() && (!tile_map_node->is_collision_animatable() || in_editor)) {
// Move the collisison shapes along with the TileMap.
for (KeyValue<Vector2i, CellData> &kv : tile_map) {
const CellData &cell_data = kv.value;

for (RID body : cell_data.bodies) {
if (body.is_valid()) {
Transform2D xform(0, tile_map_node->map_to_local(bodies_coords[body]));
xform = gl_transform * xform;
PhysicsServer2D::get_singleton()->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
}
}
}
}
} else if (p_what == DIRTY_FLAGS_TILE_MAP_LOCAL_XFORM) {
// With collisions animatable, move the collisison shapes along with the TileMap only on local xform change (they are synchornized on physics tick instead).
if (tile_map_node->is_inside_tree() && tile_map_node->is_collision_animatable() && !in_editor) {
for (KeyValue<Vector2i, CellData> &kv : tile_map) {
const CellData &cell_data = kv.value;
Expand All @@ -714,7 +718,22 @@ void TileMapLayer::_physics_notify_tilemap_change(TileMapLayer::DirtyFlags p_wha
if (body.is_valid()) {
Transform2D xform(0, tile_map_node->map_to_local(bodies_coords[body]));
xform = gl_transform * xform;
PhysicsServer2D::get_singleton()->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
}
}
}
}
} else if (p_what == DIRTY_FLAGS_TILE_MAP_IN_TREE) {
// Changes in the tree may cause the space to change (e.g. when reparenting to a SubViewport).
if (tile_map_node->is_inside_tree()) {
RID space = tile_map_node->get_world_2d()->get_space();

for (KeyValue<Vector2i, CellData> &kv : tile_map) {
const CellData &cell_data = kv.value;

for (RID body : cell_data.bodies) {
if (body.is_valid()) {
ps->body_set_space(body, space);
}
}
}
Expand Down
Loading