From 98d26f76cfd10e6fce70914c2c7d93513697ae10 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Fri, 7 May 2021 18:20:59 +0300 Subject: [PATCH] Refactor phys2d engine interface --- examples/physics_2d.cpp | 6 ++--- libs/phys/include/psemek/phys/engine_2d.hpp | 10 +++++--- libs/phys/source/engine_2d.cpp | 27 +++++++++++++++------ 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/examples/physics_2d.cpp b/examples/physics_2d.cpp index a73d0828..be688e36 100644 --- a/examples/physics_2d.cpp +++ b/examples/physics_2d.cpp @@ -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(physics.shape(ball_group, i)).radius; + float r = std::get(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(physics.shape(box_group, i)).width / 2.f; - float h = std::get(physics.shape(box_group, i)).height / 2.f; + float w = std::get(physics.get_shape(physics.get_object(box_group, i).shape))->width / 2.f; + float h = std::get(physics.get_shape(physics.get_object(box_group, i).shape))->height / 2.f; geom::vector q[4]; q[0] = {-w, -h}; diff --git a/libs/phys/include/psemek/phys/engine_2d.hpp b/libs/phys/include/psemek/phys/engine_2d.hpp index 7d880283..496a7de8 100644 --- a/libs/phys/include/psemek/phys/engine_2d.hpp +++ b/libs/phys/include/psemek/phys/engine_2d.hpp @@ -25,6 +25,8 @@ namespace psemek::phys2d shape_handle add_shape(half_space const & s); shape_handle add_shape(box const & b); + std::variant 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 shape(group_handle group, std::size_t index); + object_info const & get_object(group_handle group, std::size_t index); // Global system properties diff --git a/libs/phys/source/engine_2d.cpp b/libs/phys/source/engine_2d.cpp index b747c675..cb59bbb9 100644 --- a/libs/phys/source/engine_2d.cpp +++ b/libs/phys/source/engine_2d.cpp @@ -1022,6 +1022,14 @@ namespace psemek::phys2d return impl().shapes.insert(wrap(b)); } + std::variant engine::get_shape(shape_handle handle) const + { + using result_type = std::variant; + 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 engine::shape(group_handle group, std::size_t index) - { - using result = std::variant; - 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 const & g) { impl().gravity = g;