我正在尝试为我的引擎中的冲突实现音频功能,我很难找到一个好方法。我使用Bullet Physics,我想在两个物体碰撞时播放声音。我通过gContactAddedCallback实现了一个回调机制,所以每次两个对象碰撞时我都会得到回调。我遇到的问题是回调函数可以在每个游戏循环中多次调用,我不知道如何播放音频以进行碰撞。
我正在考虑保留某种列表,其中包含某个对象的所有当前冲突,但是通过这种方式,我再次不确定何时清理列表。我试图在每个游戏循环中清理列表但我仍然收到两个对象碰撞的回调。
任何人都可以解释或指向解释使物理引擎和音频协同工作的正确方法的资源吗?
答案 0 :(得分:1)
好的,所以我找到了一种方法来做我想要的东西,但它似乎非常不优化,有点hacky。我仍然想知道是否有一种简单的方法可以做到这一点,我没有看到。这就是我现在所做的。我保留一个持久冲突列表,并将其与另一个当前冲突列表进行比较。如果当前冲突有一个在持久冲突列表中不存在的条目,我将其添加到持久冲突列表中并播放声音。之后,我迭代持久冲突列表并删除当前冲突列表中未包含的所有条目。这是代码。
// persistentCollisions map.
std::map<int, std::vector<int>> persistentCollisions;
// Main game loop
while (!glfwWindowShouldClose(window.getWindow()))
{
//.
//.
//.
physicsEngine->getDynamicsWorld()->stepSimulation(1.0f / 60.0f);
// New collision map
std::map<int, std::vector<int>> newCollisions;
// Go over the collision manifold and extract all existing collisions
int numManifolds = physicsEngine->getDynamicsWorld()->getDispatcher()->getNumManifolds();
for (int i = 0; i < numManifolds; i++)
{
btPersistentManifold* contactManifold = physicsEngine->getDynamicsWorld()->getDispatcher()->getManifoldByIndexInternal(i);
const btCollisionObject* obA = contactManifold->getBody0();
const btCollisionObject* obB = contactManifold->getBody1();
int numContacts = contactManifold->getNumContacts();
for (int j = 0; j < numContacts; j++)
{
btManifoldPoint& pt = contactManifold->getContactPoint(j);
if (pt.getDistance() < 0.f)
{
// If it is a new collision, add to the newCollision list
if (std::find(newCollisions[obA->getUserIndex()].begin(), newCollisions[obA->getUserIndex()].end(), obB->getUserIndex()) == newCollisions[obA->getUserIndex()].end())
{
newCollisions[obA->getUserIndex()].emplace_back(obB->getUserIndex());
}
}
}
}
// Iterate over new collisions and add new collision to persistent collisions if it does not exist
std::map<int, std::vector<int>>::iterator newCollisionIterator = newCollisions.begin();
while (newCollisionIterator != newCollisions.end())
{
for (auto item : newCollisionIterator->second)
{
if (std::find(persistentCollisions[newCollisionIterator->first].begin(), persistentCollisions[newCollisionIterator->first].end(), item) == persistentCollisions[newCollisionIterator->first].end())
{
std::cout << "New collision between " << newCollisionIterator->first << " And " << item << std::endl;
// We can play our collision audio here
persistentCollisions[newCollisionIterator->first].emplace_back(item);
}
}
newCollisionIterator++;
}
// Iterate over persistent collisions and remove all collisions that did not exist in new collision
std::map<int, std::vector<int>>::iterator persistentCollisionIterator = persistentCollisions.begin();
while (persistentCollisionIterator != persistentCollisions.end())
{
std::vector<int>::iterator iter;
for (iter = persistentCollisionIterator->second.begin(); iter != persistentCollisionIterator->second.end(); )
{
if (std::find(newCollisions[persistentCollisionIterator->first].begin(), newCollisions[persistentCollisionIterator->first].end(), *iter) != newCollisions[persistentCollisionIterator->first].end())
{
++iter;
}
else
{
iter = persistentCollisionIterator->second.erase(iter);
}
}
persistentCollisionIterator++;
}
}