squares[] and sorting vertices into arrays...
Well, here i am again, and my program is broken.
I was working on a physics function which works by setting the player's height to the value of the nearest item in an array to the player's x and z. So if the player's x was 10 and z was 10, then the player's y would be squares[10][10].
I have a rough approximate function which performs the actual finding in squares[x][y], but the problem is in the function which runs once at the start of the app, which sorts the terrain's verts into squares[160][160].
I have two main ways to do this:
 Pick a vertex and scroll through the squares[160][160] until the most appropriate item is found.
 Pick an item and scroll through the verts making an average of all verts found.
The pick a vertex method takes too long, and then crashes the program when finished.
Here's my function:
The result of this is this:
And so on...
What is my best option here?
Thanks
mikey
I was working on a physics function which works by setting the player's height to the value of the nearest item in an array to the player's x and z. So if the player's x was 10 and z was 10, then the player's y would be squares[10][10].
I have a rough approximate function which performs the actual finding in squares[x][y], but the problem is in the function which runs once at the start of the app, which sorts the terrain's verts into squares[160][160].
I have two main ways to do this:
 Pick a vertex and scroll through the squares[160][160] until the most appropriate item is found.
 Pick an item and scroll through the verts making an average of all verts found.
The pick a vertex method takes too long, and then crashes the program when finished.
Here's my function:
Code:
void gravPhysics(Mesh *mesh)
{
float x1;
float y1;
float z1;
x1 = (mesh>verts[counter+1]); // *
z1 = (mesh>verts[counter+2]); //  This finds the first point
y1 = (mesh>verts[counter+3]); // *
x1 = x1/1500;
y1 = y1/1500;
z1 = z1/1500;
while ( (sqrx > 159 && sqry > 159)  (x1 != 0 && y1 != 0 && z1 != 0)) // if the square we are checking is not the last one...
{
printf("Entered While loop...\n");
printf("SQRX is %i\n", sqrx);
x1 = mesh>verts[counter+1]; // *
z1 = mesh>verts[counter+2]; //  This finds the first point
y1 = mesh>verts[counter+3]; // *
x1 = x1/1500; // O
y1 = y1/1500; // O  Not sure about these, they convert each vert's real position to one less than 160.
z1 = z1/1500; // O
printf("x1 = %d y1 = %d z1 = %d\n",x1,y1,z1);
if ( (x1 <= sqrx) && (x1 >= (sqrx 1)) && (z1 <= sqry) && (z1 >= (sqry 1))) // checks if the first point is in the square designated by sqr
{ // it is, so...
counter += 3; // pick the next vertex...
vt += y1; // change vt by the vertex' y' ( This is used in calculating the average height of the item of [b]squares[][][/b].
vtcount ++; // also used in calculating the average...
printf("The vertex is in the box...\n");
}
else
{
if (vtcount > 0)
{
squares[sqrx][sqry] = (vt/vtcount);
vtcount = 0;
vt = 0;
printf("Setting Y position of vertex: %d\n" , counter+1);
}
else
{
if (sqrx > 160) // v all used in moving the square...
{
sqrx = 0;
sqry ++;
}
else
{
sqrx++;
}
}
}
}
}
The result of this is this:
Quote:[Session started at 20090909 18:35:34 +0100.]
Entered While loop...
SQRX is 0
x1 = 2147483648 y1 = 1058685389 z1 = 2147483648
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058685389 z1 = 2147483648
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 536870912 y1 = 1058629703 z1 = 1073741824
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058685389 z1 = 1073741824
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058685389 z1 = 2147483648
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 536870912 y1 = 1058629703 z1 = 2147483648
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 536870912 y1 = 1058629703 z1 = 536870912
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058667144 z1 = 1073741824
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058685389 z1 = 536870912
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058667144 z1 = 536870912
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058667144 z1 = 1073741824
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058685389 z1 = 1073741824
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058685389 z1 = 1073741824
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058685389 z1 = 2147483648
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 536870912 y1 = 1058629703 z1 = 1073741824
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058667144 z1 = 1073741824
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058667144 z1 = 2147483648
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 536870912 y1 = 1058629703 z1 = 2147483648
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 1610612736 y1 = 1058667064 z1 = 536870912
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058667144 z1 = 1073741824
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058685389 z1 = 536870912
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 0 y1 = 1058630540 z1 = 536870912
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 0 y1 = 1058630540 z1 = 1073741824
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058685389 z1 = 1073741824
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058667144 z1 = 2147483648
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 1073741824 y1 = 1058598241 z1 = 536870912
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 2147483648 y1 = 1058667144 z1 = 2147483648
The vertex is in the box...
Entered While loop...
SQRX is 1
x1 = 1073741824 y1 = 1058598241 z1 = 2147483648
The vertex is in the box...
Entered While loop...
SQRX is 1
And so on...
What is my best option here?
Thanks
mikey
~ Bring a Pen ~
First of all, change
to
and it will start to actually tell you something helpful about what is going on.
Then have a careful look at what is going on in that while loop  the "or nothing equals 0" bit looks like a recipe for a bounds overflow to me. Maybe there's a way to refactor things into nested for loops as that makes it much easier to be sure nothing will overflow.
Code:
printf("x1 = %d y1 = %d z1 = %d\n",x1,y1,z1);
Code:
printf("x1 = %f y1 = %f z1 = %f\n",x1,y1,z1);
Then have a careful look at what is going on in that while loop  the "or nothing equals 0" bit looks like a recipe for a bounds overflow to me. Maybe there's a way to refactor things into nested for loops as that makes it much easier to be sure nothing will overflow.
Yes, thanks for the printf tipoff. I will try to rewrite the while loop soon though...
~ Bring a Pen ~
Anyone else?
~ Bring a Pen ~
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Game rectangles & OpenGL vertices  mk12  2  3,056 
Sep 7, 2010 07:45 PM Last Post: mk12 

Diehard vertexsorting function not accepting input values!  mikey  6  4,206 
Oct 31, 2009 03:36 AM Last Post: mikey 

Skewed Vertices with Quaternions, Matrices, and Good Ol' Trig  Oddity007  4  3,928 
May 12, 2009 03:13 PM Last Post: Oddity007 

Depth Sorting algorithm  Leroy  1  4,867 
Jul 2, 2007 01:47 AM Last Post: aegidian 

Finding Outer Vertices of Shapes  Nick  18  9,525 
Nov 27, 2006 07:01 AM Last Post: Nick 