Advertisement

program not running because of arrays...

Started by January 23, 2005 02:50 AM
13 comments, last by adam17 19 years, 10 months ago
Quote: Original post by adam17
i put the d in my float3 struct so when i add to it, and make it a polygon structure, i will have the distance formula precalculated for collision detection. any reasons why it shouldnt be there still?

Why do you need vector length in CD? You usualy need just vertices and plane normal. And that is stored somewhere else.
Post your fixed code so we can see what is still wrong.
You can merge _cube and _node but you would have to do a but redesign. But the point is why would you want to do it. I perefer having 2 classes. One for actual octree that holds some global information about tree and one pointer to root node (not 8 like you). Then there is node class that has pointers to 8 nodes and so on. Try to keep node structure as small a possible.
You should never let your fears become the boundaries of your dreams.
ok here are the changes that ive made
const int MAX_VERTEX = 100;struct float3{	float x, y, z;	float d;	void mag();};void float3::mag(){	d=sqrt((x*x)+(y*y)+(z*z));}struct _node{	float3			center;	float			width;	//calculated by width/pow(2, <recursion level>)	int				numVertex;	vector<float3>	vertices;	_node			*node[8];};struct _cube{		float3			center;		//center of root cube	float			width;		//width of root cube	int				numVertex;	vector<float3>	vertices;	_node			*node[8];	_cube();	void Load(vector<float> array);	void Draw();};_cube::_cube(){	numVertex = 0;}void Sort(_node temp){	int nodeLevel;	if(temp.numVertex > MAX_VERTEX)	{		for(int x=0; x<temp.numVertex; x++)		{			nodeLevel = 8;			if(temp.vertices[x].x < temp.center.x)				if(temp.vertices[x].y < temp.center.y)					if(temp.vertices[x].z < temp.center.z)					{						nodeLevel = 0;					}					else					{						nodeLevel = 1;					}				else					if(temp.vertices[x].z < temp.center.z)					{						nodeLevel = 2;					}					else					{						nodeLevel = 3;					}			else				if(temp.vertices[x].y < temp.center.y)					if(temp.vertices[x].z < temp.center.z)					{						nodeLevel = 4;					}					else					{						nodeLevel = 5;					}				else					if(temp.vertices[x].z < temp.center.z)					{						nodeLevel = 6;					}					else					{						nodeLevel = 7;					}			temp.node[nodeLevel]->numVertex++;			temp.node[nodeLevel]->vertices.resize(temp.node[nodeLevel]->numVertex);			temp.node[nodeLevel]->vertices[temp.node[nodeLevel]->numVertex-1].x = temp.vertices[x].x;			temp.node[nodeLevel]->vertices[temp.node[nodeLevel]->numVertex-1].y = temp.vertices[x].y;			temp.node[nodeLevel]->vertices[temp.node[nodeLevel]->numVertex-1].z = temp.vertices[x].z;		}		for(x=0; x<8; x++)		{			if(temp.node[x]->numVertex > MAX_VERTEX)			{				Sort(temp.node[x]);			}		}	}}void _cube::Load(vector<float> array){	float3	upperlim;	float3	lowerlim;	float	xlen, ylen, zlen;	numVertex = array.size()/3;		vertices.resize(numVertex);	for(int x=0; x<numVertex; x++)	{		vertices[x].x = array[3*x];		vertices[x].y = array[3*x+1];		vertices[x].z = array[3*x+2];	}	for(x=0; x<numVertex; x++)		vertices[x].mag();	for(x=0; x<numVertex; x++)	{		if(lowerlim.x > vertices[x].x)			lowerlim.x = vertices[x].x;		if(lowerlim.y > vertices[x].y)			lowerlim.y = vertices[x].y;		if(lowerlim.z > vertices[x].z)			lowerlim.z = vertices[x].z;		if(upperlim.x < vertices[x].x)			upperlim.x = vertices[x].x;		if(upperlim.y < vertices[x].y)			upperlim.y = vertices[x].y;		if(upperlim.z < vertices[x].z)			upperlim.z = vertices[x].z;	}		xlen = upperlim.x - lowerlim.x;	ylen = upperlim.y - lowerlim.y;	zlen = upperlim.z - lowerlim.z;	if(xlen>ylen)		if(xlen>zlen)			width=xlen;		else			width=zlen;	else		if(ylen>zlen)			width=ylen;		else			width=zlen;	center.x = (upperlim.x + lowerlim.x) / 2;	center.y = (upperlim.y + lowerlim.y) / 2;	center.z = (upperlim.z + lowerlim.z) / 2;}void _cube::Draw(){	float w = width/2;	glDisable(GL_LIGHTING);	glDisable(GL_TEXTURE_2D);	glPointSize(15);	glColor3f(1, 0, 0);	glBegin(GL_POINTS);		glVertex3f(center.x, center.y, center.z);	glEnd();	glColor4f(1, 1, 0, 1);	glLineWidth(1);	glBegin(GL_LINE_LOOP);		glVertex3f(center.x-w, center.y-w, center.z-w);		glVertex3f(center.x+w, center.y-w, center.z-w);		glVertex3f(center.x+w, center.y+w, center.z-w);		glVertex3f(center.x-w, center.y+w, center.z-w);	glEnd();	glBegin(GL_LINE_LOOP);		glVertex3f(center.x-w, center.y-w, center.z+w);		glVertex3f(center.x+w, center.y-w, center.z+w);		glVertex3f(center.x+w, center.y+w, center.z+w);		glVertex3f(center.x-w, center.y+w, center.z+w);	glEnd();	glBegin(GL_LINES);		glVertex3f(center.x-w, center.y-w, center.z-w);		glVertex3f(center.x-w, center.y-w, center.z+w);		glVertex3f(center.x+w, center.y-w, center.z-w);		glVertex3f(center.x+w, center.y-w, center.z+w);		glVertex3f(center.x-w, center.y+w, center.z-w);		glVertex3f(center.x-w, center.y+w, center.z+w);		glVertex3f(center.x+w, center.y+w, center.z-w);		glVertex3f(center.x+w, center.y+w, center.z+w);	glEnd();	glEnable(GL_LIGHTING);	glEnable(GL_TEXTURE_2D);}

i changed a few things in here like moving variables around and out of the structs.
Advertisement
Quote: Original post by The Rug
Quote: Original post by Steve132
To my knowledge, there is another problem as well: can structures even HAVE constructors or member functions? I thought that was something only classes could do. Although now that I test it..it seems to indicate that that is valid...odd....can someone explain this? it is definatly contradictory to the C++ books I have read....


IIRC in C++ structs are essentially classes where all the members are public by default. Books usually fail to mention this, possibly to avoid confusion when it comes to legacy code using C structs (which are different from C++ structs), which cannot have member functions.

Correct: A C++ struct is a class, whose member access and inheritance both default to public instead of private (as for classes declared with the class keyword).
There is still bunch of problems in your code.
-you don't call sort on load
-you are never creating new nodes in recursion
-you don't calculate node center
-_cube still has way to much of stuff in it. You only need one thing: a root node
-Sort should be _node member function, not stand-alone.
-...

And a few a bit OT points:
-what compiler are you using? I suspect it's MS VC++ 6.0. If so you *really* should update it.
-You shoud make or grab some math functions. They would make your code much more readabile. Same goes for better vector classes.
You should never let your fears become the boundaries of your dreams.
Quote: Original post by _DarkWIng_
There is still bunch of problems in your code.
-you don't call sort on load
-you are never creating new nodes in recursion
-you don't calculate node center
-_cube still has way to much of stuff in it. You only need one thing: a root node
-Sort should be _node member function, not stand-alone.
-...

And a few a bit OT points:
-what compiler are you using? I suspect it's MS VC++ 6.0. If so you *really* should update it.
-You shoud make or grab some math functions. They would make your code much more readabile. Same goes for better vector classes.

ill go back and try to make a bunch of revisions.

yeah i am still using VC++6.0. what should i upgrade to? i know 7 is out but i thought it was primarily .net code.

This topic is closed to new replies.

Advertisement