• 14
• 12
• 9
• 10
• 9

constructor not working

This topic is 4236 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have the following class:
//-------------------Rigid Body class----------------------
// This class holds all the information needed for a rigid
// body, including it's state and the forces acting on it.
// For now it can handle only squares but I hope to upgrade
// it or inherit it in the future.
//---------------------------------------------------------
class RigidBody{
private:
//a list of all the coordinates as they're in world space
vector<Vector2D> m_CornersInWorldSpace;

//a list of all the coordinates as they're in body space
vector<Vector2D> m_CornersInBodySpace;

//center of mass in world space
//Vector

//the width;
double           m_Width;
//the height
double           m_Height;

//a vector of all the forces acting on the body;
vector<Force> Forces;

public:
RigidBody(){}
RigidBody(Vector2D p, Vector2D v, double m, double w, double h, bool s);
//RigidBody(const RigidBody &R);
//RigidBody& operator= (const RigidBody& R);
~RigidBody();

//transforms the corners to world space
void UpdateGeometry();

//transforms a vector of point/one point to world space
void WorldTransform(vector<Vector2D> &points);
void WorldTransform(Vector2D &point);

//the state of the body
State            m_MyState;

//is the body static?
bool             m_Static;
//returns vector of the corners in world space, used for rendering
vector<Vector2D> GetCorners(){return m_CornersInWorldSpace;}

//adds a force to the list of forces
//clears the list
void ClearForces(){Forces.clear();}
//use the forces to get total force and torque
void ApplyForces();
Vector2D TotalForce;
double   TotalTorque;

//----------accessor methods
};

RigidBody::RigidBody(Vector2D p, Vector2D v, double m, double w, double h, bool s)
{
double MomentOfInertia = (1.0f/12.0f)*m*(w*w + h*h);
m_MyState = State(p, v, m, MomentOfInertia);

m_Width   = w;
m_Height  = h;
m_Static  = s;

m_CornersInBodySpace.push_back(Vector2D(-w,-h));
m_CornersInBodySpace.push_back(Vector2D( w,-h));
m_CornersInBodySpace.push_back(Vector2D( w, h));
m_CornersInBodySpace.push_back(Vector2D(-w, h));

for(unsigned int i=0; i<m_CornersInBodySpace.size(); ++i)
{
{
}
}

UpdateGeometry();
}


As you can see, in the ctor I fill in all the coords of the 2D box and then try to find the one thats furthest from the center (all bodies are centered at 0,0 local space) and call it's length m_BoundingRadius. Now for some reason when I do: vector<RigidBody> Bodies; Bodies.push_back(RigidBody(Vector2D(30,4), Vector2D(0,0), 10, 8, 4, false)); and then check what Bodies[0].m_BoundingRadius is (I checked with the debugger cause I noticed a problem, I find that it is some strange (sometimes negative) number - as if it had never been initialized! Whats the problem? Thanks.

Share on other sites
Is the chosen constructor executed? If not, what is called when the object is created? Does it correctly set the value of the bounding box radius?

Share on other sites
In your constructor, you are declaring a local variable called m_boundingradius with the line:

This then "hides" the actual member variable m_boundingradius. Any changes to m_boundingradius made from then on in the constructor modify the local variable, not the member variable, and the local variable is discarded at the end of the constructor.

Just remove the word double from the start of the line I mentioned above.