Original Post
I have the following class: 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 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.
//-------------------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.Length() > m_BoundingRadius)
{
m_BoundingRadius = m_CornersInBodySpace.Length();
}
}
UpdateGeometry();
}