Jump to content
  • Advertisement
Sign in to follow this  

constructor not working

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

If you intended to correct an error in the post then please contact us.

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{
	//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

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

	//ctor, overloaded ctor, and dtor.
	RigidBody(Vector2D p, Vector2D v, double m, double w, double h, bool s);
	//RigidBody(const RigidBody &R);
	//RigidBody& operator= (const RigidBody& R);

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

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


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
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
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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!