gpt4 book ai didi

c++ - 发布中的错误而不是调试中的错误

转载 作者:行者123 更新时间:2023-11-28 00:26:14 26 4
gpt4 key购买 nike

我正在为路径规划/防撞方法构建一个自上而下的 View 演示。 Visual Studio 2010 中的 c++。

当我在 Debug模式下运行时,一切正常。但是当我在 Release 中运行时,疯狂的行为发生了。以前我的角色在遇到其他角色时会大幅加速并改变方向,现在他们只是消失(可能移动到屏幕外的某个地方)。

一查,这通常是浮点运算引起的。我删除了所有 float ,浮点模型已经在 fp:precise 上用于调试和发布。我不知道从这里去哪里......

我很确定这段代码是错误的来源。每个字符都可以在一个单元格中找到。在单元格边缘,存储了角色的平均速度(cell->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 将之前使用时剩余的 ram 留给它们。

关于c++ - 发布中的错误而不是调试中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24890922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com