Skip to content

Commit

Permalink
Speed improvements in the collision loop.
Browse files Browse the repository at this point in the history
- Use Boost.Graph to solve the process order of the items.
- Cache the bounding box of the items to prevent useless computations.
- Use a dummy shape to fake the implementation of bear::universe::shape.
- Pre compute the bounding boxes before searching the collisions.
- bear::universe::static_map caches the bounding box of the items.
  • Loading branch information
j-jorge committed Nov 8, 2014
1 parent a36e31c commit b804c00
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 139 deletions.
58 changes: 46 additions & 12 deletions bear-engine/core/src/universe/code/physical_item_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* \brief Constructor.
*/
bear::universe::physical_item_state::physical_item_state()
: m_fixed(false)
: m_fixed(false), m_bounding_box_is_dirty(false), m_bounding_box( 0, 0, 0, 0 )
{

} // physical_item_state::physical_item_state()
Expand All @@ -37,7 +37,8 @@ bear::universe::physical_item_state::physical_item_state()
*/
bear::universe::physical_item_state::physical_item_state
( const physical_item_state& that )
: m_attributes(that.m_attributes), m_fixed(false)
: m_attributes(that.m_attributes), m_fixed(false),
m_bounding_box_is_dirty(false), m_bounding_box(that.m_bounding_box)
{

} // physical_item_state::physical_item_state()
Expand All @@ -58,7 +59,8 @@ bear::universe::physical_item_state::~physical_item_state()
bear::universe::size_box_type
bear::universe::physical_item_state::get_size() const
{
return shape_traits<shape>::get_size( m_attributes.m_shape );
refresh_bounding_box();
return m_bounding_box.size();
} // physical_item_state::get_size()

/*----------------------------------------------------------------------------*/
Expand All @@ -67,7 +69,8 @@ bear::universe::physical_item_state::get_size() const
*/
bear::universe::size_type bear::universe::physical_item_state::get_width() const
{
return shape_traits<shape>::get_width( m_attributes.m_shape );
refresh_bounding_box();
return m_bounding_box.width();
} // physical_item_state::get_width()

/*----------------------------------------------------------------------------*/
Expand All @@ -77,7 +80,8 @@ bear::universe::size_type bear::universe::physical_item_state::get_width() const
bear::universe::size_type
bear::universe::physical_item_state::get_height() const
{
return shape_traits<shape>::get_height( m_attributes.m_shape );
refresh_bounding_box();
return m_bounding_box.height();
} // physical_item_state::get_height()

/*----------------------------------------------------------------------------*/
Expand All @@ -99,7 +103,8 @@ void bear::universe::physical_item_state::set_bounding_box
bear::universe::rectangle_type
bear::universe::physical_item_state::get_bounding_box() const
{
return shape_traits<shape>::get_bounding_box( m_attributes.m_shape );
refresh_bounding_box();
return m_bounding_box;
} // physical_item_state::get_bounding_box()

/*----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -475,7 +480,10 @@ void bear::universe::physical_item_state::set_top( coordinate_type pos )
void bear::universe::physical_item_state::set_bottom( coordinate_type pos )
{
if (!m_fixed && (m_attributes.m_y_fixed == 0))
shape_traits<shape>::set_bottom( m_attributes.m_shape, pos );
{
m_bounding_box_is_dirty = true;
shape_traits<shape>::set_bottom( m_attributes.m_shape, pos );
}
} // physical_item_state::set_bottom()

/*----------------------------------------------------------------------------*/
Expand All @@ -486,7 +494,10 @@ void bear::universe::physical_item_state::set_bottom( coordinate_type pos )
void bear::universe::physical_item_state::set_left( coordinate_type pos )
{
if (!m_fixed && (m_attributes.m_x_fixed == 0))
shape_traits<shape>::set_left( m_attributes.m_shape, pos );
{
m_bounding_box_is_dirty = true;
shape_traits<shape>::set_left( m_attributes.m_shape, pos );
}
} // physical_item_state::set_left()

/*----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -728,7 +739,8 @@ void bear::universe::physical_item_state::set_right_middle
bear::universe::coordinate_type
bear::universe::physical_item_state::get_left() const
{
return shape_traits<shape>::get_left( m_attributes.m_shape );
refresh_bounding_box();
return m_bounding_box.left();
} // physical_item_state::get_left()

/*----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -758,7 +770,8 @@ bear::universe::physical_item_state::get_right() const
bear::universe::coordinate_type
bear::universe::physical_item_state::get_bottom() const
{
return shape_traits<shape>::get_bottom( m_attributes.m_shape );
refresh_bounding_box();
return m_bounding_box.bottom();
} // physical_item_state::get_bottom()

/*----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -1359,7 +1372,10 @@ void bear::universe::physical_item_state::set_size
void bear::universe::physical_item_state::set_width( size_type width )
{
if (!m_fixed && (m_attributes.m_x_fixed == 0))
shape_traits<shape>::set_width( m_attributes.m_shape, width );
{
m_bounding_box_is_dirty = true;
shape_traits<shape>::set_width( m_attributes.m_shape, width );
}
} // physical_item_state::set_width()

/*----------------------------------------------------------------------------*/
Expand All @@ -1370,7 +1386,10 @@ void bear::universe::physical_item_state::set_width( size_type width )
void bear::universe::physical_item_state::set_height( size_type height )
{
if (!m_fixed && (m_attributes.m_y_fixed == 0))
shape_traits<shape>::set_height( m_attributes.m_shape, height );
{
m_bounding_box_is_dirty = true;
shape_traits<shape>::set_height( m_attributes.m_shape, height );
}
} // physical_item_state::set_height()

/*----------------------------------------------------------------------------*/
Expand All @@ -1391,6 +1410,8 @@ void bear::universe::physical_item_state::set_shape( const shape& s )
shape_traits<shape>::set_width
( m_attributes.m_shape, bounding_box.width() );
}
else
m_bounding_box_is_dirty = true;

if ( m_fixed || (m_attributes.m_y_fixed != 0) )
{
Expand All @@ -1399,6 +1420,8 @@ void bear::universe::physical_item_state::set_shape( const shape& s )
shape_traits<shape>::set_height
( m_attributes.m_shape, bounding_box.height() );
}
else
m_bounding_box_is_dirty = true;
} // physical_item_state::set_shape()

/*----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -1442,6 +1465,7 @@ void bear::universe::physical_item_state::set_physical_state
return;

m_attributes = s.m_attributes;
m_bounding_box_is_dirty = true;

m_attributes.m_x_fixed = s.m_attributes.m_x_fixed;
m_attributes.m_y_fixed = s.m_attributes.m_y_fixed;
Expand Down Expand Up @@ -1502,6 +1526,16 @@ void bear::universe::physical_item_state::to_string( std::string& str ) const
str += oss.str();
} // physical_item_state::to_string()

void bear::universe::physical_item_state::refresh_bounding_box() const
{
if ( !m_bounding_box_is_dirty )
return;

m_bounding_box =
shape_traits<shape>::get_bounding_box( m_attributes.m_shape );
m_bounding_box_is_dirty = false;
}

/*----------------------------------------------------------------------------*/
/**
* \brief Output a text representation of an item.
Expand Down
Loading

0 comments on commit b804c00

Please sign in to comment.