Pogosim
Loading...
Searching...
No Matches
objects.h
Go to the documentation of this file.
1#ifndef OBJECTS_H
2#define OBJECTS_H
3
4#include <functional>
5
6#include "utils.h"
7#include "configuration.h"
8#include "render.h"
9#include "colormaps.h"
10#include "objects_geometry.h"
11
12
13class Simulation;
14class LightLevelMap;
15
16
21class Object {
22public:
31 Object(float _x, float _y, ObjectGeometry& _geom, std::string const& _category = "objects");
32
42 Object(Simulation* simulation, float _x, float _y, Configuration const& config, std::string const& _category = "objects");
43
47 virtual ~Object();
48
49
56 virtual void render(SDL_Renderer* renderer, b2WorldId world_id) const = 0;
57
63 virtual void launch_user_step(float f);
64
69
77 virtual void move(float x, float y, float theta = NAN);
78
82 virtual bool is_tangible() const { return false; };
83
95 virtual arena_polygons_t generate_contours(std::size_t points_per_contour = 0) const;
96
97 // Physical information
98 float x;
99 float y;
100 float theta;
101
102 // Base information
103 std::string category;
104
105protected:
111 virtual void parse_configuration(Configuration const& config, Simulation* simulation);
112
114};
115
116
121class PhysicalObject : public Object {
122public:
123
139 PhysicalObject(uint16_t _id, float _x, float _y,
140 ObjectGeometry& geom, b2WorldId world_id,
141 float _linear_damping = 0.0f, float _angular_damping = 0.0f,
142 float _density = 10.0f, float _friction = 0.3f, float _restitution = 0.5f,
143 std::string const& _category = "objects");
144
156 PhysicalObject(Simulation* simulation, uint16_t _id, float _x, float _y,
157 b2WorldId world_id, Configuration const& config,
158 std::string const& _category = "objects");
159
164 virtual void launch_user_step(float t) override;
165
173 virtual b2Vec2 get_position() const;
174
182 float get_angle() const;
183
189 float get_angular_velocity() const;
190
196 b2Vec2 get_linear_acceleration() const;
197
204 virtual void render(SDL_Renderer* renderer, b2WorldId world_id) const override = 0 ;
205
213 virtual void move(float x, float y, float theta = NAN) override;
214
218 virtual bool is_tangible() const override { return true; };
219
231 virtual arena_polygons_t generate_contours(std::size_t points_per_contour = 0) const override;
232
233 // Base info
234 uint16_t id;
235
236
237protected:
243 virtual void parse_configuration(Configuration const& config, Simulation* simulation) override;
244
253 virtual void create_body(b2WorldId world_id);
254
255 // Physical information
258 float density;
259 float friction;
261 b2BodyId body_id;
262
263 // Useful to compute acceleration
264 float _estimated_dt = 0.0f;
265 float _last_time = 0.0f;
266 b2Vec2 _prev_v = {NAN, NAN};
267 b2Vec2 _lin_acc = {NAN, NAN};
268};
269
270
276public:
277
294 PassiveObject(uint16_t _id, float _x, float _y,
295 ObjectGeometry& geom, b2WorldId world_id,
296 float _linear_damping = 0.0f, float _angular_damping = 0.0f,
297 float _density = 10.0f, float _friction = 0.3f, float _restitution = 0.5f,
298 std::string _colormap = "rainbow",
299 std::string const& _category = "objects");
300
312 PassiveObject(Simulation* simulation, uint16_t _id, float _x, float _y,
313 b2WorldId world_id, Configuration const& config,
314 std::string const& _category = "objects");
315
322 void render(SDL_Renderer* renderer, b2WorldId world_id) const override;
323
324protected:
325 std::string colormap;
326
332 virtual void parse_configuration(Configuration const& config, Simulation* simulation) override;
333};
334
335
346Object* object_factory(Simulation* simulation, uint16_t id, float x, float y, b2WorldId world_id, Configuration const& config, LightLevelMap* light_map, size_t userdatasize = 0, std::string const& category = "objects");
347
357void get_cmap_val(std::string const name, uint8_t const value, uint8_t* r, uint8_t* g, uint8_t* b);
358
359#endif
360
361
362// MODELINE "{{{1
363// vim:expandtab:softtabstop=4:shiftwidth=4:fileencoding=utf-8
364// vim:foldmethod=marker
Class for managing hierarchical configuration parameters.
Definition configuration.h:64
A discretized 2D grid representing light intensities over a simulation area.
Definition lights.h:20
Geometry of an object.
Definition objects_geometry.h:40
Base class of any object contained within the simulation.
Definition objects.h:21
virtual void parse_configuration(Configuration const &config, Simulation *simulation)
Parse a provided configuration and set associated members values.
Definition objects.cpp:33
float y
Y position.
Definition objects.h:99
virtual bool is_tangible() const
Returns whether this object is tangible (e.g. collisions, etc) or not.
Definition objects.h:82
std::string category
Category of the object.
Definition objects.h:103
float x
X position.
Definition objects.h:98
virtual void move(float x, float y, float theta=NAN)
Move the object to a given coordinate.
Definition objects.cpp:41
ObjectGeometry * get_geometry()
Return the object's geometry.
Definition objects.h:68
virtual void launch_user_step(float f)
Launches the user-defined step function.
Definition objects.cpp:29
ObjectGeometry * geom
Geometry of the object.
Definition objects.h:113
virtual void render(SDL_Renderer *renderer, b2WorldId world_id) const =0
Renders the object on the given SDL renderer.
virtual ~Object()
Destructor.
Definition objects.cpp:27
float theta
Orientation (in rad).
Definition objects.h:100
virtual arena_polygons_t generate_contours(std::size_t points_per_contour=0) const
Return one or more polygonal contours that represent the geometry of the object.
Definition objects.cpp:48
Object(float _x, float _y, ObjectGeometry &_geom, std::string const &_category="objects")
Constructs an Object.
Definition objects.cpp:16
virtual void parse_configuration(Configuration const &config, Simulation *simulation) override
Parse a provided configuration and set associated members values.
Definition objects.cpp:228
PassiveObject(uint16_t _id, float _x, float _y, ObjectGeometry &geom, b2WorldId world_id, float _linear_damping=0.0f, float _angular_damping=0.0f, float _density=10.0f, float _friction=0.3f, float _restitution=0.5f, std::string _colormap="rainbow", std::string const &_category="objects")
Constructs a PassiveObject.
Definition objects.cpp:183
void render(SDL_Renderer *renderer, b2WorldId world_id) const override
Renders the object on the given SDL renderer.
Definition objects.cpp:204
std::string colormap
Definition objects.h:325
float friction
Definition objects.h:259
virtual void launch_user_step(float t) override
Launches the user-defined step function. For PhysicalObject, it is also used to compute acceleration ...
Definition objects.cpp:79
virtual b2Vec2 get_position() const
Retrieves the object's current position.
Definition objects.cpp:98
virtual void move(float x, float y, float theta=NAN) override
Move the object to a given coordinate.
Definition objects.cpp:163
float linear_damping
Definition objects.h:256
float _last_time
Definition objects.h:265
float get_angle() const
Retrieves the object's current orientation angle.
Definition objects.cpp:106
virtual void parse_configuration(Configuration const &config, Simulation *simulation) override
Parse a provided configuration and set associated members values.
Definition objects.cpp:127
b2Vec2 get_linear_acceleration() const
Retrieves the object's current angular velocity.
Definition objects.cpp:123
float restitution
Definition objects.h:260
float angular_damping
Definition objects.h:257
float density
Definition objects.h:258
b2Vec2 _lin_acc
Definition objects.h:267
b2BodyId body_id
Box2D body identifier.
Definition objects.h:261
virtual void render(SDL_Renderer *renderer, b2WorldId world_id) const override=0
Renders the object on the given SDL renderer.
b2Vec2 _prev_v
Definition objects.h:266
virtual void create_body(b2WorldId world_id)
Creates the object's physical body in the simulation.
Definition objects.cpp:136
virtual arena_polygons_t generate_contours(std::size_t points_per_contour=0) const override
Return one or more polygonal contours that represent the geometry of the object.
Definition objects.cpp:175
uint16_t id
Object identifier.
Definition objects.h:234
float get_angular_velocity() const
Retrieves the object's current angular velocity.
Definition objects.cpp:115
virtual bool is_tangible() const override
Returns whether this object is tangible (e.g. collisions, etc) or not.
Definition objects.h:218
float _estimated_dt
Definition objects.h:264
PhysicalObject(uint16_t _id, float _x, float _y, ObjectGeometry &geom, b2WorldId world_id, float _linear_damping=0.0f, float _angular_damping=0.0f, float _density=10.0f, float _friction=0.3f, float _restitution=0.5f, std::string const &_category="objects")
Constructs a PhysicalObject.
Definition objects.cpp:56
Class representing the simulation environment.
Definition simulator.h:43
std::vector< std::vector< b2Vec2 > > arena_polygons_t
Definition geometry.h:13
void get_cmap_val(std::string const name, uint8_t const value, uint8_t *r, uint8_t *g, uint8_t *b)
Interface to colormaps.
Definition objects.cpp:282
Object * object_factory(Simulation *simulation, uint16_t id, float x, float y, b2WorldId world_id, Configuration const &config, LightLevelMap *light_map, size_t userdatasize=0, std::string const &category="objects")
Factory of simulation Objects. Return a constructed object from configuration.
Definition objects.cpp:238
std::unique_ptr< Simulation > simulation
Global simulation instance.
Definition simulator.cpp:74