Jump to content
  • Advertisement
Sign in to follow this  
daniel_i_l

weird structure problem

This topic is 4553 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'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
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).

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
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!