Max*_*Max 7 contact bullet game-physics bulletphysics
我想检测一个(球)何时接触另一个物体(目标),我想知道那个接触的冲动.
我知道有三种方法来检测联系人
gContactAddedCallback
Run Code Online (Sandbox Code Playgroud)
要么
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
要么
检查线性和角速度变化.(不清楚是否有接触和什么物体使速度发生变化,是物体还是阻尼,重力或某些力场.
我希望有联系信息,包括联系人冲动.我注意到在1帧模拟中解决了一些接触,其他需要2帧,脉冲则低两倍.(我得到了它调试代码.)我会很乐意得到一个总冲动的联系通知.
我列出的所有方法都没有为我提供联系方式的完整信息.有一段时间它会在球飞近目标时发射,甚至不会碰到目标.
什么是预期的方法呢?
如果接触能量很高,这些信息可用于播放冲击声或开始一些动画.
这段代码应该为您指明可能的方向
// 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
}
}
}
Run Code Online (Sandbox Code Playgroud)