gpt4 book ai didi

java - 正方形之间的 2D 碰撞检测,简单但比 boolean 值更具体 + 不受大空间跳跃的影响

转载 作者:行者123 更新时间:2023-11-29 09:24:10 27 4
gpt4 key购买 nike

想知道玩家从哪个方向撞击地形 block (只是简单的上/下、左/右)。我发现的所有内容要么太简单,要么复杂得多,似乎无法满足我的需要,例如 AABB(虽然很难说,但我的大脑难以消化真正长的方程式)。到目前为止,我得到的是今天花更多时间阅读和试验的结果:

public int move(double toX, double toY) {

int col = COLLISION_NONE; //these are bit flags, in case I collide with a block to my right as well as below me

double nextX = mX+(toX*main.getDelta()); //delta regulates speed
double nextY = mY+(toY*main.getDelta());

if(mTerrainCollision){

int w = GameView.GameLoop.TILE_WIDTH;
int h = GameView.GameLoop.TILE_HEIGHT;

for(int i = -2; i <= 2; i++) //broad tile picking will be optimized later, better trace around players path
for(int j = -2; j <= 2; j++) {
GameTerrain.Block block = main.mTerrain.get(((int)Math.round(mX)/w)+i,((int)Math.round(mY)/h)+j);
if(block.type != GameTerrain.BLOCK_TYPE_NONE) {

if(nextX+w >= block.x() && mX+w <= block.x()){ //COLLISION ON THE RIGHT?
if(mY+h > block.y() && mY < block.y()+h) { //<THIS is a problem line, see below
nextX = block.x() - w;
xMomentum = 0;

col |= COLLISION_RIGHT;
}
}

else if(nextX < block.x()+w && mX >= block.x()+w){ //COLLISION ON THE LEFT?
if(mY+h > block.y() && mY < block.y()+h) { //same as above, make sure were on the same plane
nextX = block.x() + w;
xMomentum = 0;

col |= COLLISION_LEFT;
}
}


if(nextY+h >= block.y() && mY+h <= block.y()){ //COLLISION ON THE BOTTOM?
if(mX+w > block.x() && mX < block.x()+w) { //make sure were on the same plane
nextY = block.y() - h;
yMomentum = 0;

col |= COLLISION_DOWN;
}
}

else if(nextY < block.y()+h && mY >= block.y()+h){ //COLLISION ON THE TOP?
if(mX+w > block.x() && mX < block.x()+w) { //make sure were on the same plane
nextY = block.y() + h;
yMomentum = 0;

col |= COLLISION_UP;
}
}
}
}
}

mX = nextX;
mY = nextY;

return col;

}

它有效……主要是。即使经过长时间的 sleep 使 delta 飙升,玩家也不会逐步通过 block 。除非玩家之前的位置 (mX/mY) 与我们正在检查的方 block 不在同一平面上,否则碰撞检测本身会起作用(请参阅带有“THIS...”的注释行)。假设我们与一个方 block 完全成对角线并直奔它,玩家将直接穿过。我一直在挠头一段时间,试图弄清楚如何解决最后一个问题,最好不要对所有内容进行大修,但如果必须解决这个问题,哦,好吧!我只对简单的碰撞数据感兴趣,例如“这一帧我碰到地板了吗?好吧,我可以跳”,“我现在是在碰墙吗?好吧,我可以跳墙,但如果我也碰到了地板,那就不行了” "等

最佳答案

根据对象的 AABB 大小增加墙的 AABB(保持墙的 AABB 中心固定),从对象的之前之后位置构造一条线段(使用对象的 AABB 中心),然后执行 segment-AABB intersection test .

关于java - 正方形之间的 2D 碰撞检测,简单但比 boolean 值更具体 + 不受大空间跳跃的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4007072/

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