mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
optimize contact listener
This commit is contained in:
@@ -43,6 +43,7 @@ void PhysicsContactListener::BeginContact(b2Contact* contact)
|
||||
if (find(_contactFixtures.begin(), _contactFixtures.end(), fixtureA) != _contactFixtures.end() ||
|
||||
find(_contactFixtures.begin(), _contactFixtures.end(), fixtureB) != _contactFixtures.end())
|
||||
{
|
||||
_contactMap[contact] = true;
|
||||
_beginContact(contact);
|
||||
}
|
||||
|
||||
@@ -52,13 +53,11 @@ void PhysicsContactListener::EndContact(b2Contact* contact)
|
||||
{
|
||||
if (!_endContact) return;
|
||||
|
||||
b2Fixture* fixtureA = contact->GetFixtureA();
|
||||
b2Fixture* fixtureB = contact->GetFixtureB();
|
||||
|
||||
if (find(_contactFixtures.begin(), _contactFixtures.end(), fixtureA) != _contactFixtures.end() ||
|
||||
find(_contactFixtures.begin(), _contactFixtures.end(), fixtureB) != _contactFixtures.end())
|
||||
auto i = _contactMap.find(contact);
|
||||
if (i != _contactMap.end())
|
||||
{
|
||||
_endContact(contact);
|
||||
_contactMap.erase(i);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,11 +65,7 @@ void PhysicsContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldM
|
||||
{
|
||||
if (!_preSolve) return;
|
||||
|
||||
b2Fixture* fixtureA = contact->GetFixtureA();
|
||||
b2Fixture* fixtureB = contact->GetFixtureB();
|
||||
|
||||
if (find(_contactFixtures.begin(), _contactFixtures.end(), fixtureA) != _contactFixtures.end() ||
|
||||
find(_contactFixtures.begin(), _contactFixtures.end(), fixtureB) != _contactFixtures.end())
|
||||
if (_contactMap.find(contact) != _contactMap.end())
|
||||
{
|
||||
_preSolve(contact);
|
||||
}
|
||||
@@ -80,11 +75,7 @@ void PhysicsContactListener::PostSolve(b2Contact* contact, const b2ContactImpuls
|
||||
{
|
||||
if (!_postSolve) return;
|
||||
|
||||
b2Fixture* fixtureA = contact->GetFixtureA();
|
||||
b2Fixture* fixtureB = contact->GetFixtureB();
|
||||
|
||||
if (find(_contactFixtures.begin(), _contactFixtures.end(), fixtureA) != _contactFixtures.end() ||
|
||||
find(_contactFixtures.begin(), _contactFixtures.end(), fixtureB) != _contactFixtures.end())
|
||||
if (_contactMap.find(contact) != _contactMap.end())
|
||||
{
|
||||
_impulse.init(impulse);
|
||||
_postSolve(contact, &_impulse);
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace creator {
|
||||
|
||||
class CC_DLL PhysicsContactListener : public b2ContactListener
|
||||
@@ -62,6 +63,8 @@ protected:
|
||||
|
||||
std::vector<b2Fixture*> _contactFixtures;
|
||||
PhysicsContactImpulse _impulse;
|
||||
|
||||
std::unordered_map<b2Contact*, bool> _contactMap;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user