2015-05-08 15:49:33 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "physics3d/CCPhysics3D.h"
|
2015-05-08 15:49:33 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_USE_3D_PHYSICS
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
# if (AX_ENABLE_BULLET_INTEGRATION)
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2015-05-08 15:49:33 +08:00
|
|
|
|
|
|
|
Physics3DWorld::Physics3DWorld()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _needCollisionChecking(false)
|
|
|
|
, _collisionCheckingFlag(false)
|
|
|
|
, _needGhostPairCallbackChecking(false)
|
|
|
|
, _btPhyiscsWorld(nullptr)
|
|
|
|
, _collisionConfiguration(nullptr)
|
|
|
|
, _dispatcher(nullptr)
|
|
|
|
, _broadphase(nullptr)
|
|
|
|
, _solver(nullptr)
|
|
|
|
, _ghostCallback(nullptr)
|
|
|
|
, _debugDrawer(nullptr)
|
|
|
|
{}
|
2015-05-08 15:49:33 +08:00
|
|
|
Physics3DWorld::~Physics3DWorld()
|
|
|
|
{
|
|
|
|
removeAllPhysics3DConstraints();
|
|
|
|
removeAllPhysics3DObjects();
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(_collisionConfiguration);
|
|
|
|
AX_SAFE_DELETE(_dispatcher);
|
|
|
|
AX_SAFE_DELETE(_broadphase);
|
|
|
|
AX_SAFE_DELETE(_ghostCallback);
|
|
|
|
AX_SAFE_DELETE(_solver);
|
|
|
|
AX_SAFE_DELETE(_btPhyiscsWorld);
|
|
|
|
AX_SAFE_DELETE(_debugDrawer);
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& it : _physicsComponents)
|
2015-05-08 15:49:33 +08:00
|
|
|
it->setPhysics3DObject(nullptr);
|
|
|
|
_physicsComponents.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Physics3DWorld* Physics3DWorld::create(Physics3DWorldDes* info)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto world = new Physics3DWorld();
|
2015-05-08 15:49:33 +08:00
|
|
|
world->init(info);
|
|
|
|
world->autorelease();
|
|
|
|
return world;
|
|
|
|
}
|
|
|
|
|
2015-05-27 18:07:31 +08:00
|
|
|
void Physics3DWorld::setGravity(const Vec3& gravity)
|
|
|
|
{
|
|
|
|
_btPhyiscsWorld->setGravity(convertVec3TobtVector3(gravity));
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec3 Physics3DWorld::getGravity() const
|
|
|
|
{
|
|
|
|
return convertbtVector3ToVec3(_btPhyiscsWorld->getGravity());
|
|
|
|
}
|
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
bool Physics3DWorld::init(Physics3DWorldDes* info)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
/// collision configuration contains default setup for memory, collision setup
|
2021-12-08 00:11:53 +08:00
|
|
|
_collisionConfiguration = new btDefaultCollisionConfiguration();
|
2015-05-08 15:49:33 +08:00
|
|
|
//_collisionConfiguration->setConvexConvexMultipointIterations();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/// use the default collision dispatcher. For parallel processing you can use a different dispatcher (see
|
|
|
|
/// Extras/BulletMultiThreaded)
|
2021-12-08 00:11:53 +08:00
|
|
|
_dispatcher = new btCollisionDispatcher(_collisionConfiguration);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
_broadphase = new btDbvtBroadphase();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/// the default constraint solver. For parallel processing you can use a different solver (see
|
|
|
|
/// Extras/BulletMultiThreaded)
|
2015-05-08 15:49:33 +08:00
|
|
|
btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver();
|
2021-12-25 10:04:45 +08:00
|
|
|
_solver = sol;
|
2015-06-29 10:26:09 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
btGhostPairCallback* ghostCallback = new btGhostPairCallback();
|
|
|
|
_ghostCallback = ghostCallback;
|
|
|
|
|
|
|
|
_btPhyiscsWorld = new btDiscreteDynamicsWorld(_dispatcher, _broadphase, _solver, _collisionConfiguration);
|
2015-05-08 15:49:33 +08:00
|
|
|
_btPhyiscsWorld->setGravity(convertVec3TobtVector3(info->gravity));
|
|
|
|
if (info->isDebugDrawEnabled)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_debugDrawer = new Physics3DDebugDrawer();
|
2015-05-08 15:49:33 +08:00
|
|
|
_btPhyiscsWorld->setDebugDrawer(_debugDrawer);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DWorld::setDebugDrawEnable(bool enableDebugDraw)
|
|
|
|
{
|
|
|
|
if (enableDebugDraw && _btPhyiscsWorld->getDebugDrawer() == nullptr)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_debugDrawer = new Physics3DDebugDrawer();
|
2015-05-08 15:49:33 +08:00
|
|
|
}
|
|
|
|
enableDebugDraw ? _btPhyiscsWorld->setDebugDrawer(_debugDrawer) : _btPhyiscsWorld->setDebugDrawer(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Physics3DWorld::isDebugDrawEnabled() const
|
|
|
|
{
|
|
|
|
return _btPhyiscsWorld->getDebugDrawer() != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DWorld::addPhysics3DObject(Physics3DObject* physicsObj)
|
|
|
|
{
|
|
|
|
auto it = std::find(_objects.begin(), _objects.end(), physicsObj);
|
|
|
|
if (it == _objects.end())
|
|
|
|
{
|
|
|
|
_objects.push_back(physicsObj);
|
|
|
|
physicsObj->retain();
|
|
|
|
if (physicsObj->getObjType() == Physics3DObject::PhysicsObjType::RIGID_BODY)
|
|
|
|
{
|
|
|
|
_btPhyiscsWorld->addRigidBody(static_cast<Physics3DRigidBody*>(physicsObj)->getRigidBody());
|
|
|
|
}
|
2015-06-29 10:26:09 +08:00
|
|
|
else if (physicsObj->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER)
|
|
|
|
{
|
|
|
|
_btPhyiscsWorld->addCollisionObject(static_cast<Physics3DCollider*>(physicsObj)->getGhostObject());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
_collisionCheckingFlag = true;
|
2015-06-29 10:26:09 +08:00
|
|
|
_needGhostPairCallbackChecking = true;
|
2015-05-08 15:49:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DWorld::removePhysics3DObject(Physics3DObject* physicsObj)
|
|
|
|
{
|
|
|
|
auto it = std::find(_objects.begin(), _objects.end(), physicsObj);
|
|
|
|
if (it != _objects.end())
|
|
|
|
{
|
|
|
|
if (physicsObj->getObjType() == Physics3DObject::PhysicsObjType::RIGID_BODY)
|
|
|
|
{
|
|
|
|
_btPhyiscsWorld->removeRigidBody(static_cast<Physics3DRigidBody*>(physicsObj)->getRigidBody());
|
|
|
|
}
|
2015-06-29 10:26:09 +08:00
|
|
|
else if (physicsObj->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER)
|
|
|
|
{
|
|
|
|
_btPhyiscsWorld->removeCollisionObject(static_cast<Physics3DCollider*>(physicsObj)->getGhostObject());
|
|
|
|
}
|
2015-05-08 15:49:33 +08:00
|
|
|
physicsObj->release();
|
|
|
|
_objects.erase(it);
|
2021-12-25 10:04:45 +08:00
|
|
|
_collisionCheckingFlag = true;
|
2015-06-29 10:26:09 +08:00
|
|
|
_needGhostPairCallbackChecking = true;
|
2015-05-08 15:49:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DWorld::removeAllPhysics3DObjects()
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& it : _objects)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2015-05-08 15:49:33 +08:00
|
|
|
if (it->getObjType() == Physics3DObject::PhysicsObjType::RIGID_BODY)
|
|
|
|
{
|
|
|
|
_btPhyiscsWorld->removeRigidBody(static_cast<Physics3DRigidBody*>(it)->getRigidBody());
|
|
|
|
}
|
2015-06-29 10:26:09 +08:00
|
|
|
else if (it->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER)
|
|
|
|
{
|
|
|
|
_btPhyiscsWorld->removeCollisionObject(static_cast<Physics3DCollider*>(it)->getGhostObject());
|
|
|
|
}
|
2015-05-08 15:49:33 +08:00
|
|
|
it->release();
|
|
|
|
}
|
|
|
|
_objects.clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
_collisionCheckingFlag = true;
|
2015-06-29 10:26:09 +08:00
|
|
|
_needGhostPairCallbackChecking = true;
|
2015-05-08 15:49:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DWorld::addPhysics3DConstraint(Physics3DConstraint* constraint, bool disableCollisionsBetweenLinkedObjs)
|
|
|
|
{
|
2020-11-15 00:48:53 +08:00
|
|
|
auto it = std::find(_constraints.begin(), _constraints.end(), constraint);
|
|
|
|
if (it != _constraints.end())
|
|
|
|
return;
|
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
auto body = constraint->getBodyA();
|
|
|
|
if (body)
|
|
|
|
body->addConstraint(constraint);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
body = constraint->getBodyB();
|
|
|
|
if (body)
|
|
|
|
{
|
|
|
|
body->addConstraint(constraint);
|
|
|
|
}
|
|
|
|
_btPhyiscsWorld->addConstraint(constraint->getbtContraint(), disableCollisionsBetweenLinkedObjs);
|
2020-11-15 00:48:53 +08:00
|
|
|
|
|
|
|
_constraints.push_back(constraint);
|
|
|
|
constraint->retain();
|
2015-05-08 15:49:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DWorld::removePhysics3DConstraint(Physics3DConstraint* constraint)
|
|
|
|
{
|
2020-11-15 00:48:53 +08:00
|
|
|
auto it = std::find(_constraints.begin(), _constraints.end(), constraint);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it != _constraints.end())
|
|
|
|
{
|
2020-11-15 00:48:53 +08:00
|
|
|
removePhysics3DConstraintFromBullet(constraint);
|
|
|
|
|
|
|
|
_constraints.erase(it);
|
|
|
|
constraint->release();
|
|
|
|
}
|
2015-05-08 15:49:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DWorld::removeAllPhysics3DConstraints()
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& it : _objects)
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
|
|
|
auto type = it->getObjType();
|
|
|
|
if (type == Physics3DObject::PhysicsObjType::RIGID_BODY)
|
|
|
|
{
|
|
|
|
auto& constraints = static_cast<Physics3DRigidBody*>(it)->_constraintList;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& constraint : constraints)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2015-05-08 15:49:33 +08:00
|
|
|
_btPhyiscsWorld->removeConstraint(constraint->getbtContraint());
|
|
|
|
constraint->release();
|
|
|
|
}
|
|
|
|
constraints.clear();
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& constraint : _constraints)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2020-11-15 00:48:53 +08:00
|
|
|
removePhysics3DConstraintFromBullet(constraint);
|
|
|
|
constraint->release();
|
|
|
|
}
|
|
|
|
_constraints.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DWorld::removePhysics3DConstraintFromBullet(Physics3DConstraint* constraint)
|
|
|
|
{
|
|
|
|
_btPhyiscsWorld->removeConstraint(constraint->getbtContraint());
|
|
|
|
|
|
|
|
auto bodyA = constraint->getBodyA();
|
|
|
|
auto bodyB = constraint->getBodyB();
|
|
|
|
if (bodyA)
|
|
|
|
bodyA->removeConstraint(constraint);
|
|
|
|
if (bodyB)
|
|
|
|
bodyB->removeConstraint(constraint);
|
2015-05-08 15:49:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DWorld::stepSimulate(float dt)
|
|
|
|
{
|
|
|
|
if (_btPhyiscsWorld)
|
|
|
|
{
|
2015-06-29 10:26:09 +08:00
|
|
|
setGhostPairCallback();
|
2021-12-25 10:04:45 +08:00
|
|
|
// should sync kinematic node before simulation
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& it : _physicsComponents)
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
|
|
|
it->preSimulate();
|
|
|
|
}
|
|
|
|
_btPhyiscsWorld->stepSimulation(dt, 3);
|
2021-12-25 10:04:45 +08:00
|
|
|
// sync dynamic node after simulation
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& it : _physicsComponents)
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
|
|
|
it->postSimulate();
|
|
|
|
}
|
|
|
|
if (needCollisionChecking())
|
|
|
|
collisionChecking();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DWorld::debugDraw(Renderer* renderer)
|
|
|
|
{
|
|
|
|
if (_debugDrawer)
|
|
|
|
{
|
|
|
|
_debugDrawer->clear();
|
|
|
|
_btPhyiscsWorld->debugDrawWorld();
|
|
|
|
_debugDrawer->draw(renderer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
bool Physics3DWorld::rayCast(const axis::Vec3& startPos,
|
|
|
|
const axis::Vec3& endPos,
|
2021-12-25 10:04:45 +08:00
|
|
|
Physics3DWorld::HitResult* result)
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
|
|
|
auto btStart = convertVec3TobtVector3(startPos);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto btEnd = convertVec3TobtVector3(endPos);
|
2015-05-08 15:49:33 +08:00
|
|
|
btCollisionWorld::ClosestRayResultCallback btResult(btStart, btEnd);
|
|
|
|
_btPhyiscsWorld->rayTest(btStart, btEnd, btResult);
|
|
|
|
if (btResult.hasHit())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
result->hitObj = getPhysicsObject(btResult.m_collisionObject);
|
2015-05-08 15:49:33 +08:00
|
|
|
result->hitPosition = convertbtVector3ToVec3(btResult.m_hitPointWorld);
|
2021-12-25 10:04:45 +08:00
|
|
|
result->hitNormal = convertbtVector3ToVec3(btResult.m_hitNormalWorld);
|
2015-05-08 15:49:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
result->hitObj = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Physics3DWorld::sweepShape(Physics3DShape* shape,
|
2022-07-11 17:50:21 +08:00
|
|
|
const axis::Mat4& startTransform,
|
|
|
|
const axis::Mat4& endTransform,
|
2021-12-25 10:04:45 +08:00
|
|
|
Physics3DWorld::HitResult* result)
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_ASSERT(shape->getShapeType() != Physics3DShape::ShapeType::HEIGHT_FIELD &&
|
2021-12-25 10:04:45 +08:00
|
|
|
shape->getShapeType() != Physics3DShape::ShapeType::MESH);
|
2015-05-08 15:49:33 +08:00
|
|
|
auto btStart = convertMat4TobtTransform(startTransform);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto btEnd = convertMat4TobtTransform(endTransform);
|
2015-05-08 15:49:33 +08:00
|
|
|
btCollisionWorld::ClosestConvexResultCallback btResult(btStart.getOrigin(), btEnd.getOrigin());
|
|
|
|
_btPhyiscsWorld->convexSweepTest((btConvexShape*)shape->getbtShape(), btStart, btEnd, btResult);
|
|
|
|
if (btResult.hasHit())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
result->hitObj = getPhysicsObject(btResult.m_hitCollisionObject);
|
2015-05-08 15:49:33 +08:00
|
|
|
result->hitPosition = convertbtVector3ToVec3(btResult.m_hitPointWorld);
|
2021-12-25 10:04:45 +08:00
|
|
|
result->hitNormal = convertbtVector3ToVec3(btResult.m_hitNormalWorld);
|
2015-05-08 15:49:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
result->hitObj = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Physics3DObject* Physics3DWorld::getPhysicsObject(const btCollisionObject* btObj)
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& it : _objects)
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
|
|
|
if (it->getObjType() == Physics3DObject::PhysicsObjType::RIGID_BODY)
|
|
|
|
{
|
|
|
|
if (static_cast<Physics3DRigidBody*>(it)->getRigidBody() == btObj)
|
|
|
|
return it;
|
|
|
|
}
|
2015-06-29 10:26:09 +08:00
|
|
|
else if (it->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER)
|
|
|
|
{
|
|
|
|
if (static_cast<Physics3DCollider*>(it)->getGhostObject() == btObj)
|
|
|
|
return it;
|
|
|
|
}
|
2015-05-08 15:49:33 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DWorld::collisionChecking()
|
|
|
|
{
|
|
|
|
int numManifolds = _dispatcher->getNumManifolds();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 0; i < numManifolds; ++i)
|
|
|
|
{
|
|
|
|
btPersistentManifold* contactManifold = _dispatcher->getManifoldByIndexInternal(i);
|
|
|
|
int numContacts = contactManifold->getNumContacts();
|
|
|
|
if (0 < numContacts)
|
|
|
|
{
|
2015-05-08 15:49:33 +08:00
|
|
|
const btCollisionObject* obA = static_cast<const btCollisionObject*>(contactManifold->getBody0());
|
|
|
|
const btCollisionObject* obB = static_cast<const btCollisionObject*>(contactManifold->getBody1());
|
2021-12-25 10:04:45 +08:00
|
|
|
Physics3DObject* poA = getPhysicsObject(obA);
|
|
|
|
Physics3DObject* poB = getPhysicsObject(obB);
|
|
|
|
if (poA->needCollisionCallback() || poB->needCollisionCallback())
|
|
|
|
{
|
2015-05-08 15:49:33 +08:00
|
|
|
Physics3DCollisionInfo ci;
|
|
|
|
ci.objA = poA;
|
|
|
|
ci.objB = poB;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int c = 0; c < numContacts; ++c)
|
|
|
|
{
|
|
|
|
btManifoldPoint& pt = contactManifold->getContactPoint(c);
|
2015-05-08 15:49:33 +08:00
|
|
|
Physics3DCollisionInfo::CollisionPoint cp = {
|
2021-12-25 10:04:45 +08:00
|
|
|
convertbtVector3ToVec3(pt.m_localPointA), convertbtVector3ToVec3(pt.m_positionWorldOnA),
|
|
|
|
convertbtVector3ToVec3(pt.m_localPointB), convertbtVector3ToVec3(pt.m_positionWorldOnB),
|
|
|
|
convertbtVector3ToVec3(pt.m_normalWorldOnB)};
|
2015-05-08 15:49:33 +08:00
|
|
|
ci.collisionPointList.push_back(cp);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (poA->needCollisionCallback())
|
|
|
|
{
|
2015-05-08 15:49:33 +08:00
|
|
|
poA->getCollisionCallback()(ci);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (poB->needCollisionCallback())
|
|
|
|
{
|
2015-05-08 15:49:33 +08:00
|
|
|
poB->getCollisionCallback()(ci);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Physics3DWorld::needCollisionChecking()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_collisionCheckingFlag)
|
|
|
|
{
|
2015-05-08 15:49:33 +08:00
|
|
|
_needCollisionChecking = false;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& it : _objects)
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it->getCollisionCallback() != nullptr)
|
|
|
|
{
|
2015-05-08 15:49:33 +08:00
|
|
|
_needCollisionChecking = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_collisionCheckingFlag = false;
|
|
|
|
}
|
|
|
|
return _needCollisionChecking;
|
|
|
|
}
|
|
|
|
|
2015-06-29 10:26:09 +08:00
|
|
|
void Physics3DWorld::setGhostPairCallback()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_needGhostPairCallbackChecking)
|
|
|
|
{
|
2015-06-29 10:26:09 +08:00
|
|
|
bool needCallback = false;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& it : _objects)
|
2015-06-29 10:26:09 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER)
|
|
|
|
{
|
2015-06-29 10:26:09 +08:00
|
|
|
needCallback = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
_btPhyiscsWorld->getPairCache()->setInternalGhostPairCallback(needCallback == true ? _ghostCallback : nullptr);
|
2015-06-29 10:26:09 +08:00
|
|
|
_needGhostPairCallbackChecking = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
# endif // AX_ENABLE_BULLET_INTEGRATION
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#endif // AX_USE_3D_PHYSICS
|