axmol/core/physics3d/CCPhysics3DObject.cpp

557 lines
16 KiB
C++
Raw Normal View History

2015-05-08 15:49:33 +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
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.
****************************************************************************/
#include "physics3d/CCPhysics3D.h"
2015-05-08 15:49:33 +08:00
#include "base/ccUTF8.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
2021-12-25 10:04:45 +08:00
# include "bullet/btBulletCollisionCommon.h"
# include "bullet/btBulletDynamicsCommon.h"
2015-05-08 15:49:33 +08:00
NS_AX_BEGIN
2015-05-08 15:49:33 +08:00
2021-12-25 10:04:45 +08:00
Physics3DRigidBody::Physics3DRigidBody() : _btRigidBody(nullptr), _physics3DShape(nullptr) {}
2015-05-08 15:49:33 +08:00
Physics3DRigidBody::~Physics3DRigidBody()
{
if (_physicsWorld)
{
for (auto&& constraint : _constraintList)
2015-05-08 15:49:33 +08:00
{
_physicsWorld->removePhysics3DConstraint(constraint);
}
_constraintList.clear();
}
auto ms = _btRigidBody->getMotionState();
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ms);
AX_SAFE_DELETE(_btRigidBody);
AX_SAFE_RELEASE(_physics3DShape);
2015-05-08 15:49:33 +08:00
}
Physics3DRigidBody* Physics3DRigidBody::create(Physics3DRigidBodyDes* info)
{
2021-12-08 00:11:53 +08:00
auto ret = new Physics3DRigidBody();
2015-05-08 15:49:33 +08:00
if (ret->init(info))
{
ret->autorelease();
return ret;
}
2021-12-25 10:04:45 +08:00
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ret);
2015-05-08 15:49:33 +08:00
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();
2015-05-08 15:49:33 +08:00
auto localInertia = convertVec3TobtVector3(info->localInertia);
if (mass != 0.f)
{
2021-12-25 10:04:45 +08:00
shape->calculateLocalInertia(mass, localInertia);
2015-05-08 15:49:33 +08:00
}
2021-12-25 10:04:45 +08:00
auto transform = convertMat4TobtTransform(info->originalTransform);
2015-05-08 15:49:33 +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;
2015-05-08 15:49:33 +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);
}
}
void Physics3DRigidBody::applyForce(const axis::Vec3& force, const axis::Vec3& rel_pos)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->applyForce(convertVec3TobtVector3(force), convertVec3TobtVector3(rel_pos));
}
void Physics3DRigidBody::setLinearVelocity(const axis::Vec3& lin_vel)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->setLinearVelocity(convertVec3TobtVector3(lin_vel));
}
void Physics3DRigidBody::applyCentralForce(const axis::Vec3& force)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->applyCentralForce(convertVec3TobtVector3(force));
}
void Physics3DRigidBody::applyCentralImpulse(const axis::Vec3& impulse)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->applyCentralImpulse(convertVec3TobtVector3(impulse));
}
void Physics3DRigidBody::applyTorque(const axis::Vec3& torque)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->applyTorque(convertVec3TobtVector3(torque));
}
void Physics3DRigidBody::applyTorqueImpulse(const axis::Vec3& torque)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->applyTorqueImpulse(convertVec3TobtVector3(torque));
}
void Physics3DRigidBody::applyImpulse(const axis::Vec3& impulse, const axis::Vec3& rel_pos)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->applyImpulse(convertVec3TobtVector3(impulse), convertVec3TobtVector3(rel_pos));
}
2021-12-25 10:04:45 +08:00
void Physics3DRigidBody::applyDamping(float timeStep)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->applyDamping(timeStep);
}
axis::Vec3 Physics3DRigidBody::getLinearVelocity() const
2015-05-08 15:49:33 +08:00
{
return convertbtVector3ToVec3(_btRigidBody->getLinearVelocity());
}
void Physics3DRigidBody::setLinearFactor(const axis::Vec3& linearFactor)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->setLinearFactor(convertVec3TobtVector3(linearFactor));
}
axis::Vec3 Physics3DRigidBody::getLinearFactor() const
2015-05-08 15:49:33 +08:00
{
return convertbtVector3ToVec3(_btRigidBody->getLinearFactor());
}
void Physics3DRigidBody::setAngularFactor(const axis::Vec3& angFac)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->setAngularFactor(convertVec3TobtVector3(angFac));
}
2021-12-25 10:04:45 +08:00
void Physics3DRigidBody::setAngularFactor(float angFac)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->setAngularFactor(angFac);
}
axis::Vec3 Physics3DRigidBody::getAngularFactor() const
2015-05-08 15:49:33 +08:00
{
return convertbtVector3ToVec3(_btRigidBody->getAngularFactor());
}
void Physics3DRigidBody::setAngularVelocity(const axis::Vec3& ang_vel)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->setAngularVelocity(convertVec3TobtVector3(ang_vel));
}
axis::Vec3 Physics3DRigidBody::getAngularVelocity() const
2015-05-08 15:49:33 +08:00
{
return convertbtVector3ToVec3(_btRigidBody->getAngularVelocity());
}
void Physics3DRigidBody::setCenterOfMassTransform(const axis::Mat4& xform)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->setCenterOfMassTransform(convertMat4TobtTransform(xform));
}
axis::Mat4 Physics3DRigidBody::getCenterOfMassTransform() const
2015-05-08 15:49:33 +08:00
{
return convertbtTransformToMat4(_btRigidBody->getCenterOfMassTransform());
}
2021-12-25 10:04:45 +08:00
void Physics3DRigidBody::setDamping(float lin_damping, float ang_damping)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->setDamping(lin_damping, ang_damping);
}
float Physics3DRigidBody::getLinearDamping() const
{
return _btRigidBody->getLinearDamping();
}
float Physics3DRigidBody::getAngularDamping() const
{
return _btRigidBody->getAngularDamping();
}
void Physics3DRigidBody::setGravity(const axis::Vec3& acceleration)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->setGravity(convertVec3TobtVector3(acceleration));
}
axis::Vec3 Physics3DRigidBody::getGravity() const
2015-05-08 15:49:33 +08:00
{
return convertbtVector3ToVec3(_btRigidBody->getGravity());
}
void Physics3DRigidBody::setInvInertiaDiagLocal(const axis::Vec3& diagInvInertia)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->setInvInertiaDiagLocal(convertVec3TobtVector3(diagInvInertia));
}
axis::Vec3 Physics3DRigidBody::getInvInertiaDiagLocal() const
2015-05-08 15:49:33 +08:00
{
return convertbtVector3ToVec3(_btRigidBody->getInvInertiaDiagLocal());
}
void Physics3DRigidBody::setMassProps(float mass, const axis::Vec3& inertia)
2015-05-08 15:49:33 +08:00
{
_btRigidBody->setMassProps(mass, convertVec3TobtVector3(inertia));
}
float Physics3DRigidBody::getInvMass() const
{
return _btRigidBody->getInvMass();
}
axis::Vec3 Physics3DRigidBody::getTotalForce() const
2015-05-08 15:49:33 +08:00
{
return convertbtVector3ToVec3(_btRigidBody->getTotalForce());
}
axis::Vec3 Physics3DRigidBody::getTotalTorque() const
2015-05-08 15:49:33 +08:00
{
return convertbtVector3ToVec3(_btRigidBody->getTotalTorque());
}
2021-12-25 10:04:45 +08:00
void Physics3DRigidBody::setRestitution(float rest)
2015-05-08 15:49:33 +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)
2015-05-08 15:49:33 +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)
2015-05-08 15:49:33 +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)
2015-05-08 15:49:33 +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)
2015-05-08 15:49:33 +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)
2015-05-08 15:49:33 +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)
2015-05-08 15:49:33 +08:00
{
auto iter = std::find(_constraintList.begin(), _constraintList.end(), constraint);
2021-12-25 10:04:45 +08:00
if (iter == _constraintList.end())
{
2015-05-08 15:49:33 +08:00
_constraintList.push_back(constraint);
constraint->retain();
}
}
2021-12-25 10:04:45 +08:00
void Physics3DRigidBody::removeConstraint(Physics3DConstraint* constraint)
2015-05-08 15:49:33 +08:00
{
auto iter = std::find(_constraintList.begin(), _constraintList.end(), constraint);
2021-12-25 10:04:45 +08:00
if (iter != _constraintList.end())
{
2015-05-08 15:49:33 +08:00
constraint->release();
_constraintList.erase(iter);
}
}
2021-12-25 10:04:45 +08:00
void Physics3DRigidBody::removeConstraint(unsigned int idx)
2015-05-08 15:49:33 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(idx < _constraintList.size(), "idx < _constraintList.size()");
2015-05-08 15:49:33 +08:00
removeConstraint(_constraintList[idx]);
}
2021-12-25 10:04:45 +08:00
Physics3DConstraint* Physics3DRigidBody::getConstraint(unsigned int idx) const
2015-05-08 15:49:33 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(idx < _constraintList.size(), "idx < _constraintList.size()");
2015-05-08 15:49:33 +08:00
return _constraintList[idx];
}
unsigned int Physics3DRigidBody::getConstraintCount() const
{
return (unsigned int)_constraintList.size();
}
axis::Mat4 Physics3DRigidBody::getWorldTransform() const
2015-05-08 15:49:33 +08:00
{
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;
}
2015-06-29 10:26:09 +08:00
class btCollider : public btGhostObject
{
public:
2021-12-25 10:04:45 +08:00
btCollider(Physics3DCollider* collider) : _collider(collider){};
2015-06-29 10:26:09 +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
2015-06-29 10:26:09 +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
2015-06-29 10:26:09 +08:00
int index = m_overlappingObjects.findLinearSearch(otherObject);
if (index == m_overlappingObjects.size())
{
2021-12-25 10:04:45 +08:00
// not found
2015-06-29 10:26:09 +08:00
m_overlappingObjects.push_back(otherObject);
2015-07-30 18:18:02 +08:00
if (_collider->onTriggerEnter != nullptr && _collider->isTrigger())
2015-06-29 10:26:09 +08:00
_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
2015-06-29 10:26:09 +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();
2015-07-30 18:18:02 +08:00
if (_collider->onTriggerExit != nullptr && _collider->isTrigger())
2015-06-29 10:26:09 +08:00
_collider->onTriggerExit(getPhysicsObject(otherObject));
}
}
Physics3DObject* getPhysicsObject(const btCollisionObject* btObj)
{
for (auto&& it : _collider->getPhysicsWorld()->getPhysicsObjects())
2015-06-29 10:26:09 +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;
}
private:
2021-12-25 10:04:45 +08:00
Physics3DCollider* _collider;
2015-06-29 10:26:09 +08:00
};
2021-12-25 10:04:45 +08:00
Physics3DCollider::Physics3DCollider() : _btGhostObject(nullptr), _physics3DShape(nullptr) {}
2015-06-29 10:26:09 +08:00
Physics3DCollider::~Physics3DCollider()
{
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(_btGhostObject);
AX_SAFE_RELEASE(_physics3DShape);
2015-06-29 10:26:09 +08:00
}
2021-12-25 10:04:45 +08:00
Physics3DCollider* Physics3DCollider::create(Physics3DColliderDes* info)
2015-06-29 10:26:09 +08:00
{
2021-12-08 00:11:53 +08:00
auto ret = new Physics3DCollider();
2015-06-30 14:16:05 +08:00
if (ret->init(info))
2015-06-29 10:26:09 +08:00
{
ret->autorelease();
return ret;
}
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ret);
2015-06-29 10:26:09 +08:00
return ret;
}
2015-06-29 17:20:28 +08:00
float Physics3DCollider::getCcdSweptSphereRadius() const
2015-06-29 10:26:09 +08:00
{
2015-06-29 17:20:28 +08:00
return _btGhostObject->getCcdSweptSphereRadius();
2015-06-29 10:26:09 +08:00
}
2015-06-29 17:20:28 +08:00
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);
}
2015-07-30 18:14:46 +08:00
bool Physics3DCollider::isTrigger() const
2015-06-29 17:20:28 +08:00
{
return (_btGhostObject->getCollisionFlags() & btCollisionObject::CF_NO_CONTACT_RESPONSE) != 0;
}
2015-06-30 14:16:05 +08:00
void Physics3DCollider::setTrigger(bool isTrigger)
2015-06-29 10:26:09 +08:00
{
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);
2015-06-29 10:26:09 +08:00
}
2021-12-25 10:04:45 +08:00
bool Physics3DCollider::init(Physics3DColliderDes* info)
2015-06-29 10:26:09 +08:00
{
2015-06-30 14:16:05 +08:00
_physics3DShape = info->shape;
2015-06-29 10:26:09 +08:00
_physics3DShape->retain();
_btGhostObject = new btCollider(this);
_btGhostObject->setCollisionShape(_physics3DShape->getbtShape());
2021-12-25 10:04:45 +08:00
2015-06-30 14:16:05 +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
2015-06-29 10:26:09 +08:00
_type = Physics3DObject::PhysicsObjType::COLLIDER;
return true;
}
axis::Mat4 Physics3DCollider::getWorldTransform() const
2015-06-29 10:26:09 +08:00
{
return convertbtTransformToMat4(_btGhostObject->getWorldTransform());
}
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