# layout of walls array passing problem

## Recommended Posts

Hi there
I am currently attempting to set up a track to race around. I am trying to make the track generic and possibly extend on it so people can make there own tracks.

Currently for testing I am simply just drawing the walls in the main (I figure this isn't the most efficient way of doing this).

I am trying to get it so that if you specify the 6 co-ordinates of the wall (start and end x,y and z). It will automatically be able to generate a bounding box, normal and barycentric traingle.

These can then be used for collision detection and lighting.

I am currently trying to set up the arrays in the init function like
int wall1[] = {-50,3,50,50,0,50};

The idea is to then pass this to a class to calculate collision and to a function that will draw these on screen.

Once these values are put through the init() function it gets forwarded to my Enviroment class which uses the values and stores them in its own array.

int * Enviroment::InitiliseArray(int wall[])
{
int initwall[6];

for(int i=0; i<6; i++)
{
initwall[i] = wall[i];
std::cout<<"init wall = "<<initwall[i]<<std::endl;
}

return initwall;

}

I am then wanting to pass this array to a collision function to calculate collisions?

IS all this work unnecessary? is there a more standered way of doing this?

Many thanks for any help

Let me know if you require any further information code or explanation

##### Share on other sites
Quote:
 is there a more (standard) way of doing this?

How you create and store graphics and collision data depends on the requirements for your graphics rendering system and collision engine. I suggest you actually write some rendering and collision code to determine what information you'll need.

FYI: I realize the function you posted may be just a quick example, but the value you return (an int*) points to the stack space where you call InitiliseArray. You'll probably want to allocate graphics and collision information on the heap.

##### Share on other sites
Awsome thanks

Sorry as I am still new to doing some of this stuff. I am after allowing a user to create a track as I specified earler. Currently with the syste mI have that works and draws correctly etc. I am having to manually specify everything within the main function and am using arrays for the data.

as I showed before
int wall1[] = {-50,3,50,50,0,50};
this will create the wall to the parametes stated with startX,startY,startZ,endX,endY,andZ respectivly.

I am currently just creating a new aray and givign it a new name each time. such as wall1,wall2 etc.
How do you allow for an infinite number of arrays with different names to be produced?

at the moment I just have a hacky solution of wall1 - wall10 and then 0,0,0 unless a number is specified.

If a user wants to create a track then I don't want to limit them to 10 walls, or draw walls unnecessarily wasting space etc.

Do you have to use some kind of linked list or other data structure?

Many thanks again

##### Share on other sites
Quote:
 How do you allow for an infinite number of arrays with different names to be produced?

Take a look at the C++ STL (Standard Template Library) for std::vector or std::list. Vector in this case is an object that you can "push" objects into and you needn't worry about allocating memory or how many, etc.

In C++, you'd use it something like:
struct WALL{    int wall[6];};std::vector<WALL> walls;WALL newWall;// fill newWall with datawalls.push_back(newWall);

##### Share on other sites
I have tried using the vectors however am still running into issues. I figure it must be the way my code is structured?

I have a Collision class that should first calculate a bounding box and check to see if the character is within the bounding box. If the character is it does a point-to plane test and a barycentric traingle test, if both are true then a collision has occoured.

I need to calculate the normals to do this. I want to calculate the normals in the init function and then use the result for my point to plane in the display.

The code for my Enviroment is

int Enviroment::drawWall(int wallArray[]){	// used for bounding box test	wallPoints[0] = wallArray[0]; // first X from main	wallPoints[1] = wallArray[3]; //second X from main	wallPoints[2] = wallArray[2]; //first z from main	wallPoints[3] = wallArray[5]; //second Z from main			glPushMatrix();        //draw walls	glBegin(GL_POLYGON);		glColor3f(0.7,0.7,0.7);		glTexCoord2f(0,0); glVertex3f(wallArray[0], wallArray[1], wallArray[2]);		glTexCoord2f(1,0); glVertex3f(wallArray[0], wallArray[4], wallArray[2]);		glTexCoord2f(1,1); glVertex3f(wallArray[3], wallArray[4], wallArray[5]);		glTexCoord2f(0,1); glVertex3f(wallArray[3], wallArray[1], wallArray[5]);		glEnd();glPopMatrix();	return *wallPoints;}

The idea is to draw the wall and store the points I could then pass these to my collision class to deal with.

my collision class is
bool Collision::BoundingBox(Boat_Pos Player,int wallArray[]){	//Collision boxtest;	bool collision;	std::cout<<"Player BB X1 is "<<Player.boatDirectionX<<std::endl;	std::cout<<"Player BB Z1 is "<<Player.boatDirectionZ<<std::endl;	std::cout<<"Player BB X2 is "<<Player.boatDirectionX<<std::endl;	std::cout<<"Player BB Z2 is "<<Player.boatDirectionZ<<std::endl;	std::cout<<"Wall BB X1 is "<<wallArray[0]<<std::endl;	std::cout<<"Wall BB Z1 is "<<wallArray[2]<<std::endl;	std::cout<<"Wall BB X2 is "<<wallArray[1]<<std::endl;	std::cout<<"Wall BB Z2 is "<<wallArray[3]<<std::endl;//check to see if possible collision can occour		if(!(Player.boatDirectionX > wallArray[0] || Player.boatDirectionZ >wallArray[2] || Player.boatDirectionX <wallArray[1] || Player.boatDirectionZ < wallArray[3]))	{		std::cout<<"collision"<<std::endl;		collision = true;		return collision;	}	else 	{		std::cout<<"No collision"<<std::endl;		collision = false;		return collision;	}}float * Collision::CalculateNormal(float point1[], float point2[],float point3[],float normal[3]){	//calcualte cross product	float ux = point2[0] - point1[0];	float uy = point2[1] - point1[1];	float uz = point2[2] - point1[2];	float vx = point3[0] - point1[0];	float vy = point3[1] - point1[1];	float vz = point3[2] - point1[2];	//float normal[3];	normal [0] = ((vy*uz) - (vz*uy))*-1;	normal [1] = ((vz*ux) - (vx*uz));	normal [2] = ((vx*uy) - (vy*ux));			return normal;}//check for collision from player point to point on planefloat Collision::NormaliseNormal(float B[],float point[],float triA[]){	float lengthX = sqrt((B[0]*B[0]) + (B[1]*B[1]) + (B[2]*B[2]));	float normalX = B[0] /= lengthX;	float normalY = B[1] /= lengthX;	float normalZ = B[2] /= lengthX;		float testX[] = {point[0] - triA[0]};	float testY[] = {point[1] - triA[1]};	float testZ[] = {point[2] - triA[2]};	float s = (*testX - *triA); 	float distance = Collision::DotProduct(s,normalX,0,0,0,0);	std::cout<<"Distance "<<distance<<std::endl;	float cx = (point[0] - distance) * normalX;	float cy = (point[1] - distance) * normalY;	float cz = (point[2] - distance) * normalZ;	std::cout<<"cx is "<<cx<<std::endl;	//float collisionPoints[] = {cx,cy,cz};	return cx;}

I apologise for asking all the questions I have not tried a project like this before and passing values, making somethign work in all cases graphically and structuring the code is proving to be a nightmare.

Should I keep my classes seperate as they are at the moment like collision,Enviroment and main?

How can I get my collision object to take an argument from both the main and my Enviroment class?

How can I use my normal in the init and then use the results in the display without them interfearing with each other?

Many thanks again

##### Share on other sites
Quote:
 I have not tried a project like this before and passing values, making somethign work in all cases graphically and structuring the code is proving to be a nightmare.

Yeah, it's quite apparent you've bitten off a lot more than you can chew. I apologize for suggesting you actually write some code. Your questions were fairly explicit and I thought you were further along than you are.

Generally speaking, you probably want to start out with a good idea how you want your game loop to work. That is, how you load game data, how you get user input, check for collisions, update objects, render, etc.

I'd suggest you take a step back and google around for something like "game loop design," or something similar. You will find lots of posts right here on gamedev discussing that subject. You can use the search box in the upper right-hand corner of this screen for that.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627660
• Total Posts
2978487

• 10
• 12
• 22
• 13
• 33