[go: up one dir, main page]

Menu

[8286ad]: / world / World.hpp  Maximize  Restore  History

Download this file

148 lines (123 with data), 4.5 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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Handles interactions between a car and its environment.
//
// Copyright (C) 2001-2019 Sam Varner
//
// This file is part of Vamos Automotive Simulator.
//
// Vamos is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vamos is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vamos. If not, see <http://www.gnu.org/licenses/>.
#ifndef _WORLD_H_
#define _WORLD_H_
#include "../geometry/Material.hpp"
#include "../geometry/Three_Matrix.hpp"
#include "../geometry/Three_Vector.hpp"
#include "Timing_Info.hpp"
#include <boost/circular_buffer.hpp>
#include <memory>
#include <string>
#include <vector>
namespace Vamos_Body { class Car; }
namespace Vamos_Geometry { class Three_Vector; }
namespace Vamos_Track
{
class Road;
class Strip_Track;
}
namespace Vamos_World
{
class Driver;
struct Atmosphere
{
double density;
Vamos_Geometry::Three_Vector velocity;
};
struct Car_Info
{
Car_Info(Vamos_Body::Car* car_in, Driver* driver_in);
void reset();
void propagate(double time_step, double total_time,
const Vamos_Geometry::Three_Vector& track_position,
const Vamos_Geometry::Three_Vector& pointer_position);
const Vamos_Geometry::Three_Vector& track_position() const;
size_t road_index = 0;
size_t segment_index = 0;
Vamos_Body::Car* car;
Driver* driver;
Vamos_Geometry::Three_Vector m_pointer_position;
struct Record
{
Record(){};
Record(double time, Vamos_Body::Car* car,
const Vamos_Geometry::Three_Vector& track_position);
double m_time;
Vamos_Geometry::Three_Vector m_track_position;
Vamos_Geometry::Three_Vector m_position;
Vamos_Geometry::Three_Matrix m_orientation;
};
boost::circular_buffer<Record> m_record;
};
struct Interaction_Info
{
Vamos_Body::Car* car;
Vamos_Geometry::Material::Material_Type car_material;
Vamos_Geometry::Material::Material_Type track_material;
double parallel_speed;
double perpendicular_speed;
};
class Track;
class World
{
friend class World_Reader;
public:
World(Vamos_Track::Strip_Track& track, const Atmosphere& atmosphere);
virtual ~World() = default;
virtual void add_car(Vamos_Body::Car& car, Driver& driver);
virtual void set_focused_car(size_t car_index);
double get_gravity() const { return m_gravity; }
void cars_can_interact(bool can) { m_cars_can_interact = can; }
void propagate_cars(double time_step, std::vector<Interaction_Info>& v_inter_info);
void write_results(const std::string& file) const;
protected:
virtual void start(bool qualifying, size_t laps_or_minutes);
void reset();
void restart();
Car_Info* focused_car();
Car_Info* controlled_car();
const Car_Timing& focused_timing() const;
double wind_speed() const;
void focus_other_car(int delta);
std::vector<Car_Info> m_cars;
Vamos_Track::Strip_Track& m_track;
/// The times for all cars.
std::unique_ptr<Timing_Info> mp_timing;
private:
void gravity(double g);
void interact(Vamos_Body::Car* car, size_t road_index, size_t segment_index,
std::vector<Interaction_Info>& v_inter_info);
void collide(Car_Info* car1_info, Car_Info* car2_info,
std::vector<Interaction_Info>& v_inter_info);
void place_car(Vamos_Body::Car* car, const Vamos_Geometry::Three_Vector& pos,
const Vamos_Track::Road& Road);
void set_controlled_car(size_t car_index);
/// @return The fraction of air density at car1 due to the slipstream of car2.
double slipstream_air_density_factor(Car_Info& car1, Car_Info& car2);
//!! Can gravity be handled as an interaction rather than passing to each car?
double m_gravity = 9.8;
// Used to get the focused car's car info and timing info.
size_t m_focused_car_index = 0;
bool m_cars_can_interact = true;
std::optional<size_t> m_controlled_car_index;
Atmosphere m_atmosphere;
};
} // namespace Vamos_World
#endif // not _WORLD_H_