|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- //
- // Lol Engine
- //
- // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
- // (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
- // This program is free software; you can redistribute it and/or
- // modify it under the terms of the Do What The Fuck You Want To
- // Public License, Version 2, as published by Sam Hocevar. See
- // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
- //
-
- //
- // The EasyPhysic class
- // ------------------
- //
-
- #if !defined __EASYPHYSICS_EASYPHYSICS_H__
- #define __EASYPHYSICS_EASYPHYSICS_H__
-
- #ifdef HAVE_PHYS_USE_BULLET
- #include "core.h"
- #include <bullet/btBulletDynamicsCommon.h>
- #include <bullet/btBulletCollisionCommon.h>
- #include <bullet/BulletCollision/CollisionDispatch/btGhostObject.h>
- #include <BulletDynamics/Character/btKinematicCharacterController.h>
- #endif
-
- namespace lol
- {
-
- namespace phys
- {
-
- class EasyPhysic
- {
-
- friend class EasyConstraint;
-
- #ifdef HAVE_PHYS_USE_BULLET
-
- public:
- EasyPhysic();
- ~EasyPhysic();
-
- virtual void SetShapeToBox(lol::vec3& box_size);
- virtual void SetShapeToSphere(float radius);
- virtual void SetShapeToCone(float radius, float height);
- virtual void SetShapeToCylinder(lol::vec3& cyl_size);
- virtual void SetShapeToCapsule(float radius, float height);
-
- virtual bool CanChangeCollisionChannel() { return (m_rigid_body == NULL); }
- virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)));
- virtual void SetMass(float mass);
- virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false);
- virtual void InitBodyToGhost();
- virtual void AddToSimulation(class Simulation* current_simulation);
- virtual void RemoveFromSimulation(class Simulation* current_simulation);
- virtual mat4 GetTransform();
-
- protected:
- virtual void SetLocalInertia(float mass);
- virtual void SetShapeTo(btCollisionShape* collision_shape);
-
- virtual btGhostObject* GetGhostObject();
-
- btCollisionObject* m_collision_object;
-
- btGhostObject* m_ghost_object;
-
- btRigidBody* m_rigid_body;
- btVector3 m_local_inertia;
-
- btCollisionShape* m_collision_shape;
- btConvexShape* m_convex_shape;
- btMotionState* m_motion_state;
-
- #else // NO PHYSIC IMPLEMENTATION
-
- public:
- EasyPhysic() { }
-
- virtual void SetShapeToBox(lol::vec3& BoxSize) { }
- virtual void SetShapeToSphere(float radius) { }
- virtual void SetShapeToCone(float radius, float height) { }
- virtual void SetShapeToCylinder(lol::vec3& cyl_size) { }
- virtual void SetShapeToCapsule(float radius, float height) { }
-
- virtual bool CanChangeCollisionChannel() { return true; }
- virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) { }
- virtual void SetMass(float mass) { }
- virtual void InitBodyToRigid() { }
- virtual void InitBodyToGhost() { }
- virtual void AddToSimulation(class Simulation* current_simulation) { }
- virtual void RemoveFromSimulation(class Simulation* current_simulation) { }
- virtual mat4 GetTransform() { return mat4(1.0f); }
-
- virtual void InitBodyToGhost() { }
-
- #endif // PHYSIC IMPLEMENTATION
-
- public:
- //Sets the collision Group & Mask.
- //Mask can change at runtime, not group !
- virtual bool SetCollisionChannel(int NewGroup, int NewMask)
- {
- if (CanChangeCollisionChannel())
- {
- m_collision_group = (1<<NewGroup);
- m_collision_mask = NewMask;
- return true;
- }
- return false;
- }
- int GetCollisionGroup() { return m_collision_group; }
- int GetCollisionMask() { return m_collision_mask; }
-
- protected:
- lol::mat4 m_local_to_world;
- float m_mass;
- int m_collision_group;
- int m_collision_mask;
- };
-
- class EasyCharacterController : public EasyPhysic
- {
-
- #ifdef HAVE_PHYS_USE_BULLET
-
- public:
- EasyCharacterController() :
- EasyPhysic(),
- m_character(NULL)
- {
- m_up_axis = 1;
- }
- ~EasyCharacterController()
- {
- delete m_character;
- }
-
- virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false);
- virtual void InitBodyToGhost();
- virtual void AddToSimulation(class Simulation* current_simulation);
- virtual void RemoveFromSimulation(class Simulation* current_simulation);
- virtual void SetMovementForFrame(vec3 const &MoveQuantity);
-
- protected:
-
- virtual btGhostObject* GetGhostObject();
-
- btPairCachingGhostObject* m_pair_caching_object;
- btKinematicCharacterController* m_character;
-
- float m_step_height;
- int m_up_axis;
-
- #else // NO PHYSIC IMPLEMENTATION
-
- virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false) { }
- virtual void InitBodyToGhost() { }
- virtual void AddToSimulation(class Simulation* current_simulation) { }
- virtual void RemoveFromSimulation(class Simulation* current_simulation) { }
- virtual void SetMovementForFrame(vec3 const &MoveQuantity) { }
-
- #endif // PHYSIC IMPLEMENTATION
-
- };
-
- class EasyConstraint
- {
- #ifdef HAVE_PHYS_USE_BULLET
-
- public:
- EasyConstraint() :
- m_typed_constraint(NULL),
- m_p2p_constraint(NULL),
- m_hinge_constraint(NULL),
- m_slider_constraint(NULL),
- m_cone_twist_constraint(NULL),
- m_6dof_constraint(NULL),
- m_a_physobj(NULL),
- m_b_physobj(NULL),
- m_a_transform(lol::mat4(1.f)),
- m_b_transform(lol::mat4(1.f)),
- m_using_ref_a(false),
- m_disable_a2b_collision(false)
-
- {
- }
- ~EasyConstraint()
- {
- delete m_typed_constraint;
- m_p2p_constraint = NULL;
- m_hinge_constraint = NULL;
- m_slider_constraint = NULL;
- m_cone_twist_constraint = NULL;
- m_6dof_constraint = NULL;
- }
-
- void AddToSimulation(class Simulation* current_simulation);
- void RemoveFromSimulation(class Simulation* current_simulation);
-
- private:
-
- //check if Init can be done
- bool CanProceedWithInit()
- {
- if (!m_a_physobj || !m_b_physobj)
- return false;
-
- if (!m_a_physobj->m_rigid_body || !m_b_physobj->m_rigid_body)
- return false;
-
- return true;
- }
-
- //-------------------------------------------------------------------------
- //Init constraint functions
- //--
- void CustomInitConstraintToPoint2Point()
- {
- m_p2p_constraint = new btPoint2PointConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
- LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT));
- m_typed_constraint = m_p2p_constraint;
- }
-
- void CustomInitConstraintToHinge()
- {
- m_hinge_constraint = new btHingeConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
- btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
- btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
- m_using_ref_a);
- m_typed_constraint = m_hinge_constraint;
- }
-
- void CustomInitConstraintToSlider()
- {
- m_slider_constraint = new btSliderConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
- btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
- btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
- m_using_ref_a);
- m_typed_constraint = m_slider_constraint;
- }
-
- void CustomInitConstraintToConeTwist()
- {
- m_cone_twist_constraint = new btConeTwistConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
- btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
- btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)));
- m_typed_constraint = m_cone_twist_constraint;
- }
-
- void CustomInitConstraintTo6Dof()
- {
- m_6dof_constraint = new btGeneric6DofConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
- btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
- btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
- m_using_ref_a);
- m_typed_constraint = m_6dof_constraint;
- }
-
- btTypedConstraint* m_typed_constraint;
- btPoint2PointConstraint* m_p2p_constraint;
- btHingeConstraint* m_hinge_constraint;
- btSliderConstraint* m_slider_constraint;
- btConeTwistConstraint* m_cone_twist_constraint;
- btGeneric6DofConstraint* m_6dof_constraint;
-
- #else // NO PHYSIC IMPLEMENTATION
-
- public:
- EasyConstraint() :
- m_a_physobj(NULL),
- m_b_physobj(NULL),
- m_a_transform(lol::mat4(1.f)),
- m_b_transform(lol::mat4(1.f)),
- m_using_ref_a(false),
- m_disable_a2b_collision(false)
- {
- }
-
- private:
-
- void AddToSimulation(class Simulation* current_simulation) { }
- void RemoveFromSimulation(class Simulation* current_simulation) { }
-
- //check if Init can be done
- bool CanProceedWithInit() { return false; }
- void CustomInitConstraintToPoint2Point() { }
- void CustomInitConstraintToHinge() { }
- void CustomInitConstraintToSlider() { }
- void CustomInitConstraintToConeTwist() { }
- void CustomInitConstraintTo6Dof() { }
-
- #endif // PHYSIC IMPLEMENTATION
-
- public:
- void InitConstraintToPoint2Point() { if (CanProceedWithInit()) CustomInitConstraintToPoint2Point(); }
- void InitConstraintToHinge() { if (CanProceedWithInit()) CustomInitConstraintToHinge(); }
- void InitConstraintToSlider() { if (CanProceedWithInit()) CustomInitConstraintToSlider(); }
- void InitConstraintToConeTwist() { if (CanProceedWithInit()) CustomInitConstraintToConeTwist(); }
- void InitConstraintTo6Dof() { if (CanProceedWithInit()) CustomInitConstraintTo6Dof(); }
-
- //Set given physic object to the proper slot.
- void SetPhysObjA(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(false, NewPhysObj, NewTransform); }
- void SetPhysObjB(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(true, NewPhysObj, NewTransform); }
- void SetPhysObj(bool SetToB, EasyPhysic* NewPhysObj, lol::mat4 NewTransform)
- {
- if (SetToB)
- {
- m_b_physobj = NewPhysObj;
- m_b_transform = NewTransform;
- }
- else
- {
- m_a_physobj = NewPhysObj;
- m_a_transform = NewTransform;
- }
- }
-
- //Set whether or not the physic engine should use the A object as the reference (most constraint transform are local).
- void SetRefAsA(bool NewUseRefA)
- {
- m_using_ref_a = NewUseRefA;
- }
-
- //Set whether or not to disable the collision between the bodies
- void DisableCollisionBetweenObjs(bool DisableCollision)
- {
- m_disable_a2b_collision = DisableCollision;
- }
-
- private:
- EasyPhysic* m_a_physobj;
- EasyPhysic* m_b_physobj;
- lol::mat4 m_a_transform;
- lol::mat4 m_b_transform;
- bool m_using_ref_a;
- bool m_disable_a2b_collision;
-
- };
-
- } /* namespace phys */
-
- } /* namespace lol */
-
- #endif /* __EASYPHYSICS_EASYPHYSICS_H__ */
-
|