00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _COMPLEXPHYSICALCOMPONENT_
00019 #define _COMPLEXPHYSICALCOMPONENT_
00020
00021 #include <iostream>
00022 #include <map>
00023
00024 #include "contrib/box2d/Include/box2d.h"
00025
00026 #include "game/CollisionHullFactory.h"
00027 #include "game/ModelCollisionHull.h"
00028 #include "game/PhysicalComponent.h"
00029 #include "game/Physics.h"
00030
00031 #include "util/Vector2.h"
00032
00033 class BaseCollisionHull;
00034
00035
00036 struct JointDefs
00037 {
00038 DistanceJoint _distanceDef;
00039 RevoluteJoint _revoluteDef;
00040 PrismaticJoint _prismaticDef;
00041 PulleyJoint _pulleyDef;
00042 GearJoint _gearDef;
00043 };
00044
00045 struct Constraint
00046 {
00047 b2Joint* _pJoint;
00048 JointDefs _defs;
00049 std::string _body1;
00050 std::string _body2;
00051 };
00052
00053 std::ostream & operator<<(std::ostream & os, const Constraint & constraint);
00054
00055 typedef std::map<std::string, Constraint> ConstraintMap;
00056
00057 struct BodyPart
00058 {
00059 b2Body* _pBody;
00060 BaseCollisionHullPtr _pCollisionHull;
00061 ConstraintMap _constraints;
00062 std::string _name;
00063 b2BodyDef _bodyDef;
00064 };
00065
00066 std::ostream & operator<<(std::ostream & os, const BodyPart & bp);
00067
00068 typedef std::map<std::string, BodyPart> BodyPartMap;
00069
00070 class ComplexPhysicalComponent : public PhysicalComponent
00071 {
00072 private:
00073 BodyPartMap m_bodyParts;
00074 ConstraintMap m_constraints;
00075 std::string m_mainPart;
00076 bool m_bLocalCollisions;
00077 public:
00078 DEFAULT_COMPONENT_CONSTRUCTOR(PhysicalComponent, ComplexPhysicalComponent, INIT1(m_bLocalCollisions, false) , ,);
00079
00080 virtual ~ComplexPhysicalComponent();
00081
00082 virtual void Update(const int nDelta);
00083 virtual void ToStream(std::ostream & stream) const;
00084 virtual void FromStream(std::istream & stream);
00085
00086 virtual bool FromAttribute(const Attribute* pRoot);
00087
00088 const std::string & GetMainPart() const { return m_mainPart; }
00089
00090 template<class T>
00091 boost::shared_ptr<T> CreateCollisionHull(const std::string & type);
00092
00093 void ConstructBodyPart(const std::string & name, const Vector2 & position, float fRotation, float fMass, float fLinearDamping, float fAngularDamping, bool bFixRotation, bool bBullet, bool bIsMain, BaseCollisionHullPtr pCollisionHull);
00094
00095 void TurnOnLocalCollisions();
00096 void TurnOffLocalCollisions();
00097
00098 bool GetBodyPart(const std::string& name, BodyPart** pBodyPartOut);
00099 bool RemoveBodyPart(const std::string& name);
00100
00101 bool ConstructDistanceConstraint(const std::string & name, const std::string& body1, const std::string& body2, const DistanceJoint& jointInfo);
00102 bool ConstructRevoluteConstraint(const std::string & name, const std::string& body1, const std::string& body2, const RevoluteJoint& jointInfo);
00103 bool ConstructPrismaticConstraint(const std::string & name, const std::string& body1, const std::string& body2, const PrismaticJoint& jointInfo);
00104 bool ConstructPulleyConstraint(const std::string & name, const std::string& body1, const std::string& body2, const PulleyJoint& jointInfo);
00105 bool ConstructGearConstraint(const std::string & name, const std::string& body1, const std::string& body2, const GearJoint& jointInfo);
00106
00107 bool GetConstraint(const std::string& name, Constraint** ppConstraintOut);
00108 bool RemoveConstraint(const std::string& name);
00109 };
00110
00111 template<class T>
00112 boost::shared_ptr<T> ComplexPhysicalComponent::CreateCollisionHull(const std::string & type)
00113 {
00114 T* pHull = CollisionHullFactoryCreate<T>(type);
00115
00116 if(pHull)
00117 {
00118 pHull->SetResourceCache(GET_RESOURCE_CACHE);
00119 return boost::shared_ptr<T>(pHull);
00120 }
00121
00122 return boost::shared_ptr<T>();
00123 }
00124
00125 REGISTER_COMPONENT(ComplexPhysicalComponent, "component_complexphysics", "physics");
00126
00127 #endif