Refactor phys2d engine interface

This commit is contained in:
Nikita Lisitsa 2021-05-07 18:20:59 +03:00
parent 7eb589532c
commit 98d26f76cf
3 changed files with 29 additions and 14 deletions

View file

@ -436,7 +436,7 @@ struct physics_2d_app
auto p = physics.group_static_state(ball_group)[i].position;
auto a = physics.group_static_state(ball_group)[i].rotation;
float r = std::get<phys2d::ball>(physics.shape(ball_group, i)).radius;
float r = std::get<phys2d::ball const *>(physics.get_shape(physics.get_object(ball_group, i).shape))->radius;
painter.circle(p, r, gfx::black);
painter.circle(p, r - line_width, gfx::light(gfx::blue, selected ? 0.8f : 1.f).as_color_rgba());
@ -455,8 +455,8 @@ struct physics_2d_app
auto p = physics.group_static_state(box_group)[i].position;
auto a = physics.group_static_state(box_group)[i].rotation;
float w = std::get<phys2d::box>(physics.shape(box_group, i)).width / 2.f;
float h = std::get<phys2d::box>(physics.shape(box_group, i)).height / 2.f;
float w = std::get<phys2d::box const *>(physics.get_shape(physics.get_object(box_group, i).shape))->width / 2.f;
float h = std::get<phys2d::box const *>(physics.get_shape(physics.get_object(box_group, i).shape))->height / 2.f;
geom::vector<float, 2> q[4];
q[0] = {-w, -h};

View file

@ -25,6 +25,8 @@ namespace psemek::phys2d
shape_handle add_shape(half_space const & s);
shape_handle add_shape(box const & b);
std::variant<ball const *, half_space const *, box const *> get_shape(shape_handle handle) const;
void remove_shape(shape_handle handle);
// Material management
@ -32,6 +34,10 @@ namespace psemek::phys2d
using material_handle = std::uint32_t;
material_handle add_material(material const & m);
material const & get_material(material_handle handle) const;
material & get_material(material_handle handle);
void remove_material(material_handle handle);
// Group & body management
@ -58,9 +64,7 @@ namespace psemek::phys2d
float inv_inertia;
};
object_info const & info(group_handle group, std::size_t index);
std::variant<ball, half_space, box> shape(group_handle group, std::size_t index);
object_info const & get_object(group_handle group, std::size_t index);
// Global system properties

View file

@ -1022,6 +1022,14 @@ namespace psemek::phys2d
return impl().shapes.insert(wrap(b));
}
std::variant<ball const *, half_space const *, box const *> engine::get_shape(shape_handle handle) const
{
using result_type = std::variant<ball const *, half_space const *, box const *>;
return impl().shapes.visit([](auto const & w) -> result_type {
return &(w.shape);
}, handle);
}
void engine::remove_shape(shape_handle handle)
{
impl().shapes.erase(handle);
@ -1032,6 +1040,16 @@ namespace psemek::phys2d
return impl().materials.insert(m);
}
material const & engine::get_material(material_handle handle) const
{
return impl().materials[handle];
}
material & engine::get_material(material_handle handle)
{
return impl().materials[handle];
}
void engine::remove_material(material_handle handle)
{
impl().materials.erase(handle);
@ -1099,18 +1117,11 @@ namespace psemek::phys2d
g.dynamic_states.erase(g.dynamic_states.begin() + index);
}
engine::object_info const & engine::info(group_handle group, std::size_t index)
engine::object_info const & engine::get_object(group_handle group, std::size_t index)
{
return impl().groups[group].infos[index];
}
std::variant<ball, half_space, box> engine::shape(group_handle group, std::size_t index)
{
using result = std::variant<ball, half_space, box>;
auto sh = impl().groups[group].infos[index].shape;
return impl().shapes.visit([](auto const & s) -> result { return s.shape; }, sh);
}
void engine::set_gravity(geom::vector<float, 2> const & g)
{
impl().gravity = g;