World¶
-
class Kilosim::World¶
The
World
provides the base environment for running simulations. It represents a two-dimensional bounded arena for simulating Kilobots.The user can configure the World dimensions and the monochrome light intensity at every point in the World (with a
LightPattern
), and addRobot
s to the World to simulate.After initial configuration, the simulation is run simply by invoking the
step()
method, which processes a single time step (1/32 second, by default) for allRobot
s.References to a
World
are used by both theLogger
(to save information about theRobot
s during simulation) and theViewer
(to visualize the simulation).It manages fixed-step simulated control over all
Robot
s placed in it.Public Functions
-
World(const double arena_width, const double arena_height, const std::string light_pattern_src = "", const uint32_t num_threads = 0)¶
Construct a world of a fixed size with the background light pattern
- Parameters
arena_width – Width of the rectangular World/arena in mm
arena_height – Height of the rectangular World/arena in mm
light_pattern_src – Name of image file of the light pattern to use. Aspect ratio should match the arena, but no specific resolution is mandated. If no light_pattern_src is provided (empty string), the background will be black.
num_threads – How many threads to parallelize the simulation over. If set to 0 (default), dynamic threading will be used.
-
void step()¶
Run a step of the simulator. This runs the controllers, communication, pseudo-physics, and movement for all Robots. It also increments the tick time.
This is what you should call in your main function to run the simulation.
-
bool has_light_pattern() const¶
Check whether the World has a light pattern image set
- Returns
Whether LightPattern has an image source
-
void set_light_pattern(const std::string light_img_src)¶
Set the world’s light pattern using the given image file. The image type must be supported by SFML’s Image::loadFromFile
- Parameters
light_img_src – Name and location of the image file to use for light pattern
-
void add_robot(Robot *robot)¶
Add a robot to the world by its pointer.
Warning
It is possible right now to add a Robot twice, so be careful.
-
void remove_robot(Robot *robot)¶
Remove a robot from the world by its pointer
TODO:
remove_robot
is currently unimplemented…Warning
…I haven’t implemented this yet.
-
uint16_t get_tick_rate() const¶
Get the tick rate (should be 32 by default, if not changed by user).
- Returns
Number of simulation ticks per second of real-world (wall clock) time
-
void set_tick_rate(const uint16_t tick_rate)¶
Set the tick rate of the simulation (how many ticks per second are simulated).
- Parameters
tick_rate – Number of simulation ticks per second of real-world (wall clock) time
-
uint32_t get_tick() const¶
Get the current tick of the simulation (only set by simulator)
- Returns
Number of ticks since simulation started
-
double get_time() const¶
Get the current computed time in seconds (from tick and tickRate)
- Returns
Time since simulation started (in seconds)
-
std::vector<Robot*> &get_robots()¶
Get a reference to a vector of pointers to all robots in the world. This is useful for Logger and Viewer functions, but you likely won’t need to use it as an end-user.
- Returns
All the Robots added to the world
-
std::vector<double> get_dimensions() const¶
Get the dimensions of the world (in mm)
- Returns
2-element [width, height] vector of dimensions in mm
-
void check_validity() const¶
Check that the world is in a valid state. (i.e., all
Robots
in the World are within the arena bounds, and none are overlapping).Note
If you want to allow intersections, you can just not call this.
- Throws
std::runtime_error – if any robots are out of bounds or overlapping
-
World(const double arena_width, const double arena_height, const std::string light_pattern_src = "", const uint32_t num_threads = 0)¶