2D 盒子碰撞 - 我做错了什么? (C++)

发布于 2024-11-03 07:06:02 字数 1520 浏览 1 评论 0原文

我正在尝试用 C++ 制作一款平台游戏,并且制作了一个块向量, 我只是循环遍历向量并单独检查碰撞:

//Pseudo code
class Block{
...int x
...int y
...int width
...int height
};

class Player{
int x
int y
int width
int height
int hsp //horizontal speed
int vsp //vertical speed
int facing //0 = no direction, -1 = left, 1 = right
...



void loop()
{

    if(keyboard_pressed(key_left) { x-=hsp; facing = -1;}
    if(keyboard_pressed(key_right) {x+=hsp; facing = 1;}
    if(keyboard_pressed(key_up) {y-=vsp;}
    if(keyboard_pressed(key_down) {y+=vsp;}
    if(keyboard_released(key_left | key_right) {facing = 0;}

    for(int i = 0; i < blocks.size(); i++)
    {
        Block b = blocks.at(i);
        check_Collision(b);
    }
}

};

如您所见,我的玩家只是根据 hsp 和 vsp 移动。够简单的。 我的问题的主要部分是在 check_Collision() 中。首先我检查玩家是否 在街区的顶部,如果他在,就让他留在那里。 然后我检查玩家是否在方块的两侧。 但由于某种原因出现了问题。出于某种原因,当我走到街区顶部时, 他留在顶部,但随后他被转移到左侧。

老实说,我不知道该去哪里。 以下代码仅检查顶部和左侧:

check_Collision(){
///////////////////////////////////
var myLeft, myRight, myTop, myBot;
var bLeft, bRight, bTop, bBot;
myLeft = x;
myRight = x + width;
myTop = y;
myBot = y + height;
/////////////////////
bLeft = b.x;
bRight = b.x + b.width;
bTop = b.y;
bBot = b.y + b.height;
//////////////////////////////////


//Check if we are at the top
if(myBot + vsp > bTop+1){
y = bTop - height;
}

//Check if we are at the sides
if(myBot > bTop+2){

    if(myRight + hsp > bLeft)
    {
        x = bLeft - width;
    }

}
}

如果有人可以向我指出一些有关 2D 盒子碰撞的教程,那就太好了。

I'm trying to make a platformer game in C++ and I have made a vector of blocks,
and I simply loop through the vector and check for the collision individually:

//Pseudo code
class Block{
...int x
...int y
...int width
...int height
};

class Player{
int x
int y
int width
int height
int hsp //horizontal speed
int vsp //vertical speed
int facing //0 = no direction, -1 = left, 1 = right
...



void loop()
{

    if(keyboard_pressed(key_left) { x-=hsp; facing = -1;}
    if(keyboard_pressed(key_right) {x+=hsp; facing = 1;}
    if(keyboard_pressed(key_up) {y-=vsp;}
    if(keyboard_pressed(key_down) {y+=vsp;}
    if(keyboard_released(key_left | key_right) {facing = 0;}

    for(int i = 0; i < blocks.size(); i++)
    {
        Block b = blocks.at(i);
        check_Collision(b);
    }
}

};

As you can see, my player simply moves according to hsp and vsp. Simple enough.
The main portion of my question is in check_Collision(). First I check to see if the player
is on top of the block, and if he is, let him stay there.
Then I check if the player is at the sides of the block.
But for some reason there's a problem. For some reason when I go under the top of the block,
he stays at the top, but then he gets shifted to the left side.

I honestly don't know where to go with this.
The following code only checks for the top and the left side:

check_Collision(){
///////////////////////////////////
var myLeft, myRight, myTop, myBot;
var bLeft, bRight, bTop, bBot;
myLeft = x;
myRight = x + width;
myTop = y;
myBot = y + height;
/////////////////////
bLeft = b.x;
bRight = b.x + b.width;
bTop = b.y;
bBot = b.y + b.height;
//////////////////////////////////


//Check if we are at the top
if(myBot + vsp > bTop+1){
y = bTop - height;
}

//Check if we are at the sides
if(myBot > bTop+2){

    if(myRight + hsp > bLeft)
    {
        x = bLeft - width;
    }

}
}

If anyone can point me into some tutorial on 2D box collision that would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星軌x 2024-11-10 07:06:02

你使用的逻辑对我来说没有意义。仅仅检查玩家是否位于方块下方还不够:您是否还需要确保玩家站在方块上,即是否距离方块右侧或左侧太远?同样,在第二次检查中,您必须确保玩家​​没有跳过方块(或站在上面)。您的 if 部分检查了这一点,但没有考虑到第一次检查可能已修改玩家位置的事实。

你能假设玩家永远不能在方块下面行走吗?您是否可以假设玩家永远不会移动得足够快以完全“隧道”穿过方块(hsp > b.width 等)?

如果其中任何一个的答案是否定的,您将需要更加复杂的碰撞检测。

The logic you're using doesn't make sense to me. It's not enough just to check that the player is under the block: don't you also need to make sure that the player is standing on it, ie, isn't too far to the right or left of the block? Similarly, in your second check, you've got to make sure that the player isn't jumping over the block (or standing on it). Your if partially checks this, but doesn't take into account the fact that the first check might have modified player position.

Can you assume that the player can't ever walk under the block? Can you assume that the player will never move fast enough to "tunnel" completely through the block (hsp > b.width, etc)?

If the answer to either of these is no, you will need significantly more sophisticated collision detection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文