I am working on a physics engine, following the cyclone physics engine source code but a I am having trouble with an
error that is occuring in my overloaded operator== function. It is saying that the information is unaccesible.
#include "ForceRegistry.h"
#include <algorithm>
void ForceRegistry::add(physicsEntity* body, ForceGenerator *fg)
{
ForceRegistration registration;
registration.body = body;
registration.fg = fg;
registrations.push_back(registration);
}
booloperator==(ForceRegistry::ForceRegistrationconst& r1, ForceRegistry::ForceRegistrationconst& r2)
//These two force registrations in bold are causing the two errors, but when I build the program there aren't any
//errors, and the build is successful. Is this just a false error or is there something I am missing here?
{
if (r1.body==r2.body && r1.fg==r2.fg)
returntrue;
elsereturnfalse;
}
void ForceRegistry::remove(physicsEntity* body, ForceGenerator *fg)
{
ForceRegistration registration;
registration.body = body;
registration.fg = fg;
Registry::iterator it = std::find(registrations.begin(), registrations.end(), registration);
if (it != registrations.end())
registrations.erase(it);
}
void ForceRegistry::clear()
{
registrations.clear();
}
void ForceRegistry::updateForces(real duration)
{
Registry::iterator i = registrations.begin(); // start at the first registration
for (; i != registrations.end(); i++) // and iterate through the whole list
{
// the updateForce method of the i->fg is called
i->fg->updateForce(i->body, duration); // with the registered rigid body i->body and frame
// duration passed in as the parameters
}
}
#pragma once
#include "physicsEntity.h"
#include "ForceGenerator.h"
class ForceRegistry // holds all the force generators and the particles they apply to
{
protected:
struct ForceRegistration // one force generator and the particle it applies to
{
physicsEntity *body;
ForceGenerator *fg;
};
friendbooloperator==(ForceRegistry::ForceRegistration const& r1, ForceRegistry::ForceRegistration const& r2);
typedef std::vector<ForceRegistration> Registry; // just a convenienceā¦
Registry registrations; // holds the list of registrations
public:
void add(physicsEntity* body, ForceGenerator *fg); // registers force generator to the body specified
void remove(physicsEntity* body, ForceGenerator *fg); // removes a registration from registry
void clear(); // clears all registrations (does not delete bodies or force generators, just registration*/
/* Calls all the force generators to update the forces of their corresponding particles */
void updateForces(double time); //this is the key to understanding the value of the ForceRegistry
};
If I were you I would put the operator == for ForceRegistration inside itself:
ForceRegistry.h
1 2 3 4 5 6 7
struct ForceRegistration // one force generator and the particle it applies to
{
physicsEntity *body;
ForceGenerator *fg;
booloperator == (const ForceRegistration& rhs);
};