Sign in to follow this  
daniel_i_l

constructor not working

Recommended Posts

daniel_i_l    295
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;
	
	//bounding circle radius
    double           m_BoundingRadius;

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

public:
	//ctor, overloaded ctor, and dtor.
	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
	void AddForce(Force F);
	//clears the list
	void ClearForces(){Forces.clear();}
	//use the forces to get total force and torque
    void ApplyForces();
	Vector2D TotalForce;
	double   TotalTorque;

//----------accessor methods
	double   BoundingRadius(){return m_BoundingRadius;}
};

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));

	//calculate the bounding radius
	double m_BoundingRadius = -1000000;
	for(unsigned int i=0; i<m_CornersInBodySpace.size(); ++i)
	{
		if(m_CornersInBodySpace[i].Length() > m_BoundingRadius)
		{
			m_BoundingRadius = m_CornersInBodySpace[i].Length();
		}
	}


	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 this post


Link to post
Share on other sites
ToohrVyk    1595
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 this post


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

double m_boundingradius=-100000;

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this