Sign in to follow this  
daniel_i_l

weird structure problem

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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]

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