简体   繁体   English

LibGDX 光线投射不与 Box2D 碰撞 static object

[英]LibGDX raycast does not collide with Box2D static object

I'm trying to implement a simple AI for a 2D game in libGDX.我正在尝试在 libGDX 中为 2D 游戏实现一个简单的 AI。 The AI should make an enemy follow the player if the player is in range and if the enemy can see the player.如果玩家在范围内并且敌人可以看到玩家,AI 应该让敌人跟随玩家。 To determine whether the enemy can see the player I'm using a raycast .为了确定敌人是否可以看到我正在使用raycast的玩家。

Now my problem is, that the raycast does not seem to collide with a static body in the Box2D world .现在我的问题是,光线投射似乎没有与 Box2D 世界中的 static 主体发生碰撞

(It's just an assumption, that the static body is the problem, because it's working fine with a dynamic body ) (这只是一个假设, static 主体是问题,因为它与动态主体一起工作正常)

The Question问题

Do raycasts in libGDX collide with static bodies by default?默认情况下,libGDX 中的光线投射是否与 static 物体发生碰撞? Or is there an other way to make a raycast collide with static bodies?还是有其他方法可以使光线投射与 static 物体发生碰撞?

I couldn't find any information on this in the libGDX wiki or in the API .我在libGDX wikiAPI中找不到任何相关信息。

What I've tried so far到目前为止我尝试过的

I've tested the raycast AI with a dynamic body in between the enemy and the player:我已经用敌人和玩家之间的动态物体测试了光线投射 AI: 敌人和玩家之间的动态身体

Here the enemy with the raycast AI (the one in the upper left) is not moving towards the player (in the lower right) because there is a dynamic body in between them (the other enemy in the middle), which is correctly colliding with the raycast.在这里,带有光线投射 AI 的敌人(左上角的那个)没有向玩家(右下角)移动,因为它们之间有一个动态物体(中间的另一个敌人),它正在正确地与光线投射。

But if I put a static body in between the enemy and the player the raycast won't detect the static body, but only the dynamic body (that should be found later than the static body):但是如果我在敌人和玩家之间放置一个 static 身体,光线投射将不会检测到 static 身体,而只会检测到动态身体(应该比 ZA81259CEF8E959C6224DF1D456E 身体晚)

敌人和玩家之间的静态身体

Here the static body in the middle is not detected by the raycast.此处光线投射未检测到中间的 static 主体。

I've also tested to use setSleepingAllowed(false) on the static body, to make sure it's not just sleeping.我还测试了在 static 主体上使用setSleepingAllowed(false) ,以确保它不仅仅是在睡觉。 But that also didn't fix it.但这也没有解决问题。

In box2d, RayCasts should collide with static bodies, so your problem is probably somewhere else.在 box2d 中,RayCast 应该与 static 物体发生碰撞,因此您的问题可能出在其他地方。

Keep in mind that the raycast callback is not reporting the fixtures in order of the distance to the start point.请记住,光线投射回调不会按照到起点的距离顺序报告灯具。 Instead the order is random and you have to handle that in you callback.相反,顺序是随机的,您必须在回调中处理它。 If this is not the problem, you might want to add some code to the question.如果这不是问题,您可能需要在问题中添加一些代码。

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

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