在不重叠边界的情况下计算运动圆的碰撞 [英] Calculating collision for a moving circle, without overlapping the boundaries

查看:111
本文介绍了在不重叠边界的情况下计算运动圆的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一个矩形区域内的圆形弹跳.在某个点上,该圆会与矩形的一个表面碰撞并向后反射.我这样做的通常方法是让圆与该边界重叠,然后反映速度矢量.圆实际上与边界重叠的事实通常不是问题,在低速情况下也不会真正引起注意.以很高的速度可以很清楚地知道圆正在做它不应该做的事情.

Let's say I have circle bouncing around inside a rectangular area. At some point this circle will collide with one of the surfaces of the rectangle and reflect back. The usual way I'd do this would be to let the circle overlap that boundary and then reflect the velocity vector. The fact that the circle actually overlaps the boundary isn't usually a problem, nor really noticeable at low velocity. At high velocity it becomes quite clear that the circle is doing something it shouldn't.

我想做的是在屏幕上显示圆之前,以编程方式考虑反射并将圆放置在适当的位置.这意味着我必须计算它到达当前位置和将来位置之间边界的点-而不是计算它的新位置然后检查它是否到达边界.

What I'd like to do is to programmatically take reflection into account and place the circle at it's proper position before displaying it on the screen. This means that I have to calculate the point where it hits the boundary between it's current position and it's future position -- rather than calculating it's new position and then checking if it has hit the boundary.

这比通常的圆/矩形碰撞问题要复杂一些.我对应该如何做有一个模糊的想法-基本上在当前位置和新位置之间创建一个边界矩形,这带来了一系列的问题(因为矩形是根据圆的方向旋转的速度).但是,我认为这是一个常见问题,并且已经存在一个通用解决方案.

This is a little bit more complicated than the usual circle/rectangle collision problem. I have a vague idea of how I should do it -- basically create a bounding rectangle between the current position and the new position, which brings up a slew of problems of it's own (Since the rectangle is rotated according to the direction of the circle's velocity). However, I'm thinking that this is a common problem, and that a common solution already exists.

是否存在针对此类问题的通用解决方案?也许我应该研究一些基本理论?

Is there a common solution to this kind of problem? Perhaps some basic theories which I should look into?

推荐答案

由于您只有一个圆形和一个矩形,因此实际上非常简单.可以将半径为r的圆在尺寸为w, h的矩形内弹跳,可以将其视为与圆心在矩形(w-r), (h-r)内的点p相同.

Since you just have a circle and a rectangle, it's actually pretty simple. A circle of radius r bouncing around inside a rectangle of dimensions w, h can be treated the same as a point p at the circle's center, inside a rectangle (w-r), (h-r).

现在位置更新变得简单.给定您的点在位置x, y和每帧速度dx, dy,更新的位置是x+dx, y+dy-除非您越过边界.例如,如果最后得到x+dx > W(放下W = w-r),则请执行以下操作:

Now position update becomes simple. Given your point at position x, y and a per-frame velocity of dx, dy, the updated position is x+dx, y+dy - except when you cross a boundary. If, say, you end up with x+dx > W (letting W = w-r), then you do the following:

crossover = (x+dx) - W // this is how far "past" the edge your ball went
x = W - crossover // so you bring it back the same amount on the correct side
dx = -dx // and flip the velocity to the opposite direction

并且对于y同样.您必须为每个维度中的相反边界设置类似的(反射的)检查.

And similarly for y. You'll have to set up a similar (reflected) check for the opposite boundaries in each dimension.

这篇关于在不重叠边界的情况下计算运动圆的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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