简体   繁体   中英

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);
}

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.

如果以上猜测均无济于事,那么调试的一种有效方法是暂时添加许多日志记录语句并将其写入日志文件,以便您可以跟踪代码执行并查看变量的值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM