【问题标题】:Collision with rotating rectangles using libgdx?使用libgdx与旋转矩形碰撞?
【发布时间】:2016-08-16 05:02:52
【问题描述】:

在我的游戏中,玩家是一个圆形纹理,并且有很多又高又窄的旋转方形纹理。我知道如何使用Intersector 类进行碰撞,但据我发现,它不考虑旋转。是否有某种方法/类可以从纹理创建形状,然后将其用作边界?

【问题讨论】:

  • 这感觉像是一个mathematics 问题。 Google 也是您的朋友。
  • @markspace 不是。我需要一种从精灵/纹理中提取碰撞边界的方法,以考虑旋转,该方法可以与 Intersector 之类的东西一起使用。我用谷歌搜索了这个问题的废话,没有找到任何东西。
  • 并不是说这是最好的解决方案,但你可以用它们制作多边形。多边形可以旋转。
  • @eric 你知道从纹理自动创建多边形的任何方法吗?
  • 好吧,如果您知道纹理的宽度和高度,您可以从中创建一个多边形。多边形构造函数采用顶点数组。所以你会有4个顶点。左下 (0,0) 左上 (0, texture.height)、右上 (texture.width, texture.height) 和右下 (texture.width, 0)。这将创建一个可以旋转的多边形,然后您可以处理与多边形的碰撞。

标签: java android opengl-es libgdx


【解决方案1】:

我不知道这是否有课程,但我想出了一个简单的想法。

人们相信太阳围绕地球旋转多年。因为他们从地球上看到了它。 让我们假设旋转正方形是我们的世界,实际上并没有旋转。在这两种方式中,我们都知道什么时候是白天或黑夜。

看看那些图片,你会更明白。

这种情况更难检查碰撞。

而且这种情况更容易。但实际上两种情况是一样的。

所以只需将大小写更改为 2。

你知道

  • 正方形/长方形的旋转角度
  • 正方形/矩形的宽度和高度
  • 您可以找到圆与矩形/正方形中心点之间的角度。

这是正方形/矩形类中的样子。

public boolean check_collision(Player player)
{
 Vector2 pvector=new Vector2(player.xcenter , player.ycenter);
 Vector2 svector=new Vector2(this.xcenter , this.ycenter);
 float radi=player.radius;
 if( pvector.dst2(svector) < 
 (width + radi) * (width + radi) + 
 (height + radi) * (height + radi) )// dont check if player is too far for collision
 {
  Vector2 rvector= pvector.sub(svector);// rvector from square center to player center
  rvector.setangle(rvector.angle()+ this.rotation);//make sure rotations is CCW
  pvector=rvector.add(svector); //new player vector to check collision
  return new Rectangle(pvector.x-radi/2f ,pvector.y-radi/2f,radi,radi).overlaps(new Rectangle(svector.x-width/2f ,svector.y-height/2f,width,height));
  //assume that player is also rectangle because we already checked worst case with if condition.
 }
 return false;     
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多