Skip to content

Commit

Permalink
Merge pull request #79493 from PatrikLundell/bounding_box
Browse files Browse the repository at this point in the history
Fixing bounding box errors
  • Loading branch information
Maleclypse authored Feb 3, 2025
2 parents b995a03 + 2fa0975 commit f72fb5c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
16 changes: 7 additions & 9 deletions src/map_extras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,18 +373,16 @@ static bool mx_helicopter( map &m, const tripoint_abs_sm &abs_sub )
// we can rotate it and calculate its bounding box, but don't place it on the map.
vehicle veh( crashed_hull );
veh.turn( dir1 );
// Get the bounding box, centered on mount(0,0), move the wreckage forward/backward
// half it's length so that it spawns more over the center of the debris area
// Find where the center of the vehicle is and adjust the spawn position to get it centered in the
// debris area.
const bounding_box bbox = veh.get_bounding_box();
const point_rel_ms length( std::abs( bbox.p2.x() - bbox.p1.x() ),
std::abs( bbox.p2.y() - bbox.p1.y() ) );
const point_rel_ms offset( veh.dir_vec().x * length.x() / 2, veh.dir_vec().y * length.y() / 2 );
const point_rel_ms min( std::abs( bbox.p1.x() ), std::abs( bbox.p1.y() ) );
const int x_max = SEEX * 2 - bbox.p2.x() - 1;
const int y_max = SEEY * 2 - bbox.p2.y() - 1;

point_rel_ms center_offset = {( bbox.p1.x() + bbox.p2.x() ) / 2, ( bbox.p1.y() + bbox.p2.y() ) / 2};
// Clamp x1 & y1 such that no parts of the vehicle extend over the border of the submap.
wreckage_pos = { clamp( c.x() + offset.x(), min.x(), x_max ), clamp( c.y() + offset.y(), min.y(), y_max ), abs_sub.z()};

wreckage_pos = { clamp( c.x() - center_offset.x(), abs( bbox.p1.x() ), SEEX * 2 - 1 - abs( bbox.p2.x() ) ),
clamp( c.y() - center_offset.y(), abs( bbox.p1.y() ), SEEY * 2 - 1 - abs( bbox.p2.y() ) ), abs_sub.z()
};
}

vehicle *wreckage = m.add_vehicle( crashed_hull, wreckage_pos, dir1, rng( 1, 33 ), 1 );
Expand Down
24 changes: 10 additions & 14 deletions src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1902,15 +1902,15 @@ bool vehicle::merge_appliance_into_grid( vehicle &veh_target )

bounding_box vehicle_box = get_bounding_box( false, true );
bounding_box target_vehicle_box = veh_target.get_bounding_box( false, true );

bounding_box combined_box;
combined_box.p1 = point_rel_ms( std::min( vehicle_box.p1.x(), target_vehicle_box.p1.x() ),
std::min( vehicle_box.p1.y(), target_vehicle_box.p1.y() ) );
combined_box.p2 = point_rel_ms( std::max( vehicle_box.p2.x(), target_vehicle_box.p2.x() ),
std::max( vehicle_box.p2.y(), target_vehicle_box.p2.y() ) );
const point_bub_ms min = { std::min( pos_bub().x() + vehicle_box.p1.x(), veh_target.pos_bub().x() + target_vehicle_box.p1.x() ),
std::min( pos_bub().y() + vehicle_box.p1.y(), veh_target.pos_bub().y() + target_vehicle_box.p1.y() )
};
const point_bub_ms max = { std::max( pos_bub().x() + vehicle_box.p1.x(), veh_target.pos_bub().x() + target_vehicle_box.p1.x() ),
std::max( pos_bub().y() + vehicle_box.p1.y(), veh_target.pos_bub().y() + target_vehicle_box.p1.y() )
};
point_rel_ms size;
size.x() = std::abs( ( combined_box.p2 - combined_box.p1 ).x() ) + 1;
size.y() = std::abs( ( combined_box.p2 - combined_box.p1 ).y() ) + 1;
size.x() = std::abs( ( max - min ).x() ) + 1;
size.y() = std::abs( ( max - min ).y() ) + 1;

//Make sure the resulting vehicle would not be too large
if( size.x() <= MAX_WIRE_VEHICLE_SIZE && size.y() <= MAX_WIRE_VEHICLE_SIZE ) {
Expand Down Expand Up @@ -8256,17 +8256,13 @@ bounding_box vehicle::get_bounding_box( bool use_precalc, bool no_fake )
point_rel_ms pt;
if( use_precalc ) {
const int i_use = 0;
// TODO: Check if this is correct. part_at takes a vehicle relative position, not a bub one...
// int part_idx = part_at((p - pos_abs()).xy()); // Suggested correction.
int part_idx = part_at( rebase_rel( get_map().get_bub( p ).xy() ) );
int part_idx = part_at( ( p - pos_abs() ).xy() );
if( part_idx < 0 ) {
continue;
}
pt = parts[part_idx].precalc[i_use].xy();
} else {
// TODO: Check if this is correct. part_at takes a vehicle relative position, not a bub one...
// pt = (p - pos_abs()).xy(); // Suggested correction.
pt = rebase_rel( get_map().get_bub( p ).xy() );
pt = ( p - pos_abs() ).xy();
}
if( pt.x() < min_x ) {
min_x = pt.x();
Expand Down

0 comments on commit f72fb5c

Please sign in to comment.