louis-langholtz / PlayRho

An interactive physics engine & library.
zlib License
133 stars 24 forks source link

Pass values from the ShapeConf to other (sub)shape configs #545

Closed opera-aberglund closed 11 months ago

opera-aberglund commented 11 months ago

Desired Behavior:

When creating multiple new shapes at once, it would be nice to be able to set the common attributes first that are derived from the BaseShapeConfig class (like friction, restitution ...) by creating an instance of the ShapeConf class. Then, use that config when creating the other shape configs (like ChainShapeConf, EdgeShapeConf ...).

Actual Behavior:

I have to call .UseFriction, .UseRestitution on each and every different shape, which creates a lot of code duplication when I want them to share the same configuration for these attributes.

louis-langholtz commented 11 months ago

Have you considered using code like the following to copy base members?

auto poly = PolygonShapeConf{hx, hy};
auto chain = ChainShapeConf{};
const auto copyBaseShapeConf = static_cast<const BaseShapeConf&>(poly);
static_cast<BaseShapeConf&>(chain) = copyBaseShapeConf;

I believe this would work at least for those members without invoking undefined behavior.

opera-aberglund commented 11 months ago

Have you considered using code like the following to copy base members?

auto poly = PolygonShapeConf{hx, hy};
auto chain = ChainShapeConf{};
const auto copyBaseShapeConf = static_cast<const BaseShapeConf&>(poly);
static_cast<BaseShapeConf&>(chain) = copyBaseShapeConf;

I believe this would work at least for those members without invoking undefined behavior.

This did work! I never thought of that trick to copy the base values, thanks!