如何在许多墙壁(迷宫)中进行碰撞检测? [英] How to do collision detection with many walls (maze)?

查看:118
本文介绍了如何在许多墙壁(迷宫)中进行碰撞检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的游戏中,玩家在迷宫中导航.我不知道如何对墙壁进行适当的碰撞检测.对于停留在特定区域中的碰撞检测很容易:

if (x > rightWallX - playerWidth) x = rightWallX - playerWidth;
if (x < leftWallX) x = leftWallX;
//...

但是我要如何对许多墙进行碰撞检测?

我可以进行无修正的普通碰撞检测(如if (intersecting) return true;),但是我不能正确地进行修正.如果我只存储旧的xy并将其重置,则

  1. 物体从未真正接触过墙壁
  2. 如果该对象可以上升但在右侧被挡住,则它不会上升,就不会移动.

如何在迷宫中进行碰撞检测?

解决方案

解决碰撞检测后,最简单的方法是 fix 将碰撞移动到如果不是与其碰撞的对象,则actor最接近该actor的有效位置.这没有惯性,但足以用于类似迷宫的游戏或自上而下的地图爬行游戏.

如果要进一步简化计算,可以将自己限制在检测更改actor的xy坐标是否更好.如果您的actor具有一个与轴线对齐的矩形命中框,并且所有障碍物也与一个与轴线对齐的矩形(最简单的情况),则此假设确实是正确的.但是,在其他一些情况下,结果可能并不令人满意(潜在的伪影:从对角线壁滑行而提速-在大多数迷宫游戏中并非如此).

请记住,多个碰撞可能同时发生(推向两堵墙).如果两面墙之间没有锐角,演员都可以相交(例如,如果所有障碍物都对齐并且间隔足够大),则依次固定每个碰撞就足够了-只是在第一次碰撞后不要停止.

In my game, the player navigates a maze. I can't figure out how to do proper collision detection with the walls. It is easy to do collision detection for staying in a certain area:

if (x > rightWallX - playerWidth) x = rightWallX - playerWidth;
if (x < leftWallX) x = leftWallX;
//...

But how would I do collision detection for many walls?

I can do plain collision detection without correction (like if (intersecting) return true;), but I can't correct this correctly. If I just store the old x and y and reset them, then

  1. The object never actually touches the wall
  2. If the object can go up but is blocked to the right, it won't go up, it will just not move.

How is collision detection in a maze done?

解决方案

The easiest way, once you have solved collision detection, to fix the collision is to move the actor to the closest valid position to where the actor would be were it not for the object it collides with. This assumes no inertia, but it is sufficient for maze-like games or top-down map-crawling games.

If you want to simplify your calculations further, you can limit yourself to detecting if changing the actor's x or y coordinate would be better. If your actor has an axis-aligned rectangular hit-box and all obstacles are axis-aligned rectangular as well (the simplest case), this assumption is indeed correct. However, the results might not be satisfactory in some other cases (potential artifact: speed boost from gliding diagonal walls - not the case in most maze games).

Keep in mind multiple collisions could happen concurrently (pushing against two walls). If there are no sharp angles between two walls that an actor could both intersect (say, if all your obstacles are axis aligned and sufficiently spaced), fixing each collision in turn will suffice - just don't stop after the first collision.

这篇关于如何在许多墙壁(迷宫)中进行碰撞检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆