axmol/cocos/physics3d/CCPhysics3DWorld.cpp

429 lines
14 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +08:00
http://www.cocos2d-x.org
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +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.
****************************************************************************/
#include "physics3d/CCPhysics3D.h"
#include "renderer/CCRenderer.h"
#if CC_USE_3D_PHYSICS
2021-12-25 10:04:45 +08:00
# if (CC_ENABLE_BULLET_INTEGRATION)
2019-11-23 20:27:39 +08:00
NS_CC_BEGIN
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)
{}
2019-11-23 20:27:39 +08:00
Physics3DWorld::~Physics3DWorld()
{
removeAllPhysics3DConstraints();
removeAllPhysics3DObjects();
CC_SAFE_DELETE(_collisionConfiguration);
CC_SAFE_DELETE(_dispatcher);
CC_SAFE_DELETE(_broadphase);
CC_SAFE_DELETE(_ghostCallback);
CC_SAFE_DELETE(_solver);
CC_SAFE_DELETE(_btPhyiscsWorld);
CC_SAFE_DELETE(_debugDrawer);
for (auto it : _physicsComponents)
it->setPhysics3DObject(nullptr);
_physicsComponents.clear();
}
Physics3DWorld* Physics3DWorld::create(Physics3DWorldDes* info)
{
2021-12-08 00:11:53 +08:00
auto world = new Physics3DWorld();
2019-11-23 20:27:39 +08:00
world->init(info);
world->autorelease();
return world;
}
void Physics3DWorld::setGravity(const Vec3& gravity)
{
_btPhyiscsWorld->setGravity(convertVec3TobtVector3(gravity));
}
Vec3 Physics3DWorld::getGravity() const
{
return convertbtVector3ToVec3(_btPhyiscsWorld->getGravity());
}
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();
2019-11-23 20:27:39 +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)
2019-11-23 20:27:39 +08:00
btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver();
2021-12-25 10:04:45 +08:00
_solver = sol;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
btGhostPairCallback* ghostCallback = new btGhostPairCallback();
_ghostCallback = ghostCallback;
_btPhyiscsWorld = new btDiscreteDynamicsWorld(_dispatcher, _broadphase, _solver, _collisionConfiguration);
2019-11-23 20:27:39 +08:00
_btPhyiscsWorld->setGravity(convertVec3TobtVector3(info->gravity));
if (info->isDebugDrawEnabled)
{
2021-12-08 00:11:53 +08:00
_debugDrawer = new Physics3DDebugDrawer();
2019-11-23 20:27:39 +08:00
_btPhyiscsWorld->setDebugDrawer(_debugDrawer);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +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();
2019-11-23 20:27:39 +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());
}
else if (physicsObj->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER)
{
_btPhyiscsWorld->addCollisionObject(static_cast<Physics3DCollider*>(physicsObj)->getGhostObject());
}
2021-12-25 10:04:45 +08:00
_collisionCheckingFlag = true;
2019-11-23 20:27:39 +08:00
_needGhostPairCallbackChecking = true;
}
}
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());
}
else if (physicsObj->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER)
{
_btPhyiscsWorld->removeCollisionObject(static_cast<Physics3DCollider*>(physicsObj)->getGhostObject());
}
physicsObj->release();
_objects.erase(it);
2021-12-25 10:04:45 +08:00
_collisionCheckingFlag = true;
2019-11-23 20:27:39 +08:00
_needGhostPairCallbackChecking = true;
}
}
void Physics3DWorld::removeAllPhysics3DObjects()
{
2021-12-25 10:04:45 +08:00
for (auto it : _objects)
{
2019-11-23 20:27:39 +08:00
if (it->getObjType() == Physics3DObject::PhysicsObjType::RIGID_BODY)
{
_btPhyiscsWorld->removeRigidBody(static_cast<Physics3DRigidBody*>(it)->getRigidBody());
}
else if (it->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER)
{
_btPhyiscsWorld->removeCollisionObject(static_cast<Physics3DCollider*>(it)->getGhostObject());
}
it->release();
}
_objects.clear();
2021-12-25 10:04:45 +08:00
_collisionCheckingFlag = true;
2019-11-23 20:27:39 +08:00
_needGhostPairCallbackChecking = true;
}
void Physics3DWorld::addPhysics3DConstraint(Physics3DConstraint* constraint, bool disableCollisionsBetweenLinkedObjs)
{
auto it = std::find(_constraints.begin(), _constraints.end(), constraint);
if (it != _constraints.end())
return;
2019-11-23 20:27:39 +08:00
auto body = constraint->getBodyA();
if (body)
body->addConstraint(constraint);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
body = constraint->getBodyB();
if (body)
{
body->addConstraint(constraint);
}
_btPhyiscsWorld->addConstraint(constraint->getbtContraint(), disableCollisionsBetweenLinkedObjs);
_constraints.push_back(constraint);
constraint->retain();
2019-11-23 20:27:39 +08:00
}
void Physics3DWorld::removePhysics3DConstraint(Physics3DConstraint* constraint)
{
auto it = std::find(_constraints.begin(), _constraints.end(), constraint);
2021-12-25 10:04:45 +08:00
if (it != _constraints.end())
{
removePhysics3DConstraintFromBullet(constraint);
_constraints.erase(it);
constraint->release();
}
2019-11-23 20:27:39 +08:00
}
void Physics3DWorld::removeAllPhysics3DConstraints()
{
2021-12-25 10:04:45 +08:00
for (auto it : _objects)
2019-11-23 20:27:39 +08:00
{
auto type = it->getObjType();
if (type == Physics3DObject::PhysicsObjType::RIGID_BODY)
{
auto& constraints = static_cast<Physics3DRigidBody*>(it)->_constraintList;
2021-12-25 10:04:45 +08:00
for (auto constraint : constraints)
{
2019-11-23 20:27:39 +08:00
_btPhyiscsWorld->removeConstraint(constraint->getbtContraint());
constraint->release();
}
constraints.clear();
}
}
2021-12-25 10:04:45 +08:00
for (auto constraint : _constraints)
{
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);
2019-11-23 20:27:39 +08:00
}
void Physics3DWorld::stepSimulate(float dt)
{
if (_btPhyiscsWorld)
{
setGhostPairCallback();
2021-12-25 10:04:45 +08:00
// should sync kinematic node before simulation
2019-11-23 20:27:39 +08:00
for (auto it : _physicsComponents)
{
it->preSimulate();
}
_btPhyiscsWorld->stepSimulation(dt, 3);
2021-12-25 10:04:45 +08:00
// sync dynamic node after simulation
2019-11-23 20:27:39 +08:00
for (auto it : _physicsComponents)
{
it->postSimulate();
}
if (needCollisionChecking())
collisionChecking();
}
}
void Physics3DWorld::debugDraw(Renderer* renderer)
{
if (_debugDrawer)
{
_debugDrawer->clear();
_btPhyiscsWorld->debugDrawWorld();
_debugDrawer->draw(renderer);
}
}
2021-12-25 10:04:45 +08:00
bool Physics3DWorld::rayCast(const cocos2d::Vec3& startPos,
const cocos2d::Vec3& endPos,
Physics3DWorld::HitResult* result)
2019-11-23 20:27:39 +08:00
{
auto btStart = convertVec3TobtVector3(startPos);
2021-12-25 10:04:45 +08:00
auto btEnd = convertVec3TobtVector3(endPos);
2019-11-23 20:27:39 +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);
2019-11-23 20:27:39 +08:00
result->hitPosition = convertbtVector3ToVec3(btResult.m_hitPointWorld);
2021-12-25 10:04:45 +08:00
result->hitNormal = convertbtVector3ToVec3(btResult.m_hitNormalWorld);
2019-11-23 20:27:39 +08:00
return true;
}
result->hitObj = nullptr;
return false;
}
2021-12-25 10:04:45 +08:00
bool Physics3DWorld::sweepShape(Physics3DShape* shape,
const cocos2d::Mat4& startTransform,
const cocos2d::Mat4& endTransform,
Physics3DWorld::HitResult* result)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
CC_ASSERT(shape->getShapeType() != Physics3DShape::ShapeType::HEIGHT_FIELD &&
shape->getShapeType() != Physics3DShape::ShapeType::MESH);
2019-11-23 20:27:39 +08:00
auto btStart = convertMat4TobtTransform(startTransform);
2021-12-25 10:04:45 +08:00
auto btEnd = convertMat4TobtTransform(endTransform);
2019-11-23 20:27:39 +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);
2019-11-23 20:27:39 +08:00
result->hitPosition = convertbtVector3ToVec3(btResult.m_hitPointWorld);
2021-12-25 10:04:45 +08:00
result->hitNormal = convertbtVector3ToVec3(btResult.m_hitNormalWorld);
2019-11-23 20:27:39 +08:00
return true;
}
result->hitObj = nullptr;
return false;
}
Physics3DObject* Physics3DWorld::getPhysicsObject(const btCollisionObject* btObj)
{
2021-12-25 10:04:45 +08:00
for (auto it : _objects)
2019-11-23 20:27:39 +08:00
{
if (it->getObjType() == Physics3DObject::PhysicsObjType::RIGID_BODY)
{
if (static_cast<Physics3DRigidBody*>(it)->getRigidBody() == btObj)
return it;
}
else if (it->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER)
{
if (static_cast<Physics3DCollider*>(it)->getGhostObject() == btObj)
return it;
}
}
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)
{
2019-11-23 20:27:39 +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())
{
2019-11-23 20:27:39 +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);
2019-11-23 20:27:39 +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)};
2019-11-23 20:27:39 +08:00
ci.collisionPointList.push_back(cp);
}
2021-12-25 10:04:45 +08:00
if (poA->needCollisionCallback())
{
2019-11-23 20:27:39 +08:00
poA->getCollisionCallback()(ci);
}
2021-12-25 10:04:45 +08:00
if (poB->needCollisionCallback())
{
2019-11-23 20:27:39 +08:00
poB->getCollisionCallback()(ci);
}
}
}
}
}
bool Physics3DWorld::needCollisionChecking()
{
2021-12-25 10:04:45 +08:00
if (_collisionCheckingFlag)
{
2019-11-23 20:27:39 +08:00
_needCollisionChecking = false;
2021-12-25 10:04:45 +08:00
for (auto it : _objects)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (it->getCollisionCallback() != nullptr)
{
2019-11-23 20:27:39 +08:00
_needCollisionChecking = true;
break;
}
}
_collisionCheckingFlag = false;
}
return _needCollisionChecking;
}
void Physics3DWorld::setGhostPairCallback()
{
2021-12-25 10:04:45 +08:00
if (_needGhostPairCallbackChecking)
{
2019-11-23 20:27:39 +08:00
bool needCallback = false;
for (auto it : _objects)
{
2021-12-25 10:04:45 +08:00
if (it->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER)
{
2019-11-23 20:27:39 +08:00
needCallback = true;
break;
}
}
2021-12-25 10:04:45 +08:00
_btPhyiscsWorld->getPairCache()->setInternalGhostPairCallback(needCallback == true ? _ghostCallback : nullptr);
2019-11-23 20:27:39 +08:00
_needGhostPairCallbackChecking = false;
}
}
NS_CC_END
2021-12-25 10:04:45 +08:00
# endif // CC_ENABLE_BULLET_INTEGRATION
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
#endif // CC_USE_3D_PHYSICS