squares[] and sorting vertices into arrays...

Member
Posts: 281
Joined: 2009.04
Post: #1
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:

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 2009-09-09 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 ~
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #2
First of all, change
Code:
printf("x1 = %d y1 = %d z1 = %d\n",x1,y1,z1);
to
Code:
printf("x1 = %f y1 = %f z1 = %f\n",x1,y1,z1);
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 re-factor things into nested for loops as that makes it much easier to be sure nothing will overflow.
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #3
Yes, thanks for the printf tip-off. I will try to rewrite the while loop soon though...

~ Bring a Pen ~
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #4
Anyone else?

~ Bring a Pen ~
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Game rectangles & OpenGL vertices mk12 2 2,774 Sep 7, 2010 07:45 PM
Last Post: mk12
  Die-hard vertex-sorting function not accepting input values! mikey 6 3,866 Oct 31, 2009 03:36 AM
Last Post: mikey
  Skewed Vertices with Quaternions, Matrices, and Good Ol' Trig Oddity007 4 3,589 May 12, 2009 03:13 PM
Last Post: Oddity007
  Depth Sorting algorithm Leroy 1 4,546 Jul 2, 2007 01:47 AM
Last Post: aegidian
  Finding Outer Vertices of Shapes Nick 18 8,644 Nov 27, 2006 07:01 AM
Last Post: Nick