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
|
|
|
|
2022-07-09 22:23:34 +08:00
|
|
|
https://axis-project.github.io/
|
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 "base/ccUTF8.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
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "bullet/btBulletCollisionCommon.h"
|
|
|
|
# include "bullet/btBulletDynamicsCommon.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Physics3DRigidBody::Physics3DRigidBody() : _btRigidBody(nullptr), _physics3DShape(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Physics3DRigidBody::~Physics3DRigidBody()
|
|
|
|
{
|
|
|
|
if (_physicsWorld)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto constraint : _constraintList)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_physicsWorld->removePhysics3DConstraint(constraint);
|
|
|
|
}
|
|
|
|
_constraintList.clear();
|
|
|
|
}
|
|
|
|
auto ms = _btRigidBody->getMotionState();
|
|
|
|
CC_SAFE_DELETE(ms);
|
|
|
|
CC_SAFE_DELETE(_btRigidBody);
|
|
|
|
CC_SAFE_RELEASE(_physics3DShape);
|
|
|
|
}
|
|
|
|
|
|
|
|
Physics3DRigidBody* Physics3DRigidBody::create(Physics3DRigidBodyDes* info)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto ret = new Physics3DRigidBody();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (ret->init(info))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Physics3DRigidBody::init(Physics3DRigidBodyDes* info)
|
|
|
|
{
|
|
|
|
if (info->shape == nullptr)
|
|
|
|
return false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
btScalar mass = info->mass;
|
|
|
|
auto shape = info->shape->getbtShape();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto localInertia = convertVec3TobtVector3(info->localInertia);
|
|
|
|
if (mass != 0.f)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
shape->calculateLocalInertia(mass, localInertia);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto transform = convertMat4TobtTransform(info->originalTransform);
|
2019-11-23 20:27:39 +08:00
|
|
|
btDefaultMotionState* myMotionState = new btDefaultMotionState(transform);
|
2021-12-25 10:04:45 +08:00
|
|
|
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, shape, localInertia);
|
|
|
|
_btRigidBody = new btRigidBody(rbInfo);
|
|
|
|
_type = Physics3DObject::PhysicsObjType::RIGID_BODY;
|
2019-11-23 20:27:39 +08:00
|
|
|
_physics3DShape = info->shape;
|
|
|
|
_physics3DShape->retain();
|
|
|
|
if (info->disableSleep)
|
|
|
|
_btRigidBody->setActivationState(DISABLE_DEACTIVATION);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DRigidBody::setActive(bool active)
|
|
|
|
{
|
|
|
|
if (_btRigidBody)
|
|
|
|
{
|
|
|
|
_btRigidBody->setActivationState(active ? ACTIVE_TAG : WANTS_DEACTIVATION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::applyForce(const cocos2d::Vec3& force, const cocos2d::Vec3& rel_pos)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->applyForce(convertVec3TobtVector3(force), convertVec3TobtVector3(rel_pos));
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setLinearVelocity(const cocos2d::Vec3& lin_vel)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setLinearVelocity(convertVec3TobtVector3(lin_vel));
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::applyCentralForce(const cocos2d::Vec3& force)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->applyCentralForce(convertVec3TobtVector3(force));
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::applyCentralImpulse(const cocos2d::Vec3& impulse)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->applyCentralImpulse(convertVec3TobtVector3(impulse));
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::applyTorque(const cocos2d::Vec3& torque)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->applyTorque(convertVec3TobtVector3(torque));
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::applyTorqueImpulse(const cocos2d::Vec3& torque)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->applyTorqueImpulse(convertVec3TobtVector3(torque));
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::applyImpulse(const cocos2d::Vec3& impulse, const cocos2d::Vec3& rel_pos)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->applyImpulse(convertVec3TobtVector3(impulse), convertVec3TobtVector3(rel_pos));
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::applyDamping(float timeStep)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->applyDamping(timeStep);
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Vec3 Physics3DRigidBody::getLinearVelocity() const
|
|
|
|
{
|
|
|
|
return convertbtVector3ToVec3(_btRigidBody->getLinearVelocity());
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setLinearFactor(const cocos2d::Vec3& linearFactor)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setLinearFactor(convertVec3TobtVector3(linearFactor));
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Vec3 Physics3DRigidBody::getLinearFactor() const
|
|
|
|
{
|
|
|
|
return convertbtVector3ToVec3(_btRigidBody->getLinearFactor());
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setAngularFactor(const cocos2d::Vec3& angFac)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setAngularFactor(convertVec3TobtVector3(angFac));
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setAngularFactor(float angFac)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setAngularFactor(angFac);
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Vec3 Physics3DRigidBody::getAngularFactor() const
|
|
|
|
{
|
|
|
|
return convertbtVector3ToVec3(_btRigidBody->getAngularFactor());
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setAngularVelocity(const cocos2d::Vec3& ang_vel)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setAngularVelocity(convertVec3TobtVector3(ang_vel));
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Vec3 Physics3DRigidBody::getAngularVelocity() const
|
|
|
|
{
|
|
|
|
return convertbtVector3ToVec3(_btRigidBody->getAngularVelocity());
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setCenterOfMassTransform(const cocos2d::Mat4& xform)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setCenterOfMassTransform(convertMat4TobtTransform(xform));
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Mat4 Physics3DRigidBody::getCenterOfMassTransform() const
|
|
|
|
{
|
|
|
|
return convertbtTransformToMat4(_btRigidBody->getCenterOfMassTransform());
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setDamping(float lin_damping, float ang_damping)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setDamping(lin_damping, ang_damping);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DRigidBody::getLinearDamping() const
|
|
|
|
{
|
|
|
|
return _btRigidBody->getLinearDamping();
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DRigidBody::getAngularDamping() const
|
|
|
|
{
|
|
|
|
return _btRigidBody->getAngularDamping();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setGravity(const cocos2d::Vec3& acceleration)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setGravity(convertVec3TobtVector3(acceleration));
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Vec3 Physics3DRigidBody::getGravity() const
|
|
|
|
{
|
|
|
|
return convertbtVector3ToVec3(_btRigidBody->getGravity());
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setInvInertiaDiagLocal(const cocos2d::Vec3& diagInvInertia)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setInvInertiaDiagLocal(convertVec3TobtVector3(diagInvInertia));
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Vec3 Physics3DRigidBody::getInvInertiaDiagLocal() const
|
|
|
|
{
|
|
|
|
return convertbtVector3ToVec3(_btRigidBody->getInvInertiaDiagLocal());
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setMassProps(float mass, const cocos2d::Vec3& inertia)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setMassProps(mass, convertVec3TobtVector3(inertia));
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DRigidBody::getInvMass() const
|
|
|
|
{
|
|
|
|
return _btRigidBody->getInvMass();
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Vec3 Physics3DRigidBody::getTotalForce() const
|
|
|
|
{
|
|
|
|
return convertbtVector3ToVec3(_btRigidBody->getTotalForce());
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Vec3 Physics3DRigidBody::getTotalTorque() const
|
|
|
|
{
|
|
|
|
return convertbtVector3ToVec3(_btRigidBody->getTotalTorque());
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setRestitution(float rest)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setRestitution(rest);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DRigidBody::getRestitution() const
|
|
|
|
{
|
|
|
|
return _btRigidBody->getRestitution();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setFriction(float frict)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setFriction(frict);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DRigidBody::getFriction() const
|
|
|
|
{
|
|
|
|
return _btRigidBody->getFriction();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setRollingFriction(float frict)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setRollingFriction(frict);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DRigidBody::getRollingFriction() const
|
|
|
|
{
|
|
|
|
return _btRigidBody->getRollingFriction();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setHitFraction(float hitFraction)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setHitFraction(hitFraction);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DRigidBody::getHitFraction() const
|
|
|
|
{
|
|
|
|
return _btRigidBody->getHitFraction();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setCcdMotionThreshold(float ccdMotionThreshold)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setCcdMotionThreshold(ccdMotionThreshold);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DRigidBody::getCcdMotionThreshold() const
|
|
|
|
{
|
|
|
|
return _btRigidBody->getCcdMotionThreshold();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::setCcdSweptSphereRadius(float radius)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_btRigidBody->setCcdSweptSphereRadius(radius);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DRigidBody::getCcdSweptSphereRadius() const
|
|
|
|
{
|
|
|
|
return _btRigidBody->getCcdSweptSphereRadius();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::addConstraint(Physics3DConstraint* constraint)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto iter = std::find(_constraintList.begin(), _constraintList.end(), constraint);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (iter == _constraintList.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_constraintList.push_back(constraint);
|
|
|
|
constraint->retain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::removeConstraint(Physics3DConstraint* constraint)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto iter = std::find(_constraintList.begin(), _constraintList.end(), constraint);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (iter != _constraintList.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
constraint->release();
|
|
|
|
_constraintList.erase(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DRigidBody::removeConstraint(unsigned int idx)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(idx < _constraintList.size(), "idx < _constraintList.size()");
|
|
|
|
removeConstraint(_constraintList[idx]);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Physics3DConstraint* Physics3DRigidBody::getConstraint(unsigned int idx) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(idx < _constraintList.size(), "idx < _constraintList.size()");
|
|
|
|
return _constraintList[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Physics3DRigidBody::getConstraintCount() const
|
|
|
|
{
|
|
|
|
return (unsigned int)_constraintList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Mat4 Physics3DRigidBody::getWorldTransform() const
|
|
|
|
{
|
|
|
|
const auto& transform = _btRigidBody->getWorldTransform();
|
|
|
|
return convertbtTransformToMat4(transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DRigidBody::setKinematic(bool kinematic)
|
|
|
|
{
|
|
|
|
if (kinematic)
|
|
|
|
{
|
|
|
|
_btRigidBody->setCollisionFlags(_btRigidBody->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
|
|
|
|
_btRigidBody->setActivationState(DISABLE_DEACTIVATION);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_btRigidBody->setCollisionFlags(_btRigidBody->getCollisionFlags() & ~btCollisionObject::CF_KINEMATIC_OBJECT);
|
|
|
|
_btRigidBody->setActivationState(ACTIVE_TAG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Physics3DRigidBody::isKinematic() const
|
|
|
|
{
|
|
|
|
if (_btRigidBody)
|
|
|
|
return _btRigidBody->isKinematicObject();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
class btCollider : public btGhostObject
|
|
|
|
{
|
|
|
|
public:
|
2021-12-25 10:04:45 +08:00
|
|
|
btCollider(Physics3DCollider* collider) : _collider(collider){};
|
2019-11-23 20:27:39 +08:00
|
|
|
~btCollider(){};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/// this method is mainly for expert/internal use only.
|
|
|
|
virtual void addOverlappingObjectInternal(btBroadphaseProxy* otherProxy,
|
|
|
|
btBroadphaseProxy* thisProxy = nullptr) override
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
|
|
|
|
btAssert(otherObject);
|
2021-12-25 10:04:45 +08:00
|
|
|
/// if this linearSearch becomes too slow (too many overlapping objects) we should add a more appropriate data
|
|
|
|
/// structure
|
2019-11-23 20:27:39 +08:00
|
|
|
int index = m_overlappingObjects.findLinearSearch(otherObject);
|
|
|
|
if (index == m_overlappingObjects.size())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// not found
|
2019-11-23 20:27:39 +08:00
|
|
|
m_overlappingObjects.push_back(otherObject);
|
|
|
|
if (_collider->onTriggerEnter != nullptr && _collider->isTrigger())
|
|
|
|
_collider->onTriggerEnter(getPhysicsObject(otherObject));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/// this method is mainly for expert/internal use only.
|
|
|
|
virtual void removeOverlappingObjectInternal(btBroadphaseProxy* otherProxy,
|
|
|
|
btDispatcher* /*dispatcher*/,
|
|
|
|
btBroadphaseProxy* thisProxy = nullptr) override
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
|
|
|
|
btAssert(otherObject);
|
|
|
|
int index = m_overlappingObjects.findLinearSearch(otherObject);
|
|
|
|
if (index < m_overlappingObjects.size())
|
|
|
|
{
|
|
|
|
m_overlappingObjects[index] = m_overlappingObjects[m_overlappingObjects.size() - 1];
|
|
|
|
m_overlappingObjects.pop_back();
|
|
|
|
|
|
|
|
if (_collider->onTriggerExit != nullptr && _collider->isTrigger())
|
|
|
|
_collider->onTriggerExit(getPhysicsObject(otherObject));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Physics3DObject* getPhysicsObject(const btCollisionObject* btObj)
|
|
|
|
{
|
|
|
|
for (auto it : _collider->getPhysicsWorld()->getPhysicsObjects())
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-12-25 10:04:45 +08:00
|
|
|
Physics3DCollider* _collider;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Physics3DCollider::Physics3DCollider() : _btGhostObject(nullptr), _physics3DShape(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Physics3DCollider::~Physics3DCollider()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_btGhostObject);
|
|
|
|
CC_SAFE_RELEASE(_physics3DShape);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Physics3DCollider* Physics3DCollider::create(Physics3DColliderDes* info)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto ret = new Physics3DCollider();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (ret->init(info))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DCollider::getCcdSweptSphereRadius() const
|
|
|
|
{
|
|
|
|
return _btGhostObject->getCcdSweptSphereRadius();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DCollider::setCcdSweptSphereRadius(float radius)
|
|
|
|
{
|
|
|
|
_btGhostObject->setCcdSweptSphereRadius(radius);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DCollider::getCcdMotionThreshold() const
|
|
|
|
{
|
|
|
|
return _btGhostObject->getCcdMotionThreshold();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DCollider::setCcdMotionThreshold(float ccdMotionThreshold)
|
|
|
|
{
|
|
|
|
_btGhostObject->setCcdMotionThreshold(ccdMotionThreshold);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DCollider::getHitFraction() const
|
|
|
|
{
|
|
|
|
return _btGhostObject->getHitFraction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DCollider::setHitFraction(float hitFraction)
|
|
|
|
{
|
|
|
|
_btGhostObject->setHitFraction(hitFraction);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DCollider::getRollingFriction() const
|
|
|
|
{
|
|
|
|
return _btGhostObject->getRollingFriction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DCollider::setRollingFriction(float frict)
|
|
|
|
{
|
|
|
|
_btGhostObject->setRollingFriction(frict);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DCollider::getFriction() const
|
|
|
|
{
|
|
|
|
return _btGhostObject->getFriction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DCollider::setFriction(float frict)
|
|
|
|
{
|
|
|
|
_btGhostObject->setFriction(frict);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Physics3DCollider::getRestitution() const
|
|
|
|
{
|
|
|
|
return _btGhostObject->getRestitution();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DCollider::setRestitution(float rest)
|
|
|
|
{
|
|
|
|
_btGhostObject->setRestitution(rest);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Physics3DCollider::isTrigger() const
|
|
|
|
{
|
|
|
|
return (_btGhostObject->getCollisionFlags() & btCollisionObject::CF_NO_CONTACT_RESPONSE) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DCollider::setTrigger(bool isTrigger)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_btGhostObject->setCollisionFlags(
|
|
|
|
isTrigger == true ? _btGhostObject->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE
|
|
|
|
: _btGhostObject->getCollisionFlags() & ~btCollisionObject::CF_NO_CONTACT_RESPONSE);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Physics3DCollider::init(Physics3DColliderDes* info)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_physics3DShape = info->shape;
|
|
|
|
_physics3DShape->retain();
|
|
|
|
_btGhostObject = new btCollider(this);
|
|
|
|
_btGhostObject->setCollisionShape(_physics3DShape->getbtShape());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
setTrigger(info->isTrigger);
|
|
|
|
setFriction(info->friction);
|
|
|
|
setRollingFriction(info->rollingFriction);
|
|
|
|
setRestitution(info->restitution);
|
|
|
|
setHitFraction(info->hitFraction);
|
|
|
|
setCcdSweptSphereRadius(info->ccdSweptSphereRadius);
|
|
|
|
setCcdMotionThreshold(info->ccdMotionThreshold);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_type = Physics3DObject::PhysicsObjType::COLLIDER;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Mat4 Physics3DCollider::getWorldTransform() const
|
|
|
|
{
|
|
|
|
return convertbtTransformToMat4(_btGhostObject->getWorldTransform());
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|