I have a class named Particle and i want to create a vector of particles. The problem is that my code is not drawing the objects stored in the vector. I tried different implementations, the first one was:
std::vector<Particle> particles(10);
and then i filled the vectors:
for(size_t i = 0; i < 10; ++i) particles.emplace_back(new Particle(false));
for drawing i used:
for(const auto& x: particles) window.draw(x);
the problem is that this is drawing just one object. Then i tried to create a class of particles container with a vector of shared_ptr:
std::vector<std::shared_ptr<Particle>> particle_container;
And the problem is the same, it just draws one particle, i don't know if theres something wrong with my implementation of the class particle or something wrong in the waay i use the draw function of sfml. This is my Particle class:
#include "SFML/Graphics.hpp"
#include <random>
#include <cmath>
#include <limits>
#include <memory>
using namespace sf;
class Particle : public sf::Drawable{
sf::CircleShape particle;
Vector2f position; //current position of the particle
Vector2f velocity; //velocity of the particle
double radius; //radius of each particle
double mass; //mass of the particle
int count; //number of collisions of each particle
bool sick; //if the particle is infected
Color color; //color of the particle
public:
//constructors
Particle(Vector2f _position, Vector2f _velocity, double _radius, double _mass, bool _sick);
Particle(bool sick);
}:
the Particle.cpp:
#include "Particle.h"
template<class T>
double Rand(T first, T last) {
std::random_device dev;
std::default_random_engine eng(dev());
std::uniform_real_distribution<double> dis(first, last);
return dis(eng);
}
Particle::Particle(Vector2f _position, Vector2f _velocity, double _radius, double _mass, bool _sick) {
position = _position;
velocity = _velocity;
radius = _radius;
mass = _mass;
sick = _sick;
if(sick) color = sf::Color::Green;
else color = sf::Color::Red;
//giving values to the particle
particle.setPosition(position);
particle.setRadius(radius);
particle.setFillColor(color);
}
Particle::Particle(bool sick) {
position.x = Rand(0.0, 550.0);
position.y = Rand(0.0, 550.0);
velocity.x = Rand(-0.005, 0.005);
velocity.y = Rand(-0.005, 0.005);
radius = 2.0;
mass = 0.5;
if(sick) color = sf::Color::Green;
else color = sf::Color::Red;
particle.setPosition(position);
particle.setRadius(radius);
particle.setFillColor(color);
}
And this is the particles_container
class particles_container : public sf::Drawable {
std::vector<std::shared_ptr<Particle>> particle_container;
//std::vector<Particle> particle_container;
virtual void draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const;
public:
//default constructor
particles_container();
//constructor with a number of given particles
explicit particles_container(size_t size);
//default destructor
~particles_container() override;
//returns the container
std::vector<std::shared_ptr<Particle>> getContainer();
//another function to draw
void drawContainer(sf::RenderWindow &window);
};
the particles_container.cpp:
void particles_container::draw(RenderTarget &renderTarget, sf::RenderStates renderStates) const {
for (const auto & i : particle_container) {
renderTarget.draw(*i);
}
}
particles_container::particles_container(size_t size) {
if(size > 0){
for(size_t i = 0; i < size; ++i) particle_container.emplace_back(new Particle(false));
}
}
std::vector<std::shared_ptr<Particle>> particles_container::getContainer() {
return particle_container;
}
void particles_container::drawContainer(RenderWindow &window) {
for (auto & i : particle_container) {
window.draw(*i);
}
}
and finally this is my main.cpp:
#include "Particle.h"
#include "particles_container.h"
int main() {
sf::RenderWindow window(sf::VideoMode (600, 600), "PF");
std::vector<Particle> particles(10);
for(size_t i = 0; i < 10; ++i) particles.emplace_back(new Particle(false));
Particle p1(sf::Vector2f(45.0,45.0), sf::Vector2f(0.01,0.005), 2, 0.5, false);
particles_container parts(100);
Particle p3(true);
std::vector<std::shared_ptr<Particle> > my_vector(10);
for(size_t i = 0; i < 10; ++i) my_vector.emplace_back(new Particle(false));
while(window.isOpen()){
sf::Event event;
while(window.pollEvent(event)){
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
}
}
window.clear();
//parts.drawContainer(window);
//window.draw(parts);
for(const auto& x: particles) window.draw(x);
window.display();
}
}
this is the output: output
Any help or advice would be greatly appreciated, thanks.
I am using:
SFML 2.5.1 and c++17
Your Rand
function constructs a new random device every time, which has a high probability to always return the same number sequence. Move the construction of dev
out of your Rand
function.