Hello all,

Trying to work on some code - a struct with spring variables is loaded into a vector in an insert function, but the vector is empty in later functions. I suspect it has something to do with scopes or something, but am relatively new to std::vectors.

I think it is a relatively simple problem, so will just post the bits of code related to the vector (rather than a runnable program). Apologies for any confusion, but there is also another 'Vector' class used in my program - to represent 3 variables in a row.

Code:

struct Spring {
Vector equilPos; //represents equilibrium position of spring
float k; //spring coefficient - represents tightness - high value makes them more rigid
float b; //damping coefficient
Spring(){
k=0;
b=0;
equilPos.zero();
}
};
class PhysObj {
public:
//lots of unrelated functions / variables
std::vector<Spring> springs;
void add_spring(float springcoeff, float dampcoeff, float x, float y, float z);
//calculate forces (where the spring variables are actually used)
void forces(const State &state, float t, Vector &force, Vector &torque);
}
void PhysObj::add_spring(float springcoeff, float dampcoeff, float x, float y, float z){
Spring* tempspring = new Spring;
tempspring->equilPos.x = x;
tempspring->equilPos.y = y;
tempspring->equilPos.z = z;
tempspring->k = springcoeff;
tempspring->b = dampcoeff;
springs.push_back(*tempspring);
std::cout << "spring created, now "<< springs.size() << " spring, k="
<< springs[springs.size()-1].k << " b = "<< springs[springs.size()-1].b
<< std::endl;
}
void PhysObj::forces(const State &st, float t, Vector &force, Vector &torque) {
//springs
for (int i=0; i<springs.size(); i++){
Vector dampingforce = springs[i].b * st.vel;
force == (-springs[i].k) * st.pos - springs[i].equilPos)) - dampingforce;
}
std::cout << "Force: " << force.x << ", " << force.y << ", " << force.z<< std::endl;
std::cout << "Number of springs: " << springs.size() << std::endl;
}

The 'add_spring' function says there is 1 spring with the correct coefficients, the 'forces' function says there are 0 springs (and consequently, no forces).

Can someone help?