碰撞和弹跳从检测点的数组 [英] Collision and bounce detection from array of points

查看:120
本文介绍了碰撞和弹跳从检测点的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我将会用来产生在一个游戏阶段(2D)的外侧闭合多边形围栏点的阵列。我希望有这墙,在里面一个反弹球状物体之间的碰撞检测。

I have an array of points that I will use to generate a closed polygonal fence on the outside of a game stage (2D). I wish to have collision detection between this fence and a bouncing ball-like object on the inside.

此外,我希望能够任意添加/删除/重绘实时栅栏,并有碰撞检测还是现实操作。

Additionally, I would like to be able to arbitrarily add/remove/redraw the fence in realtime and have the collision detection still operate realistically.

我也考虑过画一个雪碧/形状的点,做的HitTest在每帧检查是否反弹与否。

I have considered drawing a Sprite/Shape from the points and doing a HitTest at each frame to check whether to bounce or not.

我的问题:这是实现这一目标的最好/正确的方法是什么?考虑像JezzBall与斜线任意角度的模拟什么,我试图做的。

My question: is this the best/correct way to accomplish this goal? Consider something like JezzBall with diagonal lines of any angle a simulation of what I'm trying to do.

推荐答案

数学会在这里你的朋友。做一个快速搜索圆线,或点 - 线碰撞(这里有一个:环线碰撞检测)。

Math will be your friend here. Do a quick search for circle-line, or point-line collision (here's one: Circle line collision detection).

您做的是通过你的点阵列上运行,创造线条。所以第1行将以点[0],点[1],和线路2将点[1]和点[2]。之后,你检查每一行对你的球(如果你想适当的碰撞,将工作无论帧速率,然后创建一个球线的,这是球沿之间的旅行线路最后一帧而这一次)。做你的碰撞检测对球线,并从点(有吨线 - 线碰撞检测交易算法在网络上)做每一行。您将获得了一个这样的算法是冲突发生在当前时间步长,以及正常碰撞路线,这将给你的反射角的时间。

What you do is run through your array of points, creating lines. So line 1 will be points[0] and points[1], and line 2 will be points[1] and points[2]. After that you check each line against your ball (if you want proper collision that will work no matter the frame rate, then you create a ball line, which is the line that the ball has travelled along between the last frame and this one). Do your collision detection against the ball line and each line made from your points (there's tons of line-line collision detection algos on the web). What you'll get out of an algorithm like that is the time the collision takes place in the current time step, as well as the normal of the colliding line, which will give you the reflection angle.

如果您不知道矢量数学,然后了解它,它会让你的生活更轻松吨。同样,也有吨的净Vector2类的实现的。

If you don't know Vector math, then learn it, it'll make your life a ton easier. Again, there are tons of implementations of a Vector2 class on the net.

根据需要只是忽略那些点入住,您可以随意取下墙上的部分。

You can arbitrarily remove parts of the wall as needed by just ignoring those points in your check.

另一个懒惰的解决办法是使用物理引擎像的Box2D http://box2dflash.sourceforge.net/或颈背: HTTP://$c$c.google.com/p/nape/ - 这可能是矫枉过正,你想为你的游戏是什么,但是,嘿,这很容易

Another lazy solution would be to use a physics engine like Box2D http://box2dflash.sourceforge.net/ or Nape: http://code.google.com/p/nape/ - it might be overkill for what you want for your game, but hey, it's easy.

有关加分,这可能是您更轻松的另一种技术是分离轴定理,它采用的是flash游戏N:的 http://www.metanetsoftware.com/technique.html

For bonus points, another technique which might be easier for you is the Separating Axis Theorem, which is used in the flash game N: http://www.metanetsoftware.com/technique.html

这篇关于碰撞和弹跳从检测点的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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