18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 11:15:23 +01:00
Files
maszyna/tests/unit/test_bones.cpp
T
maj00r 5e00999cb8 feat(vehicle): bones of the vehicle models: the rods of a steam locomotive, couplers and hanging hoses
A vehicle's .mmd can pin submodels of its model to points that move and turn them to keep facing another point
(vehicle/bones.h, TDynamicObject::update_bones(), run with the wheels):

- bonepoint: fixed, crank (going round with a wheel submodel of animwheelprefix:), slide (a crosshead on its
  guide keeping the length of the connecting rod), coupler (the next vehicle's hook, following it round a curve,
  over uneven track and with the buffers pressed in), mid (halfway between two points);
- bonechain: a chain hanging between two points, its nodes swinging with gravity and inertia (Verlet
  integration in the world frame, substeps of 1/240 s at most), so a hose made of pieces swings when the vehicle
  jerks or rocks and sags as the buffers close up;
- bone: a submodel moved with its anchor point and turned about it to face another point, or only moved.

The submodels are posed with TSubModel::SetTranslate() and SetRotateXYZ(), in their own frame whatever their
origin and axes, and get their future_transform for the motion blur. A model without these entries is drawn as
before; the old entries animpistonprefix:, animconrodprefix:, animreturnprefix: and pistonfactors: still do
nothing. bones_doc.md describes them, with examples.

Unit tests (tests/unit/test_bones.cpp): the crank and the crosshead (the stroke twice the crank), the connecting
rod keeping both its ends for a submodel turned in its frame, the angles giving what TSubModel::RaAnimation()
does, the coupler, the hose meeting point, a chain hanging and swinging after a jerk, and the .mmd errors.

Game data: the placeholder Ty2 of dynamic/pkp/ty2_v1 (SVN working copy, not committed) has its rods and its brake
hose to the tender as bones in ty2.mmd. The EU07 (dynamic/pkp/4e_v1/4e.mmd) could take the six coupler lines of
bones_doc.md, which were tried on a copy in a test folder; the data in the SVN isn't changed.
2026-11-24 21:57:23 +01:00

240 lines
11 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include <numbers>
#include "vehicle/bones.h"
// the bones of the vehicle models (vehicle/bones.h): the rods of a steam locomotive pinned to the cranks of its
// wheels and to the crossheads, and the couplers following the next vehicle. the figures are those of the rods of the
// Ty2: a crank of 330 mm, a connecting rod of about 3 m
namespace {
double const radius{0.33};
glm::dvec3 const axle{0.0, 0.0, 0.7};
bones::rig rods() {
bones::rig rig;
REQUIRE(bones::add_point(rig, {"pin", "crank", "wheel3", "-1.2", "0.0", "1.03"}).empty());
REQUIRE(bones::add_point(rig, {"cross", "slide", "pin", "-1.2", "3.0", "0.7", "0", "1", "0"}).empty());
REQUIRE(bones::add_bone(rig, {"korbowod", "pin", "cross"}).empty());
REQUIRE(bones::add_bone(rig, {"wiazar", "pin", "none"}).empty());
REQUIRE(bones::add_bone(rig, {"wodzik", "cross", "none"}).empty());
return rig;
}
bones::drive turned(bones::rig const &Rig, double Angle) {
bones::drive drive;
drive.wheels.resize(Rig.points.size());
drive.couplers.resize(Rig.points.size());
drive.wheels[0] = {glm::dvec3{-1.2, 0.0, 0.7}, glm::dvec3{1.0, 0.0, 0.0}, Angle};
return drive;
}
// where the point of the submodel, given as it stands in the model, goes with the animation
glm::dvec3 animated(bones::animation const &Animation, glm::dvec3 Origin, glm::dmat3 Rotation, glm::dvec3 Point) {
auto const local{glm::transpose(Rotation) * (Point - Origin)};
return Origin + Rotation * glm::dvec3(glm::dmat4(Animation.matrix) * glm::dvec4(local, 1.0));
}
} // namespace
TEST_CASE("bones: a crank pin goes round with its wheel, the crosshead slides keeping the rod's length") {
auto const rig{rods()};
auto const length{glm::length(rig.points[1].rest - rig.points[0].rest)};
auto lowest{1e9};
auto highest{-1e9};
for (auto angle{0.0}; angle < 360.0; angle += 5.0) {
CAPTURE(angle);
auto const points{bones::pose(rig, turned(rig, angle))};
// the pin on its circle
CHECK(glm::length(points[0] - glm::dvec3{-1.2, 0.0, 0.7}) == doctest::Approx(radius));
// the crosshead on its guide, the rod's length kept
CHECK(points[1].x == doctest::Approx(-1.2));
CHECK(points[1].z == doctest::Approx(0.7));
CHECK(glm::length(points[1] - points[0]) == doctest::Approx(length));
lowest = std::min(lowest, points[1].y);
highest = std::max(highest, points[1].y);
}
// the stroke of the piston: twice the crank
CHECK(highest - lowest == doctest::Approx(2.0 * radius).epsilon(0.01));
// a quarter turn about x takes the pin from over the axle to behind it
auto const quarter{bones::crank(glm::dvec3{0.0, 0.0, 1.0}, {glm::dvec3{0.0}, glm::dvec3{1.0, 0.0, 0.0}, 90.0})};
CHECK(quarter.y == doctest::Approx(-1.0));
CHECK(quarter.z == doctest::Approx(0.0).scale(1.0));
}
TEST_CASE("bones: the connecting rod keeps its ends on the pin and the crosshead, whatever its submodel's frame") {
auto const rig{rods()};
// the submodel with its origin in the middle of the rod, turned half round about z the way some models are made
glm::dvec3 const origin{-1.2, 1.5, 0.86};
auto const rotation{glm::dmat3(glm::rotate(glm::dmat4(1.0), std::numbers::pi, glm::dvec3{0.0, 0.0, 1.0}))};
auto const pinrest{rig.points[0].rest};
auto const crossrest{rig.points[1].rest};
for (auto angle : {0.0, 37.0, 90.0, 180.0, 271.0}) {
CAPTURE(angle);
auto const points{bones::pose(rig, turned(rig, angle))};
auto const motion{bones::bone_motion(pinrest, points[0], crossrest, points[1])};
auto const animation{bones::localise(motion, origin, rotation)};
auto const pin{animated(animation, origin, rotation, pinrest)};
auto const cross{animated(animation, origin, rotation, crossrest)};
CHECK(glm::length(pin - points[0]) == doctest::Approx(0.0).scale(1.0));
CHECK(glm::length(cross - points[1]) == doctest::Approx(0.0).scale(1.0));
// the angles do what TSubModel::RaAnimation() does with them after the translation
auto matrix{glm::translate(glm::mat4(1.f), animation.translation)};
matrix = glm::rotate(matrix, glm::radians(animation.angles.x), glm::vec3(1.f, 0.f, 0.f));
matrix = glm::rotate(matrix, glm::radians(animation.angles.y), glm::vec3(0.f, 1.f, 0.f));
matrix = glm::rotate(matrix, glm::radians(animation.angles.z), glm::vec3(0.f, 0.f, 1.f));
for (auto column{0}; column < 4; ++column) {
for (auto row{0}; row < 4; ++row) {
CHECK(matrix[column][row] == doctest::Approx(animation.matrix[column][row]).scale(1.0).epsilon(1e-4));
}
}
}
}
TEST_CASE("bones: a coupling rod moves with its pin without turning") {
auto const rig{rods()};
auto const points{bones::pose(rig, turned(rig, 120.0))};
auto const motion{bones::bone_motion(rig.points[0].rest, points[0], std::nullopt, std::nullopt)};
auto const animation{bones::localise(motion, glm::dvec3{-1.2, -1.65, 1.03}, glm::dmat3{1.0})};
CHECK(animation.angles.x == doctest::Approx(0.0).scale(1.0));
CHECK(glm::length(glm::dvec3(animation.translation) - (points[0] - rig.points[0].rest)) == doctest::Approx(0.0).scale(1.0));
}
TEST_CASE("bones: a coupler faces the coupler of the next vehicle as it moves") {
bones::rig rig;
REQUIRE(bones::add_point(rig, {"hinge", "fixed", "0", "6.2", "1.06"}).empty());
REQUIRE(bones::add_point(rig, {"hook", "coupler", "front", "0", "6.9", "1.06"}).empty());
REQUIRE(bones::add_bone(rig, {"sprzeg", "hinge", "hook"}).empty());
bones::drive drive;
drive.wheels.resize(2);
drive.couplers = {std::nullopt, glm::dvec3{0.2, -0.05, 0.0}};
auto const points{bones::pose(rig, drive)};
CHECK(points[1].x == doctest::Approx(0.2));
auto const motion{bones::bone_motion(rig.points[0].rest, points[0], rig.points[1].rest, points[1])};
auto const facing{motion.rotation * (rig.points[1].rest - rig.points[0].rest)};
CHECK(glm::length(glm::normalize(facing) - glm::normalize(points[1] - points[0])) == doctest::Approx(0.0).scale(1.0));
// not coupled: as made
drive.couplers[1] = std::nullopt;
CHECK(bones::pose(rig, drive)[1] == rig.points[1].rest);
}
TEST_CASE("bones: what the .mmd can get wrong") {
bones::rig rig;
CHECK_FALSE(bones::add_point(rig, {"p", "wobble", "1", "2", "3"}).empty());
CHECK_FALSE(bones::add_point(rig, {"p", "fixed", "1", "2"}).empty());
CHECK_FALSE(bones::add_point(rig, {"p", "fixed", "1", "x", "3"}).empty());
// a slide driven by a point defined later
CHECK_FALSE(bones::add_point(rig, {"c", "slide", "pin", "0", "3", "0.7", "0", "1", "0"}).empty());
CHECK_FALSE(bones::add_point(rig, {"c", "coupler", "middle", "0", "3", "0.7"}).empty());
CHECK(bones::add_point(rig, {"p", "fixed", "1", "2", "3"}).empty());
CHECK_FALSE(bones::add_point(rig, {"p", "fixed", "1", "2", "3"}).empty());
CHECK_FALSE(bones::add_bone(rig, {"rod", "q", "none"}).empty());
CHECK_FALSE(bones::add_bone(rig, {"rod", "p", "q"}).empty());
CHECK(bones::add_bone(rig, {"rod", "p", "none"}).empty());
CHECK(rig.points.size() == 1);
CHECK(rig.bones.size() == 1);
}
namespace {
// a hose of 1.3 m hanging between two points 1 m apart, in the frame of the model file (z up)
bones::rig hose() {
bones::rig rig;
REQUIRE(bones::add_point(rig, {"kurek", "fixed", "0", "0", "0"}).empty());
REQUIRE(bones::add_point(rig, {"zlaczka", "fixed", "1", "0", "0"}).empty());
REQUIRE(bones::add_chain(rig, {"waz", "kurek", "zlaczka", "3", "0.25", "0", "-0.25", "0.5", "0", "-0.35", "0.75", "0", "-0.25"}).empty());
return rig;
}
bones::chain_physics downwards() {
bones::chain_physics physics;
physics.gravity = glm::dvec3{0.0, 0.0, -9.81};
return physics;
}
std::vector<glm::dvec3> rests(bones::rig const &Rig) {
std::vector<glm::dvec3> nodes;
for (auto const node : Rig.chains[0].nodes) {
nodes.push_back(Rig.points[node].rest);
}
return nodes;
}
void settle(bones::rig const &Rig, bones::chain_state &State, glm::dvec3 Start, glm::dvec3 End, double Seconds) {
for (auto time{0.0}; time < Seconds; time += 0.02) {
bones::simulate(Rig.chains[0].lengths, State, Start, End, 0.02, downwards());
}
}
} // namespace
TEST_CASE("bones: a chain hangs between its ends, its pieces keeping their lengths") {
auto const rig{hose()};
REQUIRE(rig.chains.size() == 1);
CHECK(rig.find("waz.2") >= 0);
CHECK(rig.chains[0].lengths.size() == 4);
bones::chain_state state;
glm::dvec3 const start{0.0};
glm::dvec3 const end{1.0, 0.0, 0.0};
bones::start_chain(state, rests(rig), start, end);
settle(rig, state, start, end, 20.0);
std::vector<glm::dvec3> points{start};
points.insert(points.end(), state.now.begin(), state.now.end());
points.push_back(end);
for (std::size_t piece{0}; piece < rig.chains[0].lengths.size(); ++piece) {
CAPTURE(piece);
CHECK(glm::length(points[piece + 1] - points[piece]) == doctest::Approx(rig.chains[0].lengths[piece]).epsilon(0.01));
}
// hanging below its ends, the middle the lowest, and at rest
CHECK(state.now[1].z < state.now[0].z);
CHECK(state.now[1].z < -0.3);
CHECK(state.now[1].x == doctest::Approx(0.5).epsilon(0.01));
CHECK(glm::length(state.now[1] - state.before[1]) < 1e-5);
}
TEST_CASE("bones: a chain jerked by its ends swings after them and settles") {
auto const rig{hose()};
bones::chain_state state;
glm::dvec3 const start{0.0};
glm::dvec3 const end{1.0, 0.0, 0.0};
bones::start_chain(state, rests(rig), start, end);
settle(rig, state, start, end, 20.0);
auto const hanging{state.now[1]};
// both ends 0.4 m on in a tenth of a second, as the vehicle jerked by its coupler
glm::dvec3 const shift{0.0, 0.4, 0.0};
bones::simulate(rig.chains[0].lengths, state, start + shift, end + shift, 0.1, downwards());
// the middle lags behind, swinging
CHECK(state.now[1].y - hanging.y < 0.35);
auto farthest{0.0};
for (auto time{0.0}; time < 2.0; time += 0.02) {
bones::simulate(rig.chains[0].lengths, state, start + shift, end + shift, 0.02, downwards());
farthest = std::max(farthest, state.now[1].y - hanging.y);
}
// and overshoots the ends before it comes back
CHECK(farthest > 0.4);
settle(rig, state, start + shift, end + shift, 20.0);
CHECK(state.now[1].y - hanging.y == doctest::Approx(0.4).epsilon(0.01));
}
TEST_CASE("bones: the hoses of two vehicles meet halfway between their cocks") {
bones::rig rig;
REQUIRE(bones::add_point(rig, {"kurek", "fixed", "0.45", "7.6", "0.95"}).empty());
REQUIRE(bones::add_point(rig, {"kurek_sasiada", "coupler", "front", "0.45", "8.3", "0.95"}).empty());
REQUIRE(bones::add_point(rig, {"spotkanie", "mid", "kurek", "kurek_sasiada"}).empty());
CHECK(rig.points[2].rest.y == doctest::Approx(7.95));
bones::drive drive;
drive.wheels.resize(3);
drive.couplers = {std::nullopt, glm::dvec3{0.1, -0.2, 0.0}, std::nullopt};
auto const points{bones::pose(rig, drive)};
CHECK(points[2].x == doctest::Approx(0.5));
CHECK(points[2].y == doctest::Approx(7.85));
// a chain's node doesn't drive other points
REQUIRE(bones::add_chain(rig, {"waz", "kurek", "spotkanie", "1", "0.45", "7.8", "0.6"}).empty());
CHECK_FALSE(bones::add_point(rig, {"x", "mid", "kurek", "waz.1"}).empty());
CHECK_FALSE(bones::add_chain(rig, {"waz2", "kurek", "nic", "1", "0", "0", "0"}).empty());
CHECK_FALSE(bones::add_chain(rig, {"waz3", "kurek", "spotkanie", "2", "0", "0", "0"}).empty());
}