我有一些簡單的Box2D的機構設置與接觸監聽器,如下:檢測兩個Box2D的機構的初始碰撞沒有連續碰撞
#import "MyContactListener.h"
MyContactListener::MyContactListener() : _contacts() {
}
MyContactListener::~MyContactListener() {
}
void MyContactListener::BeginContact(b2Contact* contact) {
// We need to copy out the data because the b2Contact passed in
// is reused.
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.push_back(myContact);
b2Body *A = contact->GetFixtureA()->GetBody();
b2Body *B = contact->GetFixtureA()->GetBody();
NSLog(@"Collision detected!");
PLAYSOUND(COLLISION);
}
void MyContactListener::EndContact(b2Contact* contact) {
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
std::vector<MyContact>::iterator pos;
pos = std::find(_contacts.begin(), _contacts.end(), myContact);
if (pos != _contacts.end()) {
_contacts.erase(pos);
}
}
void MyContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {
}
void MyContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {
}
,我需要播放聲音時,兩具屍體已經碰撞。然而,這種實現檢測連續的碰撞,因此當身體接觸時播放聲音。我對box2d和C++的瞭解已經非常有限,是否有一種簡單的方法來檢測新的碰撞而不會檢測到連續的碰撞?
我建議你通過[教程]去(http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone),這樣你將能夠檢測到單個碰撞 – Marine
「兩個物體碰撞時」= BeginContact。這不是一個連續的事情,這就是爲什麼它被稱爲'開始':) – iforce2d