BulletPhysic:联系力量/冲动
我希望检测一个(球)接触另一个物体(目标)的时间,并且希望知道该接触的冲动。
我知道三种检测联系人的方法
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帧,脉冲低两倍。 (我得到了它的调试代码。)我会完美地得到一个总冲动的联系人通知。
我列出的所有方法都不能给我提供联系人的全部信息。 当球飞向目标附近时甚至没有碰到它,有一段时间它会发射。
预期的方式是什么?
如果接触能量很高,这些信息可以用来播放冲击声或启动一些动画。
这段代码应该指向你的可能方向
// 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("%fn", 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
}
}
}
链接地址: http://www.djcxy.com/p/11329.html