所以我正在做一个简单的碰撞检测,每次更新时,玩家都会向下移动 3 次,而不是在地面上,但当它在地面上时,它会移动它的位置和地面的差异。但我感到紧张(它上下移动太多,并且没有完全静止)。
我的问题是:如何让这段代码正确计算差异?
Vector3f pos = new Vector3f(25,-50,25);
//Vector3f pos = new Vector3f(25,-50,25); isn't actually in the update method,
//but is in the object's constructer.
onGround = false;
Vector3f projPos = new Vector3f(pos);
projPos.y += fallSpeed;
//get a vector of all the triangles touching the player
Vector<Tri> tris = getTrisTouching(pos);
float minY;
//make it so if we don't have a list we don't get tossed into infinity
if(tris.size() > 0) minY = Float.POSITIVE_INFINITY;
else minY = 0;
for(int i = 0; i < tris.size(); i++){
Tri tri = tris.get(i);
if(projPos.y + radius <= tri.max.y){
float difference = tri.min.y - projPos.y;
if(difference < minY) minY = difference;
onGround = true;
}
}
if(onGround){
pos.y = minY;
}
else{
pos.y = projPos.y;
}
最佳答案
您的碰撞检测代码(第一个“if”语句内的 3 行)令人困惑,但我们假设当您在地面上时,玩家不会接触任何三角形(因为它可能比它们高 1 个像素) ),所以下一帧你会向下移动,然后下一帧你会向上移动。
解决办法可能是在 for 循环中使用 projPos 而不是实际位置。
关于java - 碰撞检测中出现抖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11180700/