在 box2d 和engine 中设置启用的联系人
如何在 box2d (Andengine) 中禁用两个物体之间的接触。我已经使用了 contact.setEnabled(false)
但这对我不起作用。我在下面给出了代码供参考。
@Override
public void beginContact(final Contact pContact)
{
final Fixture fixtureA = pContact.getFixtureA();
final Body bodyA = fixtureA.getBody();
final Object userDataA = bodyA.getUserData();
final Fixture fixtureB = pContact.getFixtureB();
final Body bodyB = fixtureB.getBody();
final Object userDataB = bodyB.getUserData();
if(userDataA==null || userDataB==null)
return;
if(userDataA.equals(target) && userDataB.equals(ball)
{
pContact.setEnabled(false);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就 Box2D 而言,您的代码看起来基本上是正确的,除此之外,您还需要检查球和目标是否相反:
但我认为您的主要问题可能是使用final...为什么final ?我不是java专家,但它看起来非常可疑,快速浏览一下维基百科就会说:“最终变量只能初始化一次”。要保持接触禁用,您需要每帧执行 SetEnabled(false) 操作。
As far as Box2D is concerned your code looks mostly correct, apart from that you will also need to check if the ball and the target are the other way around:
But I think your main problem might be the use of final... why final? I am no java expert but it looks very suspicious and a quick look at wikipedia says: "A final variable can only be initialized once". To keep the contact disabled you'll need to do SetEnabled(false) every frame.
我遇到了同样的问题,感谢@Rooban 和他的评论“fixture.isSensor=true;”我解决了。我正在使用 box2dweb (JavaScript 端口),因为所有 box2d API 都很相似,这可能会对您有所帮助。
假设您有两个物体,一堵墙和一个玩具。玩具正在穿过墙壁。
然后你必须添加 TOY 对象。
现在,当您拥有对象时,您必须检查碰撞情况。
我希望这会对您有所帮助。
I had the same problem and thanks to @Rooban and his comment "fixture.isSensor=true;" I solved it. I'm working with box2dweb (JavaScript port) and because all box2d APIs are similar this might help you.
Let say you have two objects, a WALL and a TOY. The TOY is passing through the WALL.
Then you have to add the TOY object.
Now when you have the objects you have to check for collision.
I hope this will help you.