2020-03-30 21:25:33 +00:00
|
|
|
#include "closestnotmeconvexresultcallback.hpp"
|
|
|
|
|
|
|
|
#include <BulletCollision/CollisionDispatch/btCollisionObject.h>
|
|
|
|
|
2019-02-13 07:30:16 +00:00
|
|
|
#include <components/misc/convert.hpp>
|
|
|
|
|
|
|
|
#include "../mwbase/world.hpp"
|
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
|
|
|
|
#include "collisiontype.hpp"
|
|
|
|
#include "projectile.hpp"
|
|
|
|
|
2020-03-30 21:25:33 +00:00
|
|
|
namespace MWPhysics
|
|
|
|
{
|
|
|
|
ClosestNotMeConvexResultCallback::ClosestNotMeConvexResultCallback(const btCollisionObject *me, const btVector3 &motion, btScalar minCollisionDot)
|
|
|
|
: btCollisionWorld::ClosestConvexResultCallback(btVector3(0.0, 0.0, 0.0), btVector3(0.0, 0.0, 0.0)),
|
|
|
|
mMe(me), mMotion(motion), mMinCollisionDot(minCollisionDot)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-13 07:30:16 +00:00
|
|
|
btScalar ClosestNotMeConvexResultCallback::addSingleResult(btCollisionWorld::LocalConvexResult& convexResult, bool normalInWorldSpace)
|
2020-03-30 21:25:33 +00:00
|
|
|
{
|
|
|
|
if (convexResult.m_hitCollisionObject == mMe)
|
|
|
|
return btScalar(1);
|
|
|
|
|
2019-02-13 07:30:16 +00:00
|
|
|
if (convexResult.m_hitCollisionObject->getBroadphaseHandle()->m_collisionFilterGroup == CollisionType_Projectile)
|
|
|
|
{
|
2020-11-21 15:26:45 +00:00
|
|
|
auto* projectileHolder = static_cast<Projectile*>(convexResult.m_hitCollisionObject->getUserPointer());
|
2020-10-23 18:27:07 +00:00
|
|
|
if (!projectileHolder->isActive())
|
|
|
|
return btScalar(1);
|
2020-11-21 15:26:45 +00:00
|
|
|
auto* targetHolder = static_cast<PtrHolder*>(mMe->getUserPointer());
|
2019-02-13 07:30:16 +00:00
|
|
|
const MWWorld::Ptr target = targetHolder->getPtr();
|
2020-11-21 15:26:45 +00:00
|
|
|
// do nothing if we hit the caster. Sometimes the launching origin is inside of caster collision shape
|
|
|
|
if (projectileHolder->getCaster() != target)
|
|
|
|
{
|
|
|
|
projectileHolder->hit(target, convexResult.m_hitPointLocal, convexResult.m_hitNormalLocal);
|
|
|
|
return btScalar(1);
|
|
|
|
}
|
2019-02-13 07:30:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 21:25:33 +00:00
|
|
|
btVector3 hitNormalWorld;
|
|
|
|
if (normalInWorldSpace)
|
|
|
|
hitNormalWorld = convexResult.m_hitNormalLocal;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
///need to transform normal into worldspace
|
|
|
|
hitNormalWorld = convexResult.m_hitCollisionObject->getWorldTransform().getBasis()*convexResult.m_hitNormalLocal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dot product of the motion vector against the collision contact normal
|
|
|
|
btScalar dotCollision = mMotion.dot(hitNormalWorld);
|
|
|
|
if (dotCollision <= mMinCollisionDot)
|
|
|
|
return btScalar(1);
|
|
|
|
|
|
|
|
return ClosestConvexResultCallback::addSingleResult(convexResult, normalInWorldSpace);
|
|
|
|
}
|
|
|
|
}
|