axmol/core/physics3d/Physics3DObject.h

504 lines
14 KiB
C
Raw Normal View History

2022-01-04 12:36:20 +08:00
/****************************************************************************
2019-11-23 20:27:39 +08:00
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
2021-12-25 10:04:45 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.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.
****************************************************************************/
#ifndef __PHYSICS_3D_OBJECT_H__
#define __PHYSICS_3D_OBJECT_H__
#include "math/Math.h"
#include "base/Object.h"
#include "base/Config.h"
2019-11-23 20:27:39 +08:00
#include <vector>
#if defined(AX_ENABLE_3D_PHYSICS)
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
# if (AX_ENABLE_BULLET_INTEGRATION)
2019-11-23 20:27:39 +08:00
class btCollisionShape;
class btRigidBody;
class btPersistentManifold;
class btGhostObject;
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
/**
* @addtogroup _3d
* @{
*/
class Physics3DShape;
class Physics3DWorld;
class Physics3DConstraint;
class Physics3DObject;
/**
* @brief The collision information of Physics3DObject.
*/
2022-07-16 10:43:05 +08:00
struct AX_DLL Physics3DCollisionInfo
2019-11-23 20:27:39 +08:00
{
struct CollisionPoint
{
Vec3 localPositionOnA;
Vec3 worldPositionOnA;
Vec3 localPositionOnB;
Vec3 worldPositionOnB;
Vec3 worldNormalOnB;
};
2021-12-25 10:04:45 +08:00
Physics3DObject* objA;
Physics3DObject* objB;
2019-11-23 20:27:39 +08:00
std::vector<CollisionPoint> collisionPointList;
};
/**
* @brief Inherit from Object, base class
2019-11-23 20:27:39 +08:00
*/
class AX_DLL Physics3DObject : public Object
2019-11-23 20:27:39 +08:00
{
public:
2021-12-25 10:04:45 +08:00
typedef std::function<void(const Physics3DCollisionInfo& ci)> CollisionCallbackFunc;
2019-11-23 20:27:39 +08:00
enum class PhysicsObjType
{
UNKNOWN = 0,
RIGID_BODY,
COLLIDER,
};
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Get the Physics3DObject Type. */
virtual PhysicsObjType getObjType() const { return _type; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Set the user data. */
2021-12-25 10:04:45 +08:00
void setUserData(void* userData) { _userData = userData; }
2019-11-23 20:27:39 +08:00
/** Get the user data. */
void* getUserData() const { return _userData; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Internal method. Set the pointer of Physics3DWorld. */
void setPhysicsWorld(Physics3DWorld* world) { _physicsWorld = world; };
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Get the pointer of Physics3DWorld. */
Physics3DWorld* getPhysicsWorld() const { return _physicsWorld; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Get the world matrix of Physics3DObject. */
2022-08-08 18:02:17 +08:00
virtual ax::Mat4 getWorldTransform() const = 0;
2019-11-23 20:27:39 +08:00
/** Set the collision callback function. */
2021-12-25 10:04:45 +08:00
void setCollisionCallback(const CollisionCallbackFunc& func) { _collisionCallbackFunc = func; };
2019-11-23 20:27:39 +08:00
/** Get the collision callback function. */
const CollisionCallbackFunc& getCollisionCallback() const { return _collisionCallbackFunc; }
/** Check has collision callback function. */
bool needCollisionCallback() { return _collisionCallbackFunc != nullptr; };
/** Set the mask of Physics3DObject. */
void setMask(unsigned int mask) { _mask = mask; };
/** Get the mask of Physics3DObject. */
unsigned int getMask() const { return _mask; };
2021-12-25 10:04:45 +08:00
Physics3DObject()
2021-12-25 10:04:45 +08:00
: _isEnabled(true), _type(PhysicsObjType::UNKNOWN), _userData(nullptr), _physicsWorld(nullptr), _mask(-1)
{}
virtual ~Physics3DObject() {}
2019-11-23 20:27:39 +08:00
protected:
2021-12-25 10:04:45 +08:00
bool _isEnabled;
2019-11-23 20:27:39 +08:00
PhysicsObjType _type;
2021-12-25 10:04:45 +08:00
void* _userData;
2019-11-23 20:27:39 +08:00
Physics3DWorld* _physicsWorld;
CollisionCallbackFunc _collisionCallbackFunc;
unsigned int _mask;
};
/**
* @brief The description of Physics3DRigidBody.
*/
2022-07-16 10:43:05 +08:00
struct AX_DLL Physics3DRigidBodyDes
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
float mass; // Note: mass equals zero means static, default 0
2022-08-08 18:02:17 +08:00
ax::Vec3 localInertia; // default (0, 0, 0)
2019-11-23 20:27:39 +08:00
Physics3DShape* shape;
2022-08-08 18:02:17 +08:00
ax::Mat4 originalTransform;
2021-12-25 10:04:45 +08:00
bool disableSleep; // it is always active if disabled
Physics3DRigidBodyDes() : mass(0.f), localInertia(0.f, 0.f, 0.f), shape(nullptr), disableSleep(false) {}
2019-11-23 20:27:39 +08:00
};
/**
* @brief Inherit from Physics3DObject, the main class for rigid body objects
*/
2022-07-16 10:43:05 +08:00
class AX_DLL Physics3DRigidBody : public Physics3DObject
2019-11-23 20:27:39 +08:00
{
friend class Physics3DWorld;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
public:
/**
2021-12-25 10:04:45 +08:00
* Creates a Physics3DRigidBody with Physics3DRigidBody.
2019-11-23 20:27:39 +08:00
*
* @return An autoreleased Physics3DRigidBody object.
*/
static Physics3DRigidBody* create(Physics3DRigidBodyDes* info);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Get the pointer of btRigidBody. */
btRigidBody* getRigidBody() const { return _btRigidBody; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Apply a force.
*
* @param force the value of the force
* @param rel_pos the position of the force
*/
2022-08-08 18:02:17 +08:00
void applyForce(const ax::Vec3& force, const ax::Vec3& rel_pos);
2019-11-23 20:27:39 +08:00
/**
* Apply a central force.
*
* @param force the value of the force
*/
2022-08-08 18:02:17 +08:00
void applyCentralForce(const ax::Vec3& force);
2019-11-23 20:27:39 +08:00
/**
* Apply a central impulse.
*
* @param impulse the value of the impulse
*/
2022-08-08 18:02:17 +08:00
void applyCentralImpulse(const ax::Vec3& impulse);
2019-11-23 20:27:39 +08:00
/**
* Apply a torque.
*
* @param torque the value of the torque
*/
2022-08-08 18:02:17 +08:00
void applyTorque(const ax::Vec3& torque);
2019-11-23 20:27:39 +08:00
/**
* Apply a torque impulse.
*
* @param torque the value of the torque
*/
2022-08-08 18:02:17 +08:00
void applyTorqueImpulse(const ax::Vec3& torque);
2019-11-23 20:27:39 +08:00
/**
* Apply a impulse.
*
* @param impulse the value of the impulse
* @param rel_pos the position of the impulse
*/
2022-08-08 18:02:17 +08:00
void applyImpulse(const ax::Vec3& impulse, const ax::Vec3& rel_pos);
2019-11-23 20:27:39 +08:00
/** Damps the velocity, using the given linearDamping and angularDamping. */
void applyDamping(float timeStep);
/** Set the linear velocity. */
2022-08-08 18:02:17 +08:00
void setLinearVelocity(const ax::Vec3& lin_vel);
2019-11-23 20:27:39 +08:00
/** Get the linear velocity. */
2022-08-08 18:02:17 +08:00
ax::Vec3 getLinearVelocity() const;
2019-11-23 20:27:39 +08:00
/** Set the linear factor. */
2022-08-08 18:02:17 +08:00
void setLinearFactor(const ax::Vec3& linearFactor);
2019-11-23 20:27:39 +08:00
/** Get the linear factor. */
2022-08-08 18:02:17 +08:00
ax::Vec3 getLinearFactor() const;
2019-11-23 20:27:39 +08:00
/** Set the angular factor. */
2022-08-08 18:02:17 +08:00
void setAngularFactor(const ax::Vec3& angFac);
2019-11-23 20:27:39 +08:00
/** Set the angular factor, use unified factor. */
void setAngularFactor(float angFac);
/** Get the angular factor. */
2022-08-08 18:02:17 +08:00
ax::Vec3 getAngularFactor() const;
2019-11-23 20:27:39 +08:00
/** Set the angular velocity. */
2022-08-08 18:02:17 +08:00
void setAngularVelocity(const ax::Vec3& ang_vel);
2019-11-23 20:27:39 +08:00
/** Get the angular velocity. */
2022-08-08 18:02:17 +08:00
ax::Vec3 getAngularVelocity() const;
2019-11-23 20:27:39 +08:00
/** Set the center of mass. */
2022-08-08 18:02:17 +08:00
void setCenterOfMassTransform(const ax::Mat4& xform);
2019-11-23 20:27:39 +08:00
/** Get the center of mass. */
2022-08-08 18:02:17 +08:00
ax::Mat4 getCenterOfMassTransform() const;
2019-11-23 20:27:39 +08:00
/** Set linear damping and angular damping. */
void setDamping(float lin_damping, float ang_damping);
/** Get linear damping. */
float getLinearDamping() const;
/** Get angular damping. */
float getAngularDamping() const;
/** Set the acceleration. */
2022-08-08 18:02:17 +08:00
void setGravity(const ax::Vec3& acceleration);
2019-11-23 20:27:39 +08:00
/** Get the acceleration. */
2022-08-08 18:02:17 +08:00
ax::Vec3 getGravity() const;
2019-11-23 20:27:39 +08:00
/** Set the inverse of local inertia. */
2022-08-08 18:02:17 +08:00
void setInvInertiaDiagLocal(const ax::Vec3& diagInvInertia);
2019-11-23 20:27:39 +08:00
/** Get the inverse of local inertia. */
2022-08-08 18:02:17 +08:00
ax::Vec3 getInvInertiaDiagLocal() const;
2019-11-23 20:27:39 +08:00
/** Set mass and inertia. */
2022-08-08 18:02:17 +08:00
void setMassProps(float mass, const ax::Vec3& inertia);
2019-11-23 20:27:39 +08:00
/** Get inverse of mass. */
float getInvMass() const;
/** Get total force. */
2022-08-08 18:02:17 +08:00
ax::Vec3 getTotalForce() const;
2019-11-23 20:27:39 +08:00
/** Get total torque. */
2022-08-08 18:02:17 +08:00
ax::Vec3 getTotalTorque() const;
2019-11-23 20:27:39 +08:00
/** Set restitution. */
void setRestitution(float rest);
/** Get restitution. */
float getRestitution() const;
/** Set friction. */
void setFriction(float frict);
/** Get friction. */
float getFriction() const;
/** Set rolling friction. */
void setRollingFriction(float frict);
/** Get rolling friction. */
float getRollingFriction() const;
/** Set hit friction. */
void setHitFraction(float hitFraction);
/** Get hit friction. */
float getHitFraction() const;
2021-12-25 10:04:45 +08:00
/** Set motion threshold, don't do continuous collision detection if the motion (in one step) is less then
* ccdMotionThreshold */
2019-11-23 20:27:39 +08:00
void setCcdMotionThreshold(float ccdMotionThreshold);
/** Get motion threshold. */
float getCcdMotionThreshold() const;
/** Set swept sphere radius. */
void setCcdSweptSphereRadius(float radius);
/** Get swept sphere radius. */
float getCcdSweptSphereRadius() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Set kinematic object. */
void setKinematic(bool kinematic);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Check rigid body is kinematic object. */
bool isKinematic() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** override. */
2022-08-08 18:02:17 +08:00
virtual ax::Mat4 getWorldTransform() const override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Get constraint by index. */
Physics3DConstraint* getConstraint(unsigned int idx) const;
/** Get the total number of constraints. */
unsigned int getConstraintCount() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Active or inactive. */
void setActive(bool active);
Physics3DRigidBody();
2019-11-23 20:27:39 +08:00
virtual ~Physics3DRigidBody();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool init(Physics3DRigidBodyDes* info);
2021-12-25 10:04:45 +08:00
void addConstraint(Physics3DConstraint* constraint);
void removeConstraint(Physics3DConstraint* constraint);
2019-11-23 20:27:39 +08:00
void removeConstraint(unsigned int idx);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
btRigidBody* _btRigidBody;
2021-12-25 10:04:45 +08:00
Physics3DShape* _physics3DShape;
std::vector<Physics3DConstraint*> _constraintList;
2019-11-23 20:27:39 +08:00
};
/**
* @brief The description of Physics3DCollider.
*/
2022-07-16 10:43:05 +08:00
struct AX_DLL Physics3DColliderDes
2019-11-23 20:27:39 +08:00
{
/**shape pointer*/
Physics3DShape* shape;
/**original world Transform*/
2022-08-08 18:02:17 +08:00
ax::Mat4 originalTransform;
2019-11-23 20:27:39 +08:00
/**Is collider a trigger?*/
2021-12-25 10:04:45 +08:00
bool isTrigger;
2019-11-23 20:27:39 +08:00
/**the friction*/
float friction;
/**the rolling friction*/
float rollingFriction;
/**the restitution*/
float restitution;
/**the hit fraction*/
float hitFraction;
/**the swept sphere radius*/
float ccdSweptSphereRadius;
/**the motion threshold*/
float ccdMotionThreshold;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Physics3DColliderDes()
2021-12-25 10:04:45 +08:00
: shape(nullptr)
, isTrigger(false)
, friction(0.5f)
, rollingFriction(0.0f)
, restitution(0.0f)
, hitFraction(1.0f)
, ccdSweptSphereRadius(0.0f)
, ccdMotionThreshold(0.0f)
{}
2019-11-23 20:27:39 +08:00
};
/**
2021-12-25 10:04:45 +08:00
* @brief Inherit from Physics3DObject, the main class for Colliders.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL Physics3DCollider : public Physics3DObject
2019-11-23 20:27:39 +08:00
{
public:
/**
2021-12-25 10:04:45 +08:00
* Creates a Physics3DCollider with Physics3DColliderDes.
*
* @return An autoreleased Physics3DCollider object.
*/
static Physics3DCollider* create(Physics3DColliderDes* info);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
/** Get the pointer of btGhostObject.
2019-11-23 20:27:39 +08:00
* @return The pointer of btGhostObject.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
btGhostObject* getGhostObject() const { return _btGhostObject; }
2021-12-25 10:04:45 +08:00
/** Set trigger.
2019-11-23 20:27:39 +08:00
* @param isTrigger Is a trigger.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
void setTrigger(bool isTrigger);
2021-12-25 10:04:45 +08:00
/** Check is a trigger.
2019-11-23 20:27:39 +08:00
* @return Is a trigger.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
bool isTrigger() const;
2021-12-25 10:04:45 +08:00
/** Set restitution.
2019-11-23 20:27:39 +08:00
* @param rest The restitution.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
void setRestitution(float rest);
2021-12-25 10:04:45 +08:00
/** Get restitution.
2019-11-23 20:27:39 +08:00
* @return The restitution.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
float getRestitution() const;
2021-12-25 10:04:45 +08:00
/** Set friction.
2019-11-23 20:27:39 +08:00
* @param frict The friction.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
void setFriction(float frict);
2021-12-25 10:04:45 +08:00
/** Get friction.
2019-11-23 20:27:39 +08:00
* @return The friction.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
float getFriction() const;
2021-12-25 10:04:45 +08:00
/** Set rolling friction.
2019-11-23 20:27:39 +08:00
* @param frict The rolling friction.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
void setRollingFriction(float frict);
2021-12-25 10:04:45 +08:00
/** Get rolling friction.
2019-11-23 20:27:39 +08:00
* @return The rolling friction.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
float getRollingFriction() const;
2021-12-25 10:04:45 +08:00
/** Set hit friction.
2019-11-23 20:27:39 +08:00
* @param hitFraction The hit friction.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
void setHitFraction(float hitFraction);
2021-12-25 10:04:45 +08:00
/** Get hit friction.
2019-11-23 20:27:39 +08:00
* @return The hit friction.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
float getHitFraction() const;
2021-12-25 10:04:45 +08:00
/** Set motion threshold, don't do continuous collision detection if the motion (in one step) is less then
* ccdMotionThreshold.
2019-11-23 20:27:39 +08:00
* @param ccdMotionThreshold The motion threshold.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
void setCcdMotionThreshold(float ccdMotionThreshold);
2021-12-25 10:04:45 +08:00
/** Get motion threshold.
2019-11-23 20:27:39 +08:00
* @return The motion threshold.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
float getCcdMotionThreshold() const;
2021-12-25 10:04:45 +08:00
/** Set swept sphere radius.
2019-11-23 20:27:39 +08:00
* @param radius The swept sphere radius.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
void setCcdSweptSphereRadius(float radius);
2021-12-25 10:04:45 +08:00
/** Get swept sphere radius.
2019-11-23 20:27:39 +08:00
* @return The swept sphere radius.
2021-12-25 10:04:45 +08:00
*/
2019-11-23 20:27:39 +08:00
float getCcdSweptSphereRadius() const;
/** override. */
2022-08-08 18:02:17 +08:00
virtual ax::Mat4 getWorldTransform() const;
2019-11-23 20:27:39 +08:00
/** Set a callback when trigger enter. */
2021-12-25 10:04:45 +08:00
std::function<void(Physics3DObject* otherObject)> onTriggerEnter;
2019-11-23 20:27:39 +08:00
/** Set a callback when trigger exit. */
2021-12-25 10:04:45 +08:00
std::function<void(Physics3DObject* otherObject)> onTriggerExit;
2019-11-23 20:27:39 +08:00
Physics3DCollider();
2019-11-23 20:27:39 +08:00
virtual ~Physics3DCollider();
2021-12-25 10:04:45 +08:00
bool init(Physics3DColliderDes* info);
2019-11-23 20:27:39 +08:00
protected:
2021-12-25 10:04:45 +08:00
btGhostObject* _btGhostObject;
Physics3DShape* _physics3DShape;
2019-11-23 20:27:39 +08:00
};
// end of 3d group
/// @}
NS_AX_END
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
# endif // AX_ENABLE_BULLET_INTEGRATION
2019-11-23 20:27:39 +08:00
#endif // defined(AX_ENABLE_3D_PHYSICS)
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
#endif // __PHYSICS_3D_OBJECT_H__