2D平台游戏碰撞问题在两个轴上两个、问题、平台、游戏

2023-09-07 12:53:08 作者:趁现在

我工作的一个小2D平台游戏/格斗游戏使用C ++和SDL,和我有相当多的麻烦与冲突检测。

水平是由瓷砖组成的数组,我用一个for循环要经历每一个(我知道这可能不是做的最好的方式,我可能需要帮助,太多)。对于角色的每一面,我移动一个像素在那个方向,并检查是否有冲突(我也检查,看看是否字符在朝这个方向发展)。如果有冲突,我设定速度为0,玩家移动到瓦片的边缘。

我的问题是,如果余检查水平碰撞第一和玩家垂直移动以每帧一个以上的象素,它处理水平碰撞并移动的字符的块的侧面,即使瓦低于(或以上)的字符。如果我第一次办理的垂直碰撞,但它确实是相同的,但它确实是水平轴。

如何处理两轴碰撞,而无需这些问题?有没有更好的方式来处理比碰撞我如何做?

解决方案

XNA的2D一些成熟的例子使用基于区块的碰撞也是如此。他们处理的方式有pretty的简单,可能对您有用。下面是一个什么样的在那里一个精简的解释(除去具体到他们的-演示的东西):

后的移动应用,它会检查冲突。 ,决定了播放机重叠基于玩家的边界框的瓷砖。 在它通过遍历所有这些砖... 如果被检查瓷砖是不是差强人意: 它确定多远在X轴和Y轴玩家重叠非可通过瓦片 在冲突解决仅在浅层轴: 如果Y是浅轴(ABS(overlap.y)< ABS(overlap.x)),position.y + = overlap.y;同样,如果X是浅轴。 边框是根据位置变化更新 将下一个瓷砖...

这是在HANDLECOLLISIONS player.cs()函数,如果你抓住了code,并希望看到什么,他们专门在那里做。

I'm working on a little 2D platformer/fighting game with C++ and SDL, and I'm having quite a bit of trouble with the collision detection.

2D平台砍杀动作游戏 城堡掠夺者 专题上线

The levels are made up of an array of tiles, and I use a for loop to go through each one (I know it may not be the best way to do it, and I may need help with that too). For each side of the character, I move it one pixel in that direction and check for a collision (I also check to see if the character is moving in that direction). If there is a collision, I set the velocity to 0 and move the player to the edge of the tile.

My problem is that if I check for horizontal collisions first, and the player moves vertically at more than one pixel per frame, it handles the horizontal collision and moves the character to the side of the tile even if the tile is below (or above) the character. If I handle vertical collision first, it does the same, except it does it for the horizontal axis.

How can I handle collisions on both axes without having those problems? Is there any better way to handle collision than how I'm doing it?

解决方案

XNA's 2D platformer example uses tile-based collision as well. The way they handle it there is pretty simple and may useful for you. Here's a stripped down explanation of what's in there (removing the specific-to-their-demo stuff):

After applying movement, it checks for collisions. It determines the tiles the player overlaps based on the player's bounding box. It iterates through all of those tiles...

If the tile being checked isn't passable: It determines how far on the X and Y axes the player is overlapping the non-passable tile Collision is resolved only on the shallow axis:

If Y is the shallow axis (abs(overlap.y) < abs(overlap.x)), position.y += overlap.y; likewise if X is the shallow axis. The bounding box is updated based on the position change

Move on to the next tile...

It's in player.cs in the HandleCollisions() function if you grab the code and want to see what they specifically do there.