简体   繁体   English

在没有Box2D的情况下为libgdx Actor实现碰撞检测

[英]Implementing collision detection for libgdx Actors without Box2D

I'm trying to implement collision detection for libgdx actors (player and enemies) without employing Box2D. 我试图在不使用Box2D的情况下为libgdx演员(玩家和敌人)实施碰撞检测。 As I've read Box2D supports inbuilt collision detection, but as my game does not involve any physics in the environment, I'm not comfortable with using Box2D just for that. 正如我阅读的Box2D支持内置的碰撞检测一样,但是由于我的游戏不涉及环境中的任何物理因素,因此我不愿意为此专门使用Box2D。

Many examples I found enable collision detection by defining a bounding box (Rectangle) for this, but I'm looking for an inbuilt solution. 我发现许多示例都通过为此定义一个边界框(矩形)来启用冲突检测,但是我正在寻找一种内置解决方案。

If you do not wish to use Box2d Inbuilt collision then you can always use the Rectangle's overlap method to check whether is some rectangles have been overlapped (or collided) with each other. 如果您不希望使用Box2d内置碰撞,则可以始终使用Rectangle的overlay方法来检查是否某些矩形相互重叠(或碰撞)。

So a Rectangle overlap method works something like this: 因此,矩形重叠方法的工作原理如下:

Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle();
if(rect1.overlaps(rect2)){ //do your stuff here }

One thing to note here is that, you can always put your texture at the same place where your rectangle is. 这里要注意的一件事是,您可以始终将纹理放置在矩形所在的位置。 Hence colliding rectangle also get you colliding your sprites/textures too. 因此,碰撞矩形也会使您碰撞精灵/纹理。

And i hope you might already know how to do that, but still below is an example: 我希望您可能已经知道该怎么做,但是下面仍然是一个示例:

batch.draw(myTexture,rect1.x,rect1.y,100,100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM