旋转矩形碰撞 [英] Rotate a rectangle collision

查看:62
本文介绍了旋转矩形碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

我为我的游戏写了一个非常简单的碰撞:



Hi.
I write a very simple collision for my game :

bool MRectangle::HitTo( MRectangle other )
{

    if (position.X+this->w<other.position.X) return false;
    if (position.X>other.position.X+other.w) return false;
    if (position.Y+this->h<other.position.Y) return false;
    if (position.Y>other.position.Y+other.h) return false;
    return true;
}





这是'的工作。但如果我想旋转矩形我应该怎么做?

谢谢



Its''s work. But if I want rotate the rectangle what should I do ?
Thanks

推荐答案

它取决于你旋转它的距离,以及哪个方向。但是,猜测一下,您需要跟踪哪个侧面或角落指向窗口的哪个边缘。因此,如果顺时针旋转90度,顶部变为右侧,右侧变为底部等。
It rather depends on how far you rotate it, and in which direction. But, at a guess, you need to keep track of which side or corner is pointing at which edge of your window. So, if you rotate clockwise by 90 degrees, the top becomes the right side, the right becomes the bottom, etc.


如果您想知道如何测试旋转的矩形以进行碰撞搜索超平面分离定理...我给你你需要的例子...

如果你想旋转使用旋转矩阵...... :)

因为我看到你的工作在2d所以它更容易...





if you want to know how to test rotated rect for collisions search for hyperplane separation theorem... i give you the example you need...
if you want to rotate use rotation matrix... :)
as i see you work in 2d so its easier...


1)for each rectangle(lets call this own_rect)

   2)for each line in the ownRect

      3)check the points of the other rectangle(lets call this other_rect)

      4)if all the points of the other_rect are on the same side of the line AND 
         all the points of the own_rect are on the other side of(or on) the line
         ->there is no collision

      5)else continue (there may be a collision but we are not sure yet)





找不到分离矩形的线es意味着你有一个碰撞:)



这个算法很容易在三个维度上使用...但不是你有飞机的线...... />


我希望我能帮助.. !!! :)



Not finding a line that "separates" the rectangles means you have a collision :)

it is very easy to use this algorithm in 3 dimensions... but instead of lines you have planes...

I hope i helped..!!! :)


检查两个任意旋转的矩形的碰撞几乎是微不足道的。基本上你必须像对待两个凸多边形一样对待它们。有两种算法可用于检查两个多边形的重叠。属性凸是在这个上下文中,因为它使交叉多边形的过程更容易。



你可能想要开始在其中 [ ^ ]







< a href =http://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm>这里 [ ^ ]



这些算法中的大部分都是按照目标设计的削减图形,但你可以使用它们来确定一个交叉点同样好。



在你去那里之前,你可能想花几分钟思考关于您想要存储旋转矩形的格式。这也可能会引导您进行多边形表示。
Checking for collisions of two arbitrarily rotated rectangles is all but trivial. Basically you have to treat them like two convex polygons. There are a couple of algorithms around for checking the overlap of two polygons. The attribute "convex" is in that context, as it makes the process of intersecting polygons easier.

You might want to start here[^]

and

here[^]

Most of these algorithms have been designed with the goal of clipping graphics, but you can use them for the purpose of determining an intersection equally well.

Before you go there, you might want to spend a couple of minutes thinking about the format in which you want to store your rotated rectangles. That might lead you to a polygon representation as well.


这篇关于旋转矩形碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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