簡體   English   中英

版本中的錯誤不在調試中

[英]Bugs in Release not in Debug

我正在為路徑規划/避免沖突方法構建一個自頂向下的視圖演示。 Visual Studio 2010中的c ++。

當我以Debug模式運行時,一切都很好。 但是當我在Release中運行時,會發生瘋狂的行為。 以前,我的角色遇到其他角色時會極大地加快速度並改變方向,而現在卻消失了(可能移動到屏幕外的某個地方)。

搜索表明這通常是由於浮點運算引起的。 我刪除了所有浮點,並且對於調試和發行版,浮點模型已經在fp:precise上。 我不知道從這里去哪里...

我很確定這段代碼是錯誤的根源。 每個字符都可以在一個單元格中找到。 在像元邊緣上,存儲了字符的平均速度(像元-> vLeft等),並且此角色想將自己的速度與像元邊緣上存儲的速度進行平均。

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

}

任何幫助將不勝感激!

編輯:這是_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);
}

切換時的一個常見故障是未初始化變量的行為。 您是否曾經將所有uicGrids [] []都設置為某個初始值?

通常,Debug會為您自動將它們初始化為0。.Release使它們與以前使用時剩余的內存無關。

如果以上猜測均無濟於事,那么調試的一種有效方法是暫時添加許多日志記錄語句並將其寫入日志文件,以便您可以跟蹤代碼執行並查看變量的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM