Physics engine 2d: support retrieving object info

This commit is contained in:
Nikita Lisitsa 2020-12-01 22:03:25 +03:00
parent 3b141aa84d
commit dcaff99b75
2 changed files with 29 additions and 10 deletions

View file

@ -7,6 +7,7 @@
#include <psemek/geom/box.hpp> #include <psemek/geom/box.hpp>
#include <memory> #include <memory>
#include <variant>
namespace psemek::phys2d namespace psemek::phys2d
{ {
@ -46,9 +47,21 @@ namespace psemek::phys2d
static_state * group_static_state(group_handle handle); static_state * group_static_state(group_handle handle);
dynamic_state * group_dynamic_state(group_handle handle); dynamic_state * group_dynamic_state(group_handle handle);
void add_object(group_handle group, shape_handle shape, material_handle material, static_state const & ss, dynamic_state const & ds); std::size_t add_object(group_handle group, shape_handle shape, material_handle material, static_state const & ss, dynamic_state const & ds);
void remove_object(group_handle group, std::size_t index); void remove_object(group_handle group, std::size_t index);
struct object_info
{
engine::shape_handle shape;
engine::material_handle material;
float inv_mass;
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);
// Global system properties // Global system properties
void set_gravity(geom::vector<float, 2> const & g); void set_gravity(geom::vector<float, 2> const & g);

View file

@ -369,14 +369,6 @@ namespace psemek::phys2d
util::flat_list<material, engine::material_handle> materials; util::flat_list<material, engine::material_handle> materials;
struct object_info
{
engine::shape_handle shape;
engine::material_handle material;
float inv_mass;
float inv_inertia;
};
struct group struct group
{ {
std::vector<object_info> infos; std::vector<object_info> infos;
@ -687,7 +679,7 @@ namespace psemek::phys2d
return impl().groups[handle].dynamic_states.data(); return impl().groups[handle].dynamic_states.data();
} }
void engine::add_object(group_handle group, shape_handle shape, material_handle material, static_state const & ss, dynamic_state const & ds) std::size_t engine::add_object(group_handle group, shape_handle shape, material_handle material, static_state const & ss, dynamic_state const & ds)
{ {
float const density = impl().materials[material].density; float const density = impl().materials[material].density;
float const area = impl().shapes.visit([](auto const & s){ return s.area; }, shape); float const area = impl().shapes.visit([](auto const & s){ return s.area; }, shape);
@ -697,6 +689,8 @@ namespace psemek::phys2d
g.infos.push_back({shape, material, 1.f / (area * density), 1.f / (inertia * density)}); g.infos.push_back({shape, material, 1.f / (area * density), 1.f / (inertia * density)});
g.static_states.push_back(ss); g.static_states.push_back(ss);
g.dynamic_states.push_back(ds); g.dynamic_states.push_back(ds);
return g.infos.size() - 1;
} }
void engine::remove_object(group_handle group, std::size_t index) void engine::remove_object(group_handle group, std::size_t index)
@ -707,6 +701,18 @@ namespace psemek::phys2d
g.dynamic_states.erase(g.dynamic_states.begin() + index); g.dynamic_states.erase(g.dynamic_states.begin() + index);
} }
engine::object_info const & engine::info(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) void engine::set_gravity(geom::vector<float, 2> const & g)
{ {
impl().gravity = g; impl().gravity = g;