-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcustom_roll_model.cpp
More file actions
57 lines (48 loc) · 1.9 KB
/
custom_roll_model.cpp
File metadata and controls
57 lines (48 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Minimal custom RollModel: linear deceleration, no slope handling.
// Apply constant deceleration along velocity direction, integrate
// semi-implicit Euler, stop below a fixed velocity threshold.
#include "FlightSimulator.hpp"
#include "RollModel.hpp"
#include "ground_surface.hpp"
#include "math_utils.hpp"
#include <cmath>
#include <cstdio>
#include <memory>
class LinearDecelRollModel : public RollModel
{
public:
static constexpr float STOP_VELOCITY = 0.1F; // ft/s
static constexpr float DECEL = 6.0F; // ft/s^2
RollResult step(const RollState &s,
const GroundSurface & /*surface*/) const override
{
const float speed = math_utils::magnitude(s.velocity);
if (speed < STOP_VELOCITY)
{
return {s.position, {0.0F, 0.0F, 0.0F}, s.spinVector, true};
}
const float dv = DECEL * s.dt;
const float scale = (dv >= speed) ? 0.0F : (speed - dv) / speed;
const Vector3D vNew{s.velocity[0] * scale,
s.velocity[1] * scale,
s.velocity[2] * scale};
const Vector3D pNew{s.position[0] + vNew[0] * s.dt,
s.position[1] + vNew[1] * s.dt,
s.position[2] + vNew[2] * s.dt};
const bool atRest = math_utils::magnitude(vNew) < STOP_VELOCITY;
return {pNew, vNew, s.spinVector, atRest};
}
};
int main()
{
const LaunchData launch{160.0F, 11.0F, 0.0F, 3000.0F, 0.0F};
const AtmosphericData atmos{70.0F, 0.0F, 0.0F, 0.0F, 0.0F, 50.0F, 29.92F};
GroundSurface ground;
auto roll = std::make_shared<LinearDecelRollModel>();
FlightSimulator sim(launch, atmos, ground,
/*aero*/ nullptr, /*bounce*/ nullptr, roll);
sim.run();
const LandingResult r = sim.getLandingResult();
std::printf("Landing: %.1f %.1f %.1f yards\n", r.xF, r.yF, r.zF);
return 0;
}