gpt4 book ai didi

contacts - 子弹物理 : contacts force/impulse

转载 作者:行者123 更新时间:2023-12-02 10:29:20 30 4
gpt4 key购买 nike

我希望检测一个(球)何时接触另一个物体(目标),并且我希望知道该接触的冲动。

我知道三种检测联系人的方法

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
}
}
}

关于contacts - 子弹物理 : contacts force/impulse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13000764/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com