1
votes

I have a C++ script with a struct object 'Particle' , with x, y ,z position stored. I create several particle objects with unique ID's. How do I get the position of a particle by ID? Something like: particle.getPosByID(int ID);

My code is as follows:

struct Particle {
Vector position     { 0.0f, 0.0f, 0.0f };
Vector velocity     { 0.0f, 0.0f, 0.0f };
Vector acceleration { 0.0f, 0.0f, 0.0f};
Vector color        { 1.0f, 0.0f, 0.0f }; // RED
int ID = 0;}

I've tried within the struct:

    float getPosX(int ID)
{
    return this->position.x;
}

with:float ix = particles->getPosX(5);

but no luck. Any ideas? Would I need a class with getters? If so, how do I go about that? Struct has been good and easy to use so far...

Thanks!

EDITED

Vector is a struct:

struct Vector
{
float x, y, z;}

Particles are stored using an array:

const int MaxParticles = 5;
Particle particles[MaxParticles];
4
Can you update this post so we have a snippet of code that we are able to compile ourself? - JVApen
How are you storing these Particles? - NathanOliver
You'll have to show us how you create these items and how you're storing them. Read through minimal reproducible example and work on updating your question. - Retired Ninja
How is Vector implemented? Could you please add it to the problem? - Sumit Jha
Edited problem to include details on vector and particle container - Nas25

4 Answers

1
votes

Particles are stored using an array

If the particle IDs correspond to array indices, then access that array by id:

float ix = particles[id].position.x;

If not, you'll probably be better off storing them inside some associative structure like a map:

std::map<int, Particle> particles;

You can drop the id from the struct then, and use the same code as above.

1
votes

Since you store the particles in an array, you have to go through them one by one to check

float getPosX(int ID)
{
    for (auto const & p : particles) { // assume particles are a global variable
        if (p.ID == ID) 
            return p.position.x;
    }
    return 0; // or some value to indicate error or not found
}
1
votes

if you are using an array to store your particles and the array indices do not correspond to the particle ids, then you need to just search the array.

#include <iostream>
#include <algorithm>
using namespace std;

struct Vector { 
    float x, y, z;
};

struct Particle {
    int ID;
    Vector position     { 0.0f, 0.0f, 0.0f };
    Vector velocity     { 0.0f, 0.0f, 0.0f };
    Vector acceleration { 0.0f, 0.0f, 0.0f};
    Vector color        { 1.0f, 0.0f, 0.0f }; // RED
};

int main() {
    const int MaxParticles = 5;
    Particle particles[MaxParticles] = {{0},{3},{2},{4},{5}};

    // find particle with loop
    int findid = 3;
    for(size_t i=0;i< MaxParticles;++i)
    {
        if (particles[i].ID == findid)
        {
            std::cout << "found partcile with id " << findid <<  " at index " << i << std::endl;
            break;
        }
    }

    // find particle with std::find_if
    auto iter = std::find_if(particles,particles+MaxParticles, [&](const Particle& p){
        if (p.ID == findid)
            return true;
        return false;
    });

    std::cout << "Found particle " << iter->ID << std::endl;

    return 0;
}

Demo

You would be likely be better off to just use an associative container like std::map as suggested in one of other answers though.

1
votes

Here is class definition which can help you to achieve this. Here you can use std::map instead of array 'particles' for better search performance.

struct Particle 
{
    /// This is your existing particle struct
}

class Particles
{
    const int MaxParticles = 5;
    Particle particles[MaxParticles];
    Particles()
    {

    }

    void AddParticle(Particle p)
    {
        ///Insert particle 'p' into array 'particles'
    }

    void RemoveParticle(Particle p)
    {
        ///Remove particle 'p' from array 'particles'
    }

    Particle GetParticle(int ID)
    {
        Particle p;
        ///Add loop over 'particles' to search particle with specified ID and then return that particle
        return p;
    }

    Vector GetParticlePosition(int ID)
    {
        Vector v;
        ///Add loop over 'particles' to search particle with specified ID and then return it postion
        return v;
    }
}