Tessa
Tessa

Reputation: 317

Bugs in Release not in Debug

I'm building a top-down view demo for a path planning/collision avoidance method. c++ in Visual Studio 2010.

When I run in Debug mode everything is fine. But when I run in Release, crazy behavior occurs. Previously my characters would speed up immensily and change direction when running into other characters, and now they are just dissapearing (probably moving somewhere outside the screen).

A search showed that this is usually because of floating point operations. I removed all floats, and Floating Point Model was already on fp:precise for both Debug and Release. I have no idea where to go from here...

I'm pretty sure this code is the source of the errors. Each character can be found in a cell. On the cell edges, the average velocity of the characters there is stored (cell->vLeft etc), and this character wants to average it's own velocity with that stored on the cell edges.

void CharacterQueryStructure_Grid::computeActualVelocity(Character& character, double dt){

// find the cell for this character
const CellCoord& cellID = _posToCell2D(character.getPosition());
int x = cellID.first, y = cellID.second;
int layerID = character.getLayer();

if(x == -1 && y == -1){ // if the position is invalid, return no neighbours
    //TODO: ERRORRRR
    //return Point(NULL, NULL);
    character.setNewVelocity_EulerIntegration(character.getPreferredVelocity(), dt);
}
else{

    //Interpolate between the four points to find the average velocity at that location
    UIC_cell* cell = uicGrids[layerID][_getCellID(x,y)];

    double distLeft = (character.getPosition().x - xMin) - cellSize * x;
    double distBot  = (character.getPosition().y - yMin) - cellSize * y;

    //First find the interpolated velocity at the location of your projection on the horizontal and vertical axes.
    Point vHor = cell->vLeft * ((cellSize - distLeft)/cellSize) + cell->vRight * (distLeft/cellSize);
    Point vVer = cell->vBot  * ((cellSize - distBot)/cellSize)  + cell->vTop * (distBot/cellSize);

    //Now interpolate these to your location
    double distHor = abs(distLeft - .5 * cellSize);
    double distVer = abs(distBot - .5 * cellSize);

    //Point vAverage = vHor * (distHor / (.5 * cellSize)) + vVer * (distVer / (.5 * cellSize));

    Point vAverage = (vHor + vVer) / 2;


    //Calculate the new velocity based on your own preferred velocity, the average velocity and the density in your cell.
    Point newVelo = character.getPreferredVelocity() + (cell->density / maxDensity) * (vAverage - character.getPreferredVelocity());

    // set it, while adhering smoothness constraints
    character.setPreferredVelocity(newVelo);
}   

}

Any help would be greatly appreciated!

Edit: And here is _posToCell2D...

CellCoord CharacterQueryStructure_Grid::_posToCell2D(const Point& pos) const
{
    int x = (int)floor((pos.x - xMin) / cellSize);
    int y = (int)floor((pos.y - yMin) / cellSize);

    // if this coordinate lies outside the grid: return nothing
    if(x < 0 || y < 0 || x >= xNrCells || y >= yNrCells)
        return CellCoord(-1,-1);

    // otherwise, return the correct cell ID
    return CellCoord(x,y);
}

Upvotes: 0

Views: 273

Answers (2)

ScottMcP-MVP
ScottMcP-MVP

Reputation: 10415

If none of the above guesses help then a productive method of debugging this is to temporarily add a lot of logging statements and write to a log file so you can trace the code execution and see the value of variables.

Upvotes: 1

Yeraze
Yeraze

Reputation: 3289

One common glitch when switching is the behavior of uninitialized variables. Do you ever set all of the uicGrids[][] to some initial value?

Frequently, Debug will automagically initialize these to 0 for you.. Release leaves them with whatever ram is leftover from previous usage.

Upvotes: 2

Related Questions