C#:如何判断碰撞发生在哪一侧?

发布于 2024-08-05 20:14:20 字数 787 浏览 1 评论 0原文

我认为标题是相当不言自明的,但只是为了澄清我试图弄清楚如何判断碰撞发生在哪一边。

更详细一点,我正在尝试制作一个类似迷宫的游戏,这样我就不能简单地在碰撞时停止所有运动。相反,我需要能够判断碰撞发生在哪一侧,以便我可以阻止该方向。

感谢任何帮助,如果有更好的方法来解决这个问题,我完全赞成尝试。

我希望这是足够的详细信息,但如果您需要更多信息,请询问,我会进行编辑。提前致谢。

[编辑] @viggity - 不,我没有使用任何特定的游戏引擎,我会发布当前的“检测”代码,但它有点,荒谬的,强大。

@Streklin - 我正在使用 this.Paint 事件来绘制表单本身,因为建议我首先这样做以更好地实时绘制。我还使用了每次计时器根据我按下的按钮(左、右、上、下)进行更新的位置。是的,迷宫是基于瓷砖的。目前它甚至只包含 3 种颜色。我不是一个非常高级的程序员。

@Eric - 绝对是一场单向游戏。同样,我只有 3 种颜色,线条是黑色,背景是白色,方块(用户)是绿色。我使用 DrawImage() 和位图在屏幕上绘制。

[编辑伪代码摘要]

foreach(墙_墙在墙上) if(player.intersectsWith(_wall)) 停止运动;

@JeffH - 我不太确定你在问什么,因为除了我用来尝试让它工作的测试代码之外,这几乎就是全部了。我唯一遗漏的是 if 语句来检查它是否是 x 轴,以便 x 和 y 可以彼此独立移动。因此,您可以靠着墙壁滑动,而不是因为碰到墙壁而被“卡住”。但我没有看到包括这一点的意义,因为问题在那之前就发生了。

I think the title is rather self explanatory but just to clarify I am trying to figure out how to tell which side the collision has occured on.

For a bit more detail, I'm trying to make a maze-like game so I can't simply stop all movement upon a collision. Instead I need to be able to tell which side the collision has happened on so I can block that direction.

Any help is appreciated and if there is a better approach to this issue, I'm all for trying it out.

I hope this is enough details but if you need anymore, ask and I'll edit. Thanks in advance.

[edit]
@viggity - No, I'm not using any specific game engine and I would post the current "detection" code but it's a little, absurdly, robust.

@Streklin - I'm using the this.Paint event to draw onto the form itself as it was recommended I start by doing that to get better at drawing real time. I'm also using a location that's updated each time the timer ticks based on what I press (left, right, up, down). Yes the maze is tile based. Currently it only consists of 3 colors even. I'm not a very advanced programmer.

@Eric - Definately a one-d game. Again, I only have 3 colors, the lines are black, the background is white and the square (the user) is green. I'm using the DrawImage() with Bitmaps to draw onto the screen.

[edit psuedo-code summary]

foreach(Wall _wall in walls)
if(player.intersectsWith(_wall))
stop movement;

@JeffH - I'm not really sure what you're asking as that's pretty much all there is besides testing code that I was using to try and get it working. The only thing I left out was the if statement to check if it was the x axis or not so that x and y could move indepedently from each other. So instead of getting "stuck" because you touched the wall, you could slide against it. I didn't see the point in including that though since the problem occurs before that.

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

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

发布评论

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

评论(2

虚拟世界 2024-08-12 20:14:21

假设您在这里谈论的是 3D 游戏。

您可以看到的脸部法线指向您,因此方向向量与脸部法线的点积将为负。如果是阳性,那么您就是从后面攻击脸部。

如果为零,则您的运动方向与面部成直角。

|              <---------- your direction of travel
|
|----------> <- face normal
|
| <- face

如果您不在 3D 环境中,那么您可以存储墙壁面向的方向(作为 2D 矢量),并与 2D 移动方向执行相同的点积。

Assuming you're talking about a 3D game here.

The normal of the face you can see points towards you, so the dot product of your direction vector with the face normal will be negative. If it's positive then you are coming at the face from the back.

If it's zero you're travelling at right angles to the face.

|              <---------- your direction of travel
|
|----------> <- face normal
|
| <- face

If you're not in 3D then you could store the direction the wall is facing (as a 2D vector) and do the same dot product with your 2D direction of movement.

枯寂 2024-08-12 20:14:21

根据您的编辑,您一次只能走一个方向?或者你可以沿着对角线方向走吗?如果是后者,ChrisF已经为您提供了3D的答案以及2D的相应信息。如果没有,您应该只需要停止沿行进方向的行进 - 因为只有四种可能性,所以应该很容易检查所有这些可能性以进行简单的入门游戏。

Based on your edit you can only go one direction at a time? Or can you go in diagonal directions? If it's the later, ChrisF has provided you the answer in 3D and the corresponding information for 2D. If not, you should just have to stop travel in the direction of travel - since there are only four possibilities it should be easy enough to check them all for simple starter game.

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