我希望检测一个人(球)何时触摸另一物体(目标),并且希望知道该接触的冲动。
我知道三种检测联系人的方法
gContactAddedCallback
或者
int numManifolds = m_dynamicsWorld->getDispatcher()->getNumManifolds();
for (int i=0;i<numManifolds;i++)
{
btRigidBody* obA = static_cast<btRigidBody*>(contactManifold->getBody0());
btRigidBody* obB = static_cast<btRigidBody*>(contactManifold->getBody1());
// May be there is contact obA and obB
btPersistentManifold* contactManifold = m_dynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i);
int numContacts = contactManifold->getNumContacts();
for (int j=0;j<numContacts;j++)
{
btManifoldPoint& pt = contactManifold->getContactPoint(j);
if (pt.getDistance()<0.f)
{
// One contact point is inside of another object
// But some contacts are ignored
}
}
}
或者
检查线速度和角速度变化。 (尚不清楚是否有接触以及是什么物体引起了速度变化,是物体还是阻尼,重力或某些力场。
我希望获得联系信息以包括联系冲动。
我注意到在1帧模拟中解决的某些接触占用了2帧,而冲量降低了两倍。 (我得到了调试代码。)
我会很完美地获得1个总冲动的联系通知。
我列出的所有方法都没有为我提供联系的完整信息。
有时,当球飞到目标附近甚至不触及目标时,它会发射光。
可以预期的方式是什么?
如果接触能量很高,则此类信息可用于播放撞击声或启动某些动画。
最佳答案
此代码应指导您
// some global constants needed
enum collisiontypes {
NOTHING = 0, // things that don't collide
BALL_BODY = 1<<2, // is ball
TARGET_BODY = 1<<3 // is target
};
int ballBodyCollidesWith = TARGET_BODY | BALL_BODY; // balls collide with targets and other balls
int targetBodyCollidesWith = BALL_BODY; // targets collide with balls
// ...
// bodies creation
dynamicsWorld->addRigidBody(ballBody, BALL_BODY, ballBodyCollidesWith);
dynamicsWorld->addRigidBody(targetBody, TARGET_BODY, targetBodyCollidesWith);
//...
// find out whether a ball collides with a target
int numManifolds = m_dynamicsWorld->getDispatcher()->getNumManifolds();
for (int i=0;i<numManifolds;i++)
{
btRigidBody* obA = static_cast<btRigidBody*>(contactManifold->getBody0());
btRigidBody* obB = static_cast<btRigidBody*>(contactManifold->getBody1());
// May be there is contact obA and obB
// ignore bodies that are not balls or targets
if (
(!(obA->getCollisionFlags() | BALL_TYPE) && !(obB->getCollisionFlags() | BALL_TYPE)) // there is no BALL_TYPE colliding
||
(!(obA->getCollisionFlags() | TARGET_TYPE) && !(obB->getCollisionFlags() | TARGET_TYPE)) // there is no TARGET_TYPE colliding
)
continue; // no more searching needed
btPersistentManifold* contactManifold = m_dynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i);
int numContacts = contactManifold->getNumContacts();
for (int j=0;j<numContacts;j++)
{
btManifoldPoint& pt = contactManifold->getContactPoint(j);
printf("%f\n", pt.getAppliedImpulse()); // log to see the variation range of getAppliedImpulse and to chose the appropriate impulseThreshold
if (pt.getAppliedImpulse() > impulseThreshold)
{
// increase score or something
break; // no more searching needed
}
}
}
的可能方向关于contact - BulletPhysic : contacts force/impulse,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13000764/