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:
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)
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)
#2
I'll leave this up just in case anyone winds up confusing TGEScript arrays with C++ arrays like I did!
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!
Torque 3D Owner Stephen Zepp
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.