17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-19 00:19:19 +02:00
Files
maszyna/tests/unit/test_horn.cpp
jerrrrycho 00934a17ed fix(physics): automatic couplers don't catch above the safe collision speed (#86)
Two vehicles with automatic couplers coupled the moment they touched, in CouplerForce(), before
CollisionDetect() ran; its derail check only covers uncoupled vehicles, so they never collided nor
derailed, at any speed. With crash damage on, the coupling now waits for a closing speed of at most
15 km/h, the limit of the derail check. A faster hit locks the automatic coupling until the vehicles
part, so the collision runs its course instead of the couplers catching once it has slowed them down.

Two collision bugs on the way:
- CollisionVelocity() took the length of the difference of the heading quaternions times the speeds;
  a quaternion turns by half its angle, so for vehicles facing each other the speeds came out at right
  angles: a head-on hit at 10 + 10 km/h read as 14.1 km/h, under the limit. The closing speed is now
  taken from the touching ends, as CouplerForce() relates the vehicles: the sum of the speeds head-on,
  the difference rear-end, negative when the ends part,
- CollisionDetect() gave ComputeCollision() the other vehicle's speed along that vehicle's own axis,
  which points the other way for vehicles facing each other: a wagon hit head-on counted as running
  into the hitter, and the hitter bounced back as from a wall (20 km/h into a standing wagon: it ran
  off at 6 km/h instead of 17). The speed is turned into this vehicle's frame and back.

Head-on hits now count with both speeds, so with crash damage on they derail sooner than before, at
the 15 km/h the check was meant for.

Tests: test::pair runs two test vehicles on one track the way vehicle_table::update() runs a scene
(neighbours, forces, movement), with coupling, buffing, collisions and derailing coming from the
physics. tests/unit/test_couplers.cpp: catching up to 15 km/h and not above; a slow hit couples, a fast
one doesn't, nor once slowed down, the lock resets once the vehicles part; a hit a little over 15 km/h
bounces apart on the rails, harder ones derail; head-on 7 + 7 couples, 8 + 8 bounces, 15 + 15 derails;
no catching a coupler of another type, couplers of different control systems couple without the
control connection.
2026-10-17 13:49:05 +02:00

40 lines
1.3 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "vehicle/horn.h"
// the horn keys of the cab: low tone (A) and high tone (shift+A), held to sound
TEST_CASE("horn: a single lever for both tones sounds one tone at a time (eu07/maszyna#392)") {
auto signal{0};
signal = horn::pressed(signal, horn::low, true);
REQUIRE(signal == horn::low);
// the lever moved over to the high tone while the low tone key is still held
signal = horn::pressed(signal, horn::high, true);
CHECK(signal == horn::high);
// letting go of the low tone key leaves the high tone sounding
signal = horn::released(signal, horn::low);
CHECK(signal == horn::high);
signal = horn::released(signal, horn::high);
CHECK(signal == 0);
}
TEST_CASE("horn: separate buttons for the tones can sound both at once") {
auto signal{0};
signal = horn::pressed(signal, horn::low, false);
signal = horn::pressed(signal, horn::high, false);
CHECK(signal == (horn::low | horn::high));
signal = horn::released(signal, horn::low);
CHECK(signal == horn::high);
}
TEST_CASE("horn: releasing a tone leaves the other bits of the signal alone") {
auto const third{4}; // the third horn, operated separately
CHECK(horn::released(horn::low | third, horn::low) == third);
CHECK(horn::pressed(third, horn::high, true) == (horn::high | third));
}