| @@ -30,7 +30,7 @@ public: | |||||
| m_physics.SetShapeToBox(BoxSize); | m_physics.SetShapeToBox(BoxSize); | ||||
| m_physics.SetMass(.0f); | m_physics.SetMass(.0f); | ||||
| m_physics.SetTransform(base_location, base_rotation); | m_physics.SetTransform(base_location, base_rotation); | ||||
| m_physics.InitBodyToRigid(); | |||||
| m_physics.InitBodyToRigid(true); | |||||
| m_physics.AddToSimulation(new_sim); | m_physics.AddToSimulation(new_sim); | ||||
| } | } | ||||
| @@ -164,7 +164,7 @@ protected: | |||||
| private: | private: | ||||
| //Base datas | //Base datas | ||||
| EasyMesh m_mesh; | EasyMesh m_mesh; | ||||
| EasyPhysics m_physics; | |||||
| EasyPhysic m_physics; | |||||
| bool m_ready; | bool m_ready; | ||||
| bool m_should_render; | bool m_should_render; | ||||
| @@ -30,7 +30,7 @@ namespace phys | |||||
| #ifdef HAVE_PHYS_USE_BULLET | #ifdef HAVE_PHYS_USE_BULLET | ||||
| EasyPhysics::EasyPhysics() : | |||||
| EasyPhysic::EasyPhysic() : | |||||
| m_collision_object(NULL), | m_collision_object(NULL), | ||||
| m_rigid_body(NULL), | m_rigid_body(NULL), | ||||
| m_collision_shape(NULL), | m_collision_shape(NULL), | ||||
| @@ -42,7 +42,7 @@ EasyPhysics::EasyPhysics() : | |||||
| { | { | ||||
| } | } | ||||
| EasyPhysics::~EasyPhysics() | |||||
| EasyPhysic::~EasyPhysic() | |||||
| { | { | ||||
| m_rigid_body = NULL; | m_rigid_body = NULL; | ||||
| delete m_collision_object; | delete m_collision_object; | ||||
| @@ -54,7 +54,7 @@ EasyPhysics::~EasyPhysics() | |||||
| //Set Shape functions | //Set Shape functions | ||||
| //-- | //-- | ||||
| void EasyPhysics::SetShapeTo(btCollisionShape* collision_shape) | |||||
| void EasyPhysic::SetShapeTo(btCollisionShape* collision_shape) | |||||
| { | { | ||||
| bool bReinitToRigidBody = false; | bool bReinitToRigidBody = false; | ||||
| if (m_rigid_body) | if (m_rigid_body) | ||||
| @@ -72,60 +72,43 @@ void EasyPhysics::SetShapeTo(btCollisionShape* collision_shape) | |||||
| } | } | ||||
| //Box Shape support | //Box Shape support | ||||
| void EasyPhysics::SetShapeToBox(lol::vec3& box_size) | |||||
| void EasyPhysic::SetShapeToBox(lol::vec3& box_size) | |||||
| { | { | ||||
| vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE; | vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE; | ||||
| SetShapeTo(new btBoxShape(LOL2BT_VEC3(new_box_size))); | SetShapeTo(new btBoxShape(LOL2BT_VEC3(new_box_size))); | ||||
| } | } | ||||
| void EasyPhysics::SetShapeToSphere(float radius) | |||||
| void EasyPhysic::SetShapeToSphere(float radius) | |||||
| { | { | ||||
| SetShapeTo(new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE)); | SetShapeTo(new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE)); | ||||
| } | } | ||||
| void EasyPhysics::SetShapeToCone(float radius, float height) | |||||
| void EasyPhysic::SetShapeToCone(float radius, float height) | |||||
| { | { | ||||
| SetShapeTo(new btConeShape( radius * LOL2BT_UNIT, | SetShapeTo(new btConeShape( radius * LOL2BT_UNIT, | ||||
| height * LOL2BT_UNIT)); | height * LOL2BT_UNIT)); | ||||
| } | } | ||||
| void EasyPhysics::SetShapeToCylinder(lol::vec3& cyl_size) | |||||
| void EasyPhysic::SetShapeToCylinder(lol::vec3& cyl_size) | |||||
| { | { | ||||
| vec3 new_cyl_size = cyl_size * LOL2BT_UNIT; | vec3 new_cyl_size = cyl_size * LOL2BT_UNIT; | ||||
| new_cyl_size.y *= LOL2BT_SIZE; | new_cyl_size.y *= LOL2BT_SIZE; | ||||
| SetShapeTo(new btCylinderShape(LOL2BT_VEC3(new_cyl_size))); | SetShapeTo(new btCylinderShape(LOL2BT_VEC3(new_cyl_size))); | ||||
| } | } | ||||
| void EasyPhysics::SetShapeToCapsule(float radius, float height) | |||||
| void EasyPhysic::SetShapeToCapsule(float radius, float height) | |||||
| { | { | ||||
| SetShapeTo(new btCapsuleShape( radius * LOL2BT_UNIT * LOL2BT_SIZE, | SetShapeTo(new btCapsuleShape( radius * LOL2BT_UNIT * LOL2BT_SIZE, | ||||
| height * LOL2BT_UNIT * LOL2BT_SIZE)); | height * LOL2BT_UNIT * LOL2BT_SIZE)); | ||||
| } | } | ||||
| //------------------------------------------------------------------------- | |||||
| //Bullet collision channel setup | |||||
| //-- | |||||
| void EasyPhysics::CustomSetCollisionChannel(int NewGroup, int NewMask) | |||||
| { | |||||
| if (m_rigid_body) | |||||
| m_rigid_body->setCollisionFlags(m_collision_mask); | |||||
| } | |||||
| //------------------------------------------------------------------------- | //------------------------------------------------------------------------- | ||||
| //Base Location/Rotation setup | //Base Location/Rotation setup | ||||
| //-- | //-- | ||||
| void EasyPhysics::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation) | |||||
| void EasyPhysic::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation) | |||||
| { | { | ||||
| if (m_motion_state) | if (m_motion_state) | ||||
| { | |||||
| if (m_mass != .0f) | |||||
| m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location))); | |||||
| else | |||||
| { | |||||
| m_rigid_body->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location))); | |||||
| m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location))); | |||||
| } | |||||
| } | |||||
| m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location))); | |||||
| else | else | ||||
| m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location))); | m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location))); | ||||
| } | } | ||||
| @@ -134,7 +117,7 @@ void EasyPhysics::SetTransform(const lol::vec3& base_location, const lol::quat& | |||||
| //Mass related functions | //Mass related functions | ||||
| //-- | //-- | ||||
| //Set Shape functions | //Set Shape functions | ||||
| void EasyPhysics::SetMass(float mass) | |||||
| void EasyPhysic::SetMass(float mass) | |||||
| { | { | ||||
| m_mass = mass; | m_mass = mass; | ||||
| @@ -150,7 +133,7 @@ void EasyPhysics::SetMass(float mass) | |||||
| //-- | //-- | ||||
| //Init to rigid body | //Init to rigid body | ||||
| void EasyPhysics::InitBodyToRigid() | |||||
| void EasyPhysic::InitBodyToRigid(bool SetToKinematic) | |||||
| { | { | ||||
| if (m_collision_object) | if (m_collision_object) | ||||
| delete m_collision_object; | delete m_collision_object; | ||||
| @@ -158,12 +141,19 @@ void EasyPhysics::InitBodyToRigid() | |||||
| SetLocalInertia(m_mass); | SetLocalInertia(m_mass); | ||||
| if (!m_motion_state) | if (!m_motion_state) | ||||
| SetTransform(vec3(.0f)); | SetTransform(vec3(.0f)); | ||||
| btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia); | btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia); | ||||
| m_rigid_body = new btRigidBody(NewInfos); | m_rigid_body = new btRigidBody(NewInfos); | ||||
| m_collision_object = m_rigid_body; | m_collision_object = m_rigid_body; | ||||
| if (m_mass == .0f && SetToKinematic) | |||||
| { | |||||
| m_rigid_body->setActivationState(DISABLE_DEACTIVATION); | |||||
| m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); | |||||
| } | |||||
| } | } | ||||
| void EasyPhysics::AddToSimulation(class Simulation* current_simulation) | |||||
| void EasyPhysic::AddToSimulation(class Simulation* current_simulation) | |||||
| { | { | ||||
| btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld(); | btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld(); | ||||
| if (m_rigid_body) | if (m_rigid_body) | ||||
| @@ -182,7 +172,7 @@ void EasyPhysics::AddToSimulation(class Simulation* current_simulation) | |||||
| //Getter functons | //Getter functons | ||||
| //-- | //-- | ||||
| mat4 EasyPhysics::GetTransform() | |||||
| mat4 EasyPhysic::GetTransform() | |||||
| { | { | ||||
| m_local_to_world = lol::mat4(1.0f); | m_local_to_world = lol::mat4(1.0f); | ||||
| if (m_rigid_body && m_motion_state) | if (m_rigid_body && m_motion_state) | ||||
| @@ -197,14 +187,15 @@ mat4 EasyPhysics::GetTransform() | |||||
| } | } | ||||
| //Set Local Inertia | //Set Local Inertia | ||||
| void EasyPhysics::SetLocalInertia(float mass) | |||||
| void EasyPhysic::SetLocalInertia(float mass) | |||||
| { | { | ||||
| if (mass != .0f) | if (mass != .0f) | ||||
| m_collision_shape->calculateLocalInertia(mass, m_local_inertia); | m_collision_shape->calculateLocalInertia(mass, m_local_inertia); | ||||
| else | else | ||||
| m_local_inertia = btVector3(.0f, .0f, .0f); | m_local_inertia = btVector3(.0f, .0f, .0f); | ||||
| } | } | ||||
| #endif | |||||
| #endif // HAVE_PHYS_USE_BULLET | |||||
| } /* namespace phys */ | } /* namespace phys */ | ||||
| @@ -10,7 +10,7 @@ | |||||
| // | // | ||||
| // | // | ||||
| // The EasyPhysics class | |||||
| // The EasyPhysic class | |||||
| // ------------------ | // ------------------ | ||||
| // | // | ||||
| @@ -29,12 +29,14 @@ namespace lol | |||||
| namespace phys | namespace phys | ||||
| { | { | ||||
| class EasyPhysics | |||||
| class EasyPhysic | |||||
| { | { | ||||
| #ifdef HAVE_PHYS_USE_BULLET | #ifdef HAVE_PHYS_USE_BULLET | ||||
| public: | public: | ||||
| EasyPhysics(); | |||||
| ~EasyPhysics(); | |||||
| EasyPhysic(); | |||||
| ~EasyPhysic(); | |||||
| void SetShapeToBox(lol::vec3& box_size); | void SetShapeToBox(lol::vec3& box_size); | ||||
| void SetShapeToSphere(float radius); | void SetShapeToSphere(float radius); | ||||
| @@ -42,10 +44,10 @@ public: | |||||
| void SetShapeToCylinder(lol::vec3& cyl_size); | void SetShapeToCylinder(lol::vec3& cyl_size); | ||||
| void SetShapeToCapsule(float radius, float height); | void SetShapeToCapsule(float radius, float height); | ||||
| void CustomSetCollisionChannel(int NewGroup, int NewMask); | |||||
| bool CanChangeCollisionChannel() { return (m_rigid_body == NULL); } | |||||
| void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))); | void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))); | ||||
| void SetMass(float mass); | void SetMass(float mass); | ||||
| void InitBodyToRigid(); | |||||
| void InitBodyToRigid(bool ZeroMassIsKinematic=false); | |||||
| void AddToSimulation(class Simulation* current_simulation); | void AddToSimulation(class Simulation* current_simulation); | ||||
| mat4 GetTransform(); | mat4 GetTransform(); | ||||
| @@ -60,9 +62,11 @@ protected: | |||||
| btCollisionShape* m_collision_shape; | btCollisionShape* m_collision_shape; | ||||
| btMotionState* m_motion_state; | btMotionState* m_motion_state; | ||||
| #else | |||||
| #else // NO PHYSIC IMPLEMENTATION | |||||
| public: | public: | ||||
| EasyPhysics() { } | |||||
| EasyPhysic() { } | |||||
| void SetShapeToBox(lol::vec3& BoxSize) { } | void SetShapeToBox(lol::vec3& BoxSize) { } | ||||
| void SetShapeToSphere(float radius) { } | void SetShapeToSphere(float radius) { } | ||||
| @@ -70,22 +74,27 @@ public: | |||||
| void SetShapeToCylinder(lol::vec3& cyl_size) { } | void SetShapeToCylinder(lol::vec3& cyl_size) { } | ||||
| void SetShapeToCapsule(float radius, float height) { } | void SetShapeToCapsule(float radius, float height) { } | ||||
| void CustomSetCollisionChannel(int NewGroup, int NewMask) { } | |||||
| bool CanChangeCollisionChannel() { return true; } | |||||
| void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) { } | void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) { } | ||||
| void SetMass(float mass) { } | void SetMass(float mass) { } | ||||
| void InitBodyToRigid() { } | void InitBodyToRigid() { } | ||||
| void AddToSimulation(class Simulation* current_simulation) { } | void AddToSimulation(class Simulation* current_simulation) { } | ||||
| mat4 GetTransform() { return mat4(1.0f); } | mat4 GetTransform() { return mat4(1.0f); } | ||||
| #endif | |||||
| #endif // PHYSIC IMPLEMENTATION | |||||
| public: | public: | ||||
| //Sets the collision Group & Mask. | //Sets the collision Group & Mask. | ||||
| //Mask can change at runtime, not group ! | //Mask can change at runtime, not group ! | ||||
| void SetCollisionChannel(int NewGroup, int NewMask) | |||||
| bool SetCollisionChannel(int NewGroup, int NewMask) | |||||
| { | { | ||||
| m_collision_group = (1<<NewGroup); | |||||
| m_collision_mask = NewMask; | |||||
| CustomSetCollisionChannel(NewGroup, NewMask); | |||||
| if (CanChangeCollisionChannel()) | |||||
| { | |||||
| m_collision_group = (1<<NewGroup); | |||||
| m_collision_mask = NewMask; | |||||
| return true; | |||||
| } | |||||
| return false; | |||||
| } | } | ||||
| int GetCollisionGroup() { return m_collision_group; } | int GetCollisionGroup() { return m_collision_group; } | ||||
| int GetCollisionMask() { return m_collision_mask; } | int GetCollisionMask() { return m_collision_mask; } | ||||
| @@ -97,6 +106,27 @@ protected: | |||||
| int m_collision_mask; | int m_collision_mask; | ||||
| }; | }; | ||||
| class EasyConstraint | |||||
| { | |||||
| EasyConstraint() | |||||
| { | |||||
| //btPoint2PointConstraint(bA, bB, PivotA, PivotB) | |||||
| //btHingeConstraint(bA, bB, TransfA, TransfB, UseRefA) | |||||
| //btSliderConstraint(bA, bB, TransfA, TransfB, UseRefA) | |||||
| //btConeTwistConstraint(bA, bB, TransfA, TransfB) | |||||
| //btGeneric6DofConstraint(bA, bB, TransfA, TransfB, UseRefA) | |||||
| } | |||||
| #ifdef HAVE_PHYS_USE_BULLET | |||||
| btTypedConstraint* m_typed_constraint; | |||||
| #else // NO PHYSIC IMPLEMENTATION | |||||
| #endif // PHYSIC IMPLEMENTATION | |||||
| }; | |||||
| } /* namespace phys */ | } /* namespace phys */ | ||||
| } /* namespace lol */ | } /* namespace lol */ | ||||
| @@ -20,6 +20,8 @@ | |||||
| namespace lol | namespace lol | ||||
| { | { | ||||
| #ifdef HAVE_PHYS_USE_BULLET | |||||
| #define LOL2BT_UNIT 1.0f | #define LOL2BT_UNIT 1.0f | ||||
| #define BT2LOL_UNIT 1.0f | #define BT2LOL_UNIT 1.0f | ||||
| @@ -32,6 +34,8 @@ namespace lol | |||||
| #define LOL2BT_QUAT(ELEMENT) btQuaternion((ELEMENT).x, (ELEMENT).y, (ELEMENT).z, (ELEMENT).w) | #define LOL2BT_QUAT(ELEMENT) btQuaternion((ELEMENT).x, (ELEMENT).y, (ELEMENT).z, (ELEMENT).w) | ||||
| #define BT2LOL_QUAT(ELEMENT) lol::quat((ELEMENT).getW(), BT2LOL_VEC3((ELEMENT).getAxis()) | #define BT2LOL_QUAT(ELEMENT) lol::quat((ELEMENT).getW(), BT2LOL_VEC3((ELEMENT).getAxis()) | ||||
| #endif // HAVE_PHYS_USE_BULLET | |||||
| } /* namespace lol */ | } /* namespace lol */ | ||||
| #endif /* __LOLBTPHYSICSINTEGRATION_H__ */ | #endif /* __LOLBTPHYSICSINTEGRATION_H__ */ | ||||
| @@ -23,8 +23,6 @@ namespace phys | |||||
| class Simulation : public Entity | class Simulation : public Entity | ||||
| { | { | ||||
| friend class EasyPhysics; | |||||
| public: | public: | ||||
| Simulation() : | Simulation() : | ||||
| m_broadphase(0), | m_broadphase(0), | ||||
| @@ -109,7 +107,9 @@ private: | |||||
| btSequentialImpulseConstraintSolver* m_solver; | btSequentialImpulseConstraintSolver* m_solver; | ||||
| // The world. | // The world. | ||||
| btDiscreteDynamicsWorld* m_dynamics_world; | btDiscreteDynamicsWorld* m_dynamics_world; | ||||
| #else | |||||
| #else // NO PHYSIC IMPLEMENTATION | |||||
| public: | public: | ||||
| void Init() { } | void Init() { } | ||||
| void TickGame(float seconds) { } | void TickGame(float seconds) { } | ||||
| @@ -118,7 +118,8 @@ private: | |||||
| void CustomSetContinuousDetection(bool ShouldUseCCD) { } | void CustomSetContinuousDetection(bool ShouldUseCCD) { } | ||||
| void CustomSetGravity(vec3 &NewGravity) { } | void CustomSetGravity(vec3 &NewGravity) { } | ||||
| void CustomSetTimestep(float NewTimestep) { } | void CustomSetTimestep(float NewTimestep) { } | ||||
| #endif //HAVE_PHYS_USE_BULLET | |||||
| #endif // PHYSIC IMPLEMENTATION | |||||
| public: | public: | ||||
| //Main logic : | //Main logic : | ||||
| @@ -150,13 +151,18 @@ public: | |||||
| } | } | ||||
| private: | private: | ||||
| //Adds the given EasyPhysics to the correct list. | |||||
| void AddToDynamic(EasyPhysics* dynamic_EP) { m_dynamic_list << dynamic_EP; } | |||||
| void AddToStatic(EasyPhysics* static_EP) { m_static_list << static_EP; } | |||||
| friend class EasyPhysic; | |||||
| friend class EasyConstraint; | |||||
| //Adds the given EasyPhysic to the correct list. | |||||
| void AddToDynamic(EasyPhysic* NewEPDynamic) { m_dynamic_list << NewEPDynamic; } | |||||
| void AddToStatic(EasyPhysic* NewEPStatic) { m_static_list << NewEPStatic; } | |||||
| void AddToConstraint(EasyConstraint* NewEC) { m_constraint_list << NewEC; } | |||||
| //Easy Physics body List | //Easy Physics body List | ||||
| Array<EasyPhysics*> m_dynamic_list; | |||||
| Array<EasyPhysics*> m_static_list; | |||||
| Array<EasyPhysic*> m_dynamic_list; | |||||
| Array<EasyPhysic*> m_static_list; | |||||
| Array<EasyConstraint*> m_constraint_list; | |||||
| //Easy Physics data storage | //Easy Physics data storage | ||||
| float m_timestep; | float m_timestep; | ||||