Game Development Community

Array init issue--copying terraingrid info to another array

by Stephen Zepp · in Torque Game Engine · 03/13/2005 (10:12 am) · 2 replies

Ok, I'm re-writing the terrain deformation work to have it pass down a small height array (15,15) so that we can allow for more dynamic interaction with the deformation object: specifically, more than one peon excavating at once.

Things are looking pretty good, with one exception: When I initialize the array at the very beginning, I need to set it to the heights of the current terrain, before any modifications. The code is pretty simple:

(in the .h)
#define MAX_DEFORM_GRID_X_SIZE 15
#define MAX_DEFORM_GRID_Y_SIZE 15
   U32 mDeformGrid[MAX_DEFORM_GRID_X_SIZE, MAX_DEFORM_GRID_Y_SIZE];

(and .cc)

bool TerrainDeformer::onAdd()
{
   if(!Parent::onAdd())
      return false;
  Point2I displayDeformPoint(0,0);
  worldToGrid(mPosition, displayDeformPoint);
//  worldToGrid(spot, displaySpotPoint);
  Con::printf("TerrainDeformer::onAdd()--deform center is %f, %f (%i,%i)",
                mPosition.x, mPosition.y, displayDeformPoint.x, displayDeformPoint.y);
                
  TerrainBlock *terrBlock = isServerObject() ?         
                            gServerSceneGraph->getCurrentTerrain() :
                            gClientSceneGraph->getCurrentTerrain();
  if (!terrBlock)
  {
    Con::printf("TerrainDeformer::onAdd()--No terrain block found!");
    return false;
  }
  // do our checks to make sure we have the right qualifiers
  if ( (!mDataBlock->mProcessOverTime) ||
       (mDeformShape != eDeformShapeRectangle) )
    return true;

  const MatrixF & mat = terrBlock->getTransform();
  Point3F origin;
  mat.getColumn(3, &origin);
 
  U32 squareSize = (U32) terrBlock->getSquareSize();
  U32 halfSquareSize = squareSize / 2;

 	Point2I startBounds(0,0);
 	Point2I endBounds(0,0);
 	Point2I deformationCenter(0,0);
 	worldToGrid(Point3F(mPosition.x - mXSize/2, mPosition.y - mYSize/2, mPosition.z), startBounds);
 	worldToGrid(Point3F(mPosition.x + mXSize/2, mPosition.y + mYSize/2, mPosition.z), endBounds); 
 	worldToGrid(Point3F(mPosition.x, mPosition.y, mPosition.z), deformationCenter);
 	U32 cDeformX, cDeformY;
 	for (cDeformX = 0; cDeformX < mXSize/squareSize; cDeformX++)
 	{
 	  for (cDeformY = 0; cDeformY < mYSize/squareSize; cDeformY++)
 	  {
 	    U32 squareHeight;
 	    squareHeight = terrBlock->getHeight( (startBounds.x + cDeformX), (startBounds.y + cDeformY) );
      mDeformGrid[cDeformX, cDeformY] = squareHeight;
      Con::printf("Set deformGrid %i, %i (Real: %i,%i) to height %i (real is%i)", cDeformX, cDeformY, startBounds.x + cDeformX, startBounds.y + cDeformY, 
                  mDeformGrid[cDeformX, cDeformY], terrBlock->getHeight(startBounds.x + cDeformX, startBounds.y + cDeformY));
    }
  }
  Con::printf("Confirmation check:");
 	for (cDeformX = 0; cDeformX < mXSize/squareSize; cDeformX++)
 	{
 	  for (cDeformY = 0; cDeformY < mYSize/squareSize; cDeformY++)
 	  {
      
      Con::printf("Set deformGrid %i, %i (Real: %i,%i) to height %i, real is %i", cDeformX, cDeformY, startBounds.x + cDeformX, startBounds.y + cDeformY, 
                  mDeformGrid[cDeformX, cDeformY], terrBlock->getHeight(startBounds.x + cDeformX, startBounds.y + cDeformY));
    }
  }
  
  
   mObjBox = Box3F(Point3F(0, 0, 0), Point3F(0, 0, 0));
   resetWorldBox();

   addToScene();

   return true;
}

The problem I'm running into is copying over all the terrain height values into the array. It appears that instead of copying each grid point's value, it's over-writing previous column's values as well. Here's the debugging output:
(cont)

#1
03/13/2005 (10:12 am)
TerrainDeformer::onAdd()--deform center is -335.738007, 341.528992 (86,171)
Set deformGrid 0, 0 (Real: 84,168) to height 8177 (real is8177)
Set deformGrid 0, 1 (Real: 84,169) to height 8081 (real is8081)
Set deformGrid 0, 2 (Real: 84,170) to height 8050 (real is8050)
Set deformGrid 0, 3 (Real: 84,171) to height 7944 (real is7944)
Set deformGrid 0, 4 (Real: 84,172) to height 7968 (real is7968)
Set deformGrid 1, 0 (Real: 85,168) to height 8218 (real is8218)
Set deformGrid 1, 1 (Real: 85,169) to height 8169 (real is8169)
Set deformGrid 1, 2 (Real: 85,170) to height 8305 (real is8305)
Set deformGrid 1, 3 (Real: 85,171) to height 8255 (real is8255)
Set deformGrid 1, 4 (Real: 85,172) to height 8145 (real is8145)
Set deformGrid 2, 0 (Real: 86,168) to height 8324 (real is8324)
Set deformGrid 2, 1 (Real: 86,169) to height 8412 (real is8412)
Set deformGrid 2, 2 (Real: 86,170) to height 8438 (real is8438)
Set deformGrid 2, 3 (Real: 86,171) to height 8477 (real is8477)
Set deformGrid 2, 4 (Real: 86,172) to height 8340 (real is8340)
Set deformGrid 3, 0 (Real: 87,168) to height 8466 (real is8466)
Set deformGrid 3, 1 (Real: 87,169) to height 8525 (real is8525)
Set deformGrid 3, 2 (Real: 87,170) to height 8612 (real is8612)
Set deformGrid 3, 3 (Real: 87,171) to height 8609 (real is8609)
Set deformGrid 3, 4 (Real: 87,172) to height 8491 (real is8491)
Set deformGrid 4, 0 (Real: 88,168) to height 8392 (real is8392)
Set deformGrid 4, 1 (Real: 88,169) to height 8540 (real is8540)
Set deformGrid 4, 2 (Real: 88,170) to height 8655 (real is8655)
Set deformGrid 4, 3 (Real: 88,171) to height 8674 (real is8674)
Set deformGrid 4, 4 (Real: 88,172) to height 8588 (real is8588)
Confirmation check:
Set deformGrid 0, 0 (Real: 84,168) to height 8392, real is 8177
Set deformGrid 0, 1 (Real: 84,169) to height 8540, real is 8081
Set deformGrid 0, 2 (Real: 84,170) to height 8655, real is 8050
Set deformGrid 0, 3 (Real: 84,171) to height 8674, real is 7944
Set deformGrid 0, 4 (Real: 84,172) to height 8588, real is 7968
Set deformGrid 1, 0 (Real: 85,168) to height 8392, real is 8218
Set deformGrid 1, 1 (Real: 85,169) to height 8540, real is 8169
Set deformGrid 1, 2 (Real: 85,170) to height 8655, real is 8305
Set deformGrid 1, 3 (Real: 85,171) to height 8674, real is 8255
Set deformGrid 1, 4 (Real: 85,172) to height 8588, real is 8145
Set deformGrid 2, 0 (Real: 86,168) to height 8392, real is 8324
Set deformGrid 2, 1 (Real: 86,169) to height 8540, real is 8412
Set deformGrid 2, 2 (Real: 86,170) to height 8655, real is 8438
Set deformGrid 2, 3 (Real: 86,171) to height 8674, real is 8477
Set deformGrid 2, 4 (Real: 86,172) to height 8588, real is 8340
Set deformGrid 3, 0 (Real: 87,168) to height 8392, real is 8466
Set deformGrid 3, 1 (Real: 87,169) to height 8540, real is 8525
Set deformGrid 3, 2 (Real: 87,170) to height 8655, real is 8612
Set deformGrid 3, 3 (Real: 87,171) to height 8674, real is 8609
Set deformGrid 3, 4 (Real: 87,172) to height 8588, real is 8491
Set deformGrid 4, 0 (Real: 88,168) to height 8392, real is 8392
Set deformGrid 4, 1 (Real: 88,169) to height 8540, real is 8540
Set deformGrid 4, 2 (Real: 88,170) to height 8655, real is 8655
Set deformGrid 4, 3 (Real: 88,171) to height 8674, real is 8674
Set deformGrid 4, 4 (Real: 88,172) to height 8588, real is 8588

Note that during the initialization loop, it reports accurate mappings, but even immediately thereafter, when we iterate through the array again, it reports that every column has been set to the last column. It has to be something really basic that I'm missing, but I can't see it!

For the output:
the first column of numbers are the array indexes. The (Real:) line are the world grid coordinates , the first height value is what is in the array, and the second is what is actually in the main terrain grid.
#2
03/13/2005 (1:01 pm)
Talk about a DUH! moment--the problem is that you don't reference arrays via mDeformGrid[x,y], but mDeformGrid[x][y].

I'll leave this up just in case anyone winds up confusing TGEScript arrays with C++ arrays like I did!