weird structure problem

Started by
2 comments, last by daniel_i_l 18 years ago
I'm writing a structure that hold a box (SBox), it is made up of a vector of planes (SPlane) and each plane is made out of a vector of corners(v_Corners) were each corner has x,y and z values. I wanted to make a constructer that initiates all the verticies (planes,corners,verticies) by a center point (x,y,z) a width (w), height (h), and a depth (d). I did this:

struct SPlane{
 vector<CVector3> v_Corners;
 int ID;
};

struct SBox{
 vector<SPlane> v_Sides;
 int ID;
 SBox(vector<SPlane> s, int i){v_Sides = s; ID = i;}
 SBox(float x, float y, float z, float w, float d, float h, int i);
 //for debugging only
 void DrawBox();
};

SBox :: SBox(float x, float y, float z, float w, float d, float h, int i)
{
  SPlane temp_plane;
  CVector3 temp_corner;
  
  ID = i;
  
  //front side
  temp_corner.x = x - w;  temp_corner.y = y - h; temp_corner.z = z + d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x + w;  temp_corner.y = y - h; temp_corner.z = z + d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x + w;  temp_corner.y = y + h; temp_corner.z = z + d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x - w;  temp_corner.y = y + h; temp_corner.z = z + d;
  temp_plane.v_Corners.push_back(temp_corner);
  v_Sides.push_back(temp_plane);
  
  //right side
  temp_corner.x = x + w;  temp_corner.y = y - h; temp_corner.z = z + d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x + w;  temp_corner.y = y + h; temp_corner.z = z + d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x + w;  temp_corner.y = y + h; temp_corner.z = z - d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x + w;  temp_corner.y = y - h; temp_corner.z = z - d;
  temp_plane.v_Corners.push_back(temp_corner);
  v_Sides.push_back(temp_plane);
  
  //back side
  temp_corner.x = x + w;  temp_corner.y = y - h; temp_corner.z = z - d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x + w;  temp_corner.y = y + h; temp_corner.z = z - d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x - w;  temp_corner.y = y + h; temp_corner.z = z - d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x - w;  temp_corner.y = y - h; temp_corner.z = z - d;
  temp_plane.v_Corners.push_back(temp_corner);
  v_Sides.push_back(temp_plane);
  
  //left side
  temp_corner.x = x - w;  temp_corner.y = y - h; temp_corner.z = z - d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x - w;  temp_corner.y = y + h; temp_corner.z = z - d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x - w;  temp_corner.y = y + h; temp_corner.z = z + d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x - w;  temp_corner.y = y - h; temp_corner.z = z + d;
  temp_plane.v_Corners.push_back(temp_corner);
  v_Sides.push_back(temp_plane);
  
  //top side
  temp_corner.x = x - w;  temp_corner.y = y + h; temp_corner.z = z + d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x + w;  temp_corner.y = y + h; temp_corner.z = z + d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x + w;  temp_corner.y = y + h; temp_corner.z = z - d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x - w;  temp_corner.y = y + h; temp_corner.z = z - d;
  temp_plane.v_Corners.push_back(temp_corner);
  v_Sides.push_back(temp_plane);
  
  //bottom side
  temp_corner.x = x - w;  temp_corner.y = y - h; temp_corner.z = z + d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x + w;  temp_corner.y = y - h; temp_corner.z = z + d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x + w;  temp_corner.y = y - h; temp_corner.z = z - d;
  temp_plane.v_Corners.push_back(temp_corner);
  
  temp_corner.x = x - w;  temp_corner.y = y - h; temp_corner.z = z - d;
  temp_plane.v_Corners.push_back(temp_corner);
  v_Sides.push_back(temp_plane);
}

But for some reason all of the 6 sides got the same corners and vertices as the front side? Why would this be? Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Advertisement
Quote:Original post by daniel_i_l
But for some reason all of the 6 sides got the same corners and vertices as the front side? Why would this be?
Thanks.
You need to call 'temp_plane.v_Corners.clear()' before adding each side.

Also, std::vector<> is most appropriate for arrays whose size is not known at compile time, or that may need to be resized at some point. Otherwise, just use a standard fixed-size C++ array (or perhaps boost::array).
You are never clearing the vector of corners in your temporary plane [smile]. Thus, you have one plane with 16 or so points, which means that all of your sides are using the same first 4 points of the plane. After each call to:

v_Sides.push_back(temp_plane);

add

temp_plane.v_Corners.clear();

That should fix the problem.


[EDIT] - Darn you jyk! You beat me to it! [crying]
Thanks, now it works.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky

This topic is closed to new replies.

Advertisement