【问题标题】:libGDX, detect side touching between rectangles (side collision)libGDX,检测矩形之间的侧面接触(侧面碰撞)
【发布时间】:2014-03-17 20:40:24
【问题描述】:

我为我的游戏使用 libGDX 库。我使用overlap方法检测两个矩形之间的碰撞检测。

...
if (r1.overlaps(r2)) collisionTest();
...

我想检测矩形上的触摸边(顶部、底部、左侧或右侧):

r1 overlap r2 on the left side

谁能给我这个代码,但这需要快速的方法。

谢谢

【问题讨论】:

    标签: java android libgdx


    【解决方案1】:

    您可以使用Intersector 类中提供的方法intersectRectangles 来确定两个矩形是否重叠,如果是,它们重叠的位置。您可以使用此信息来确定它们是否与左侧、右侧、顶部和/或底部重叠。

    Rectangle r1 = /*Initialize*/;                             
    Rectangle r2 = /*Initialize*/;                             
    Rectangle intersection = new Rectangle();                  
    Intersector.intersectRectangles(r1, r2, intersection);     
    if(intersection.x > r1.x)                                  
        //Intersects with right side                              
    if(intersection.y > r1.y)                                  
        //Intersects with top side                                
    if(intersection.x + intersection.width < r1.x + r1.width)  
        //Intersects with left side                               
    if(intersection.y + intersection.height < r1.y + r1.height)
        //Intersects with bottom side    
    

    【讨论】:

      猜你喜欢
      • 2014-02-07
      • 1970-01-01
      • 2013-06-18
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      相关资源
      最近更新 更多