18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-05 00:55:17 +01:00
Files
maszyna/world/worldgeometry.cpp
T
maj00r 6ab8ce52c6 feat(physics): the gradient of the track against the vertical at the vehicle, the body of the world at run time
Step 5 of docs/world-format-eu07.md (section 6): the physics takes the vertical from the body the world is set on,
not from the y axis. world_geometry::current_body() is the plane unless a world on a curved body is loaded (none
is yet), and track_gradient() gives the gradient under a vehicle: on the plane exactly -vFront.y, as the physics
has always had it, on a curved body minus the share of vFront along the normal of the ellipsoid at the vehicle.
One vertical per segment wouldn't do: it turns by 0.157 mrad per kilometre, 0.16 per mille at the ends of a
segment of 2 km. TDynamicObject::updateTrackParameters takes the gradient from it.

The cant, the height of the wire, the camera and the particles need the same vertical; they come from the
geometry of the track and of the image, and follow with the sphere in the renderer (step 4).

Checked in the game (--test 120 s, seed 1): the physics logs of td.scn and Glinojeck.scn the same bit for bit as
before. Tests: the gradient on the plane is -front.y to the bit, on the Earth 1 per mille against the vertical
at the equator, level against the local vertical 1 km north where the vertical of the start would show 0.157
per mille.
2026-12-03 02:48:08 +01:00

160 lines
4.7 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "world/worldgeometry.h"
#include <cmath>
#include <istream>
#include <ostream>
#include "scene/sn_utils.h"
namespace world_geometry {
namespace {
// WGS84: the semi-major axis and the flattening, as the standard defines them
constexpr double wgs84_a{6'378'137.0};
constexpr double wgs84_flattening{1.0 / 298.257223563};
constexpr double wgs84_b{wgs84_a * (1.0 - wgs84_flattening)};
constexpr double micrometres_in_metre{static_cast<double>(micrometres_per_metre)};
// enough for the latitude to settle to the last bit anywhere on the Earth; it settles in 3 to 4 steps
constexpr int geodetic_iterations{12};
body &current_body_storage() {
static body storage{body::plane()};
return storage;
}
} // namespace
std::int64_t micrometres(double const Metres) {
return std::llround(Metres * micrometres_in_metre);
}
double metres(std::int64_t const Micrometres) {
// an int64 below 2^53 converts exactly, and the division rounds correctly: the nearest double to the decimal value
return static_cast<double>(Micrometres) / micrometres_in_metre;
}
position from_metres(glm::dvec3 const &Metres) {
return {micrometres(Metres.x), micrometres(Metres.y), micrometres(Metres.z)};
}
glm::dvec3 to_metres(position const &Position) {
return {metres(Position.x), metres(Position.y), metres(Position.z)};
}
body body::earth() {
return {body_kind::earth, wgs84_a, wgs84_b};
}
body body::plane() {
return {body_kind::plane, 0.0, 0.0};
}
body body::fictional_planet() {
return {body_kind::fictional_planet, wgs84_a, wgs84_b};
}
body body::fictional_planet(double const Radius) {
return {body_kind::fictional_planet, Radius, Radius};
}
glm::dvec3 body::up(glm::dvec3 const &Point) const {
if (!curved())
return {0.0, 1.0, 0.0};
auto const a2{a * a};
auto const b2{b * b};
glm::dvec3 const normal{Point.x / a2, Point.y / a2, Point.z / b2};
auto const length{std::sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z)};
return normal / length;
}
glm::dvec3 body::surface_along(glm::dvec3 const &Direction) const {
auto const scale{1.0 / std::sqrt((Direction.x * Direction.x + Direction.y * Direction.y) / (a * a) + Direction.z * Direction.z / (b * b))};
return Direction * scale;
}
double body::eccentricity_squared() const {
return 1.0 - (b * b) / (a * a);
}
void write(std::ostream &Output, body const &Body) {
sn_utils::s_uint8(Output, static_cast<std::uint8_t>(Body.kind));
sn_utils::ls_float64(Output, Body.a);
sn_utils::ls_float64(Output, Body.b);
}
bool read(std::istream &Input, body &Body) {
auto const kind{sn_utils::d_uint8(Input)};
auto const a{sn_utils::ld_float64(Input)};
auto const b{sn_utils::ld_float64(Input)};
if (!Input)
return false;
if (kind == static_cast<std::uint8_t>(body_kind::plane)) {
if (a != 0.0 || b != 0.0)
return false;
}
else if (kind > static_cast<std::uint8_t>(body_kind::plane) || !(b > 0.0) || b > a || !std::isfinite(a)) {
return false;
}
Body = {static_cast<body_kind>(kind), a, b};
return true;
}
body const &current_body() {
return current_body_storage();
}
void set_current_body(body const &Body) {
current_body_storage() = Body;
}
double track_gradient(body const &Body, glm::dvec3 const &Front, glm::dvec3 const &Position) {
if (!Body.curved())
return -Front.y;
return -glm::dot(Front, Body.up(Position));
}
glm::dvec3 to_cartesian(body const &Body, geodetic const &Point) {
auto const e2{Body.eccentricity_squared()};
auto const sine{std::sin(Point.latitude)};
auto const cosine{std::cos(Point.latitude)};
auto const n{Body.a / std::sqrt(1.0 - e2 * sine * sine)};
return {
(n + Point.height) * cosine * std::cos(Point.longitude),
(n + Point.height) * cosine * std::sin(Point.longitude),
(n * (1.0 - e2) + Point.height) * sine};
}
geodetic to_geodetic(body const &Body, glm::dvec3 const &Point) {
auto const e2{Body.eccentricity_squared()};
auto const p{std::hypot(Point.x, Point.y)};
geodetic result;
result.longitude = std::atan2(Point.y, Point.x);
auto latitude{std::atan2(Point.z, p * (1.0 - e2))};
for (int step = 0; step < geodetic_iterations; ++step) {
auto const sine{std::sin(latitude)};
auto const n{Body.a / std::sqrt(1.0 - e2 * sine * sine)};
latitude = std::atan2(Point.z + e2 * n * sine, p);
}
auto const sine{std::sin(latitude)};
auto const n{Body.a / std::sqrt(1.0 - e2 * sine * sine)};
result.latitude = latitude;
// valid at the poles too, unlike p / cos(latitude) - n
result.height = p * std::cos(latitude) + Point.z * sine - Body.a * Body.a / n;
return result;
}
} // namespace world_geometry