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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __PHYSICS_3D_WORLD_H__
|
|
|
|
#define __PHYSICS_3D_WORLD_H__
|
|
|
|
|
|
|
|
#include "math/CCMath.h"
|
|
|
|
#include "base/CCRef.h"
|
|
|
|
#include "base/ccConfig.h"
|
|
|
|
|
|
|
|
#if CC_USE_3D_PHYSICS
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# if (CC_ENABLE_BULLET_INTEGRATION)
|
2015-05-08 15:49:33 +08:00
|
|
|
|
|
|
|
class btDynamicsWorld;
|
|
|
|
class btDefaultCollisionConfiguration;
|
|
|
|
class btCollisionDispatcher;
|
2018-03-13 09:40:24 +08:00
|
|
|
struct btDbvtBroadphase;
|
2015-05-08 15:49:33 +08:00
|
|
|
class btSequentialImpulseConstraintSolver;
|
2015-06-29 10:26:09 +08:00
|
|
|
class btGhostPairCallback;
|
2015-05-08 15:49:33 +08:00
|
|
|
class btRigidBody;
|
|
|
|
class btCollisionObject;
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2015-05-08 15:49:33 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup _3d
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Physics3DObject;
|
|
|
|
class Physics3DConstraint;
|
|
|
|
class Physics3DDebugDrawer;
|
|
|
|
class Physics3DComponent;
|
|
|
|
class Physics3DShape;
|
|
|
|
class Renderer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The description of Physics3DWorld.
|
|
|
|
*/
|
|
|
|
struct CC_DLL Physics3DWorldDes
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isDebugDrawEnabled; // using physics debug draw?, false by default
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::Vec3 gravity; // gravity, (0, -9.8, 0)
|
2015-05-08 15:49:33 +08:00
|
|
|
Physics3DWorldDes()
|
|
|
|
{
|
|
|
|
isDebugDrawEnabled = false;
|
2022-07-11 17:50:21 +08:00
|
|
|
gravity = axis::Vec3(0.f, -9.8f, 0.f);
|
2015-05-08 15:49:33 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* @brief The physics information container, include Physics3DObjects, Physics3DConstraints, collision information and
|
|
|
|
* so on.
|
2015-05-08 15:49:33 +08:00
|
|
|
*/
|
|
|
|
class CC_DLL Physics3DWorld : public Ref
|
|
|
|
{
|
|
|
|
friend class Physics3DComponent;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
public:
|
|
|
|
struct HitResult
|
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::Vec3 hitPosition;
|
|
|
|
axis::Vec3 hitNormal;
|
2015-05-08 15:49:33 +08:00
|
|
|
Physics3DObject* hitObj;
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Creates a Physics3DWorld with Physics3DWorldDes.
|
2015-05-08 15:49:33 +08:00
|
|
|
*
|
|
|
|
* @return An autoreleased Physics3DWorld object.
|
|
|
|
*/
|
|
|
|
static Physics3DWorld* create(Physics3DWorldDes* info);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-27 18:07:31 +08:00
|
|
|
/** set gravity for the physics world */
|
|
|
|
void setGravity(const Vec3& gravity);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-27 18:07:31 +08:00
|
|
|
/** get current gravity */
|
|
|
|
Vec3 getGravity() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
/** Add a Physics3DObject. */
|
|
|
|
void addPhysics3DObject(Physics3DObject* physicsObj);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
/** Remove a Physics3DObject. */
|
|
|
|
void removePhysics3DObject(Physics3DObject* physicsObj);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
/** Remove all Physics3DObjects. */
|
|
|
|
void removeAllPhysics3DObjects();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
/** Add a Physics3DConstraint. */
|
|
|
|
void addPhysics3DConstraint(Physics3DConstraint* constraint, bool disableCollisionsBetweenLinkedObjs = true);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
/** Remove a Physics3DConstraint. */
|
|
|
|
void removePhysics3DConstraint(Physics3DConstraint* constraint);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
/** Remove all Physics3DConstraint. */
|
|
|
|
void removeAllPhysics3DConstraints();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
/** Simulate one frame. */
|
|
|
|
void stepSimulate(float dt);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
/** Enable or disable debug drawing. */
|
|
|
|
void setDebugDrawEnable(bool enableDebugDraw);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
/** Check debug drawing is enabled. */
|
|
|
|
bool isDebugDrawEnabled() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
/** Internal method, the updater of debug drawing, need called each frame. */
|
2022-07-11 17:50:21 +08:00
|
|
|
void debugDraw(axis::Renderer* renderer);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
/** Get the list of Physics3DObjects. */
|
|
|
|
const std::vector<Physics3DObject*>& getPhysicsObjects() const { return _objects; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
/**
|
|
|
|
* Ray cast method
|
|
|
|
* @param startPos The start position of ray.
|
|
|
|
* @param endPos The end position of ray.
|
|
|
|
* @param result the result of ray cast.
|
|
|
|
*/
|
2022-07-11 17:50:21 +08:00
|
|
|
bool rayCast(const axis::Vec3& startPos, const axis::Vec3& endPos, HitResult* result);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
/** Performs a swept shape cast on all objects in the Physics3DWorld. */
|
2021-12-25 10:04:45 +08:00
|
|
|
bool 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
|
|
|
HitResult* result);
|
|
|
|
|
2022-03-18 21:46:07 +08:00
|
|
|
Physics3DWorld();
|
2015-05-08 15:49:33 +08:00
|
|
|
virtual ~Physics3DWorld();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
bool init(Physics3DWorldDes* info);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
Physics3DObject* getPhysicsObject(const btCollisionObject* btObj);
|
|
|
|
|
|
|
|
void collisionChecking();
|
|
|
|
bool needCollisionChecking();
|
2015-06-29 10:26:09 +08:00
|
|
|
void setGhostPairCallback();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
protected:
|
2020-11-15 00:48:53 +08:00
|
|
|
void removePhysics3DConstraintFromBullet(Physics3DConstraint* constraint);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::vector<Physics3DObject*> _objects;
|
|
|
|
std::vector<Physics3DConstraint*> _constraints;
|
|
|
|
std::vector<Physics3DComponent*> _physicsComponents; // physics3d components
|
2015-05-08 15:49:33 +08:00
|
|
|
bool _needCollisionChecking;
|
|
|
|
bool _collisionCheckingFlag;
|
2015-06-29 10:26:09 +08:00
|
|
|
bool _needGhostPairCallbackChecking;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
# if (CC_ENABLE_BULLET_INTEGRATION)
|
2015-05-08 15:49:33 +08:00
|
|
|
btDynamicsWorld* _btPhyiscsWorld;
|
|
|
|
btDefaultCollisionConfiguration* _collisionConfiguration;
|
|
|
|
btCollisionDispatcher* _dispatcher;
|
|
|
|
btDbvtBroadphase* _broadphase;
|
|
|
|
btSequentialImpulseConstraintSolver* _solver;
|
2021-12-25 10:04:45 +08:00
|
|
|
btGhostPairCallback* _ghostCallback;
|
|
|
|
Physics3DDebugDrawer* _debugDrawer;
|
|
|
|
# endif // CC_ENABLE_BULLET_INTEGRATION
|
2015-05-08 15:49:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// end of 3d group
|
|
|
|
/// @}
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // CC_USE_3D_PHYSICS
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // __PHYSICS_3D_WORLD_H__
|