问题描述
public class Contact implements ContactListener {
@Override
public void beginContact(Contact contact) {
Fixture fa = contact.getFixtureA();
Fixture fb = contact.getFixtureB();
if (fa.getFilterData().categoryBits==16){
((Gamescreen)fa.getUserData()).starttouch(fa,fb);
}
@Override
public void endContact(Contact contact) {
Fixture fa = contact.getFixtureA();
Fixture fb = contact.getFixtureB();
if (fa.getFilterData().categoryBits==16)
{
((Gamescreen)fa.getUserData()).endtouch();
}
当只有一个物体要触摸时,这段代码可以很好地工作,但是有时候我需要像拖曳另一个物体一样.
This code works fine when there is just one object to touch but some time i need to make like tow object beside of each others.
就像玩家在彼此旁边的两个物体上行走(没有跳跃)时,调用了第二种方法(endcontact
),但是第一种方法(begincontact
)不再被调用.
Like when the player walk on 2 objects (without jumping) beside each others the second method (endcontact
) called but the first method (begincontact
) does not get call again.
推荐答案
如果我正确理解,那么此链接可能就是您要查找的内容: http://www.iforce2d.net/b2dtut/collision-callbacks
If I understood correctly, then this link may be what you're looking for:http://www.iforce2d.net/b2dtut/collision-callbacks
它是C ++,但是您可以看到一对对象的碰撞回调的示例实现:
It's C++, but you can see example implementation of collision callback for a pair of objects:
void BeginContact(b2Contact* contact) {
//check if fixture A was a ball
void* bodyUserData = contact->GetFixtureA()->GetBody()->GetUserData();
if ( bodyUserData )
static_cast<Ball*>( bodyUserData )->startContact();
//check if fixture B was a ball
bodyUserData = contact->GetFixtureB()->GetBody()->GetUserData();
if ( bodyUserData )
static_cast<Ball*>( bodyUserData )->startContact();
}
此外,如果开始联系,您不会保存true/false,但是会保存联系人数:
Additionally, you don't save true/false if contact started, but number of contacts:
//new implementation for contact state change
void startContact() { m_numContacts++; }
void endContact() { m_numContacts--; }
这篇关于需要知道何时开始碰撞和结束box2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!