123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574 |
- #ifndef __CCPHYSICS_BODY_H__
- #define __CCPHYSICS_BODY_H__
- #include "base/ccConfig.h"
- #if CC_USE_PHYSICS
- #include "2d/CCComponent.h"
- #include "math/CCGeometry.h"
- #include "physics/CCPhysicsShape.h"
- #include "base/CCVector.h"
- struct cpBody;
- NS_CC_BEGIN
- class Node;
- class PhysicsWorld;
- class PhysicsJoint;
- typedef Vec2 Vect;
- const PhysicsMaterial PHYSICSBODY_MATERIAL_DEFAULT(0.1f, 0.5f, 0.5f);
- class CC_DLL PhysicsBody : public Component
- {
- public:
- const static std::string COMPONENT_NAME;
-
- static PhysicsBody* create();
-
-
- static PhysicsBody* create(float mass);
-
-
- static PhysicsBody* create(float mass, float moment);
-
-
- static PhysicsBody* createCircle(float radius, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Vec2& offset = Vec2::ZERO);
-
- static PhysicsBody* createBox(const Size& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Vec2& offset = Vec2::ZERO);
-
- static PhysicsBody* createPolygon(const Vec2* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Vec2& offset = Vec2::ZERO);
-
- static PhysicsBody* createEdgeSegment(const Vec2& a, const Vec2& b, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
-
- static PhysicsBody* createEdgeBox(const Size& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1, const Vec2& offset = Vec2::ZERO);
-
- static PhysicsBody* createEdgePolygon(const Vec2* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
-
- static PhysicsBody* createEdgeChain(const Vec2* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
-
- virtual PhysicsShape* addShape(PhysicsShape* shape, bool addMassAndMoment = true);
-
- void removeShape(PhysicsShape* shape, bool reduceMassAndMoment = true);
-
- void removeShape(int tag, bool reduceMassAndMoment = true);
-
-
- void removeAllShapes(bool reduceMassAndMoment = true);
-
- const Vector<PhysicsShape*>& getShapes() const { return _shapes; }
-
- PhysicsShape* getFirstShape() const { return _shapes.size() >= 1 ? _shapes.at(0) : nullptr; }
-
- PhysicsShape* getShape(int tag) const;
-
-
- virtual void applyForce(const Vec2& force, const Vec2& offset = Vec2::ZERO);
-
- virtual void resetForces();
-
- virtual void applyImpulse(const Vec2& impulse, const Vec2& offset = Vec2::ZERO);
-
-
- virtual void applyTorque(float torque);
-
-
- virtual void setVelocity(const Vec2& velocity);
-
-
- virtual Vec2 getVelocity();
-
-
- virtual void setAngularVelocity(float velocity);
-
-
- virtual Vec2 getVelocityAtLocalPoint(const Vec2& point);
-
-
- virtual Vec2 getVelocityAtWorldPoint(const Vec2& point);
-
-
- virtual float getAngularVelocity();
-
-
- virtual void setVelocityLimit(float limit);
-
-
- virtual float getVelocityLimit();
-
-
- virtual void setAngularVelocityLimit(float limit);
-
-
- virtual float getAngularVelocityLimit();
-
-
- void removeFromWorld();
-
-
- PhysicsWorld* getWorld() const { return _world; }
-
- const std::vector<PhysicsJoint*>& getJoints() const { return _joints; }
-
-
- Node* getNode() const { return _owner; }
-
-
- void setCategoryBitmask(int bitmask);
-
-
- void setContactTestBitmask(int bitmask);
-
-
- void setCollisionBitmask(int bitmask);
-
-
- int getCategoryBitmask() const;
-
-
- int getContactTestBitmask() const;
-
-
- int getCollisionBitmask() const;
-
-
- void setGroup(int group);
-
-
- int getGroup() const;
-
-
- Vec2 getPosition() const;
-
- float getRotation();
-
- void setPositionOffset(const Vec2& position);
-
- const Vec2& getPositionOffset() const { return _positionOffset; }
-
-
- void setRotationOffset(float rotation);
-
-
- float getRotationOffset() const { return _rotationOffset; }
-
-
- bool isDynamic() const { return _dynamic; }
-
- void setDynamic(bool dynamic);
-
-
- void setMass(float mass);
-
-
- float getMass() const { return _mass; }
-
- void addMass(float mass);
-
-
- void setMoment(float moment);
-
-
- float getMoment() const { return _moment; }
-
-
- void addMoment(float moment);
-
-
- float getLinearDamping() const { return _linearDamping; }
-
-
- void setLinearDamping(float damping) { _linearDamping = damping; updateDamping(); }
-
-
- float getAngularDamping() const { return _angularDamping; }
-
- void setAngularDamping(float damping) { _angularDamping = damping; updateDamping(); }
-
-
- bool isResting() const;
-
-
- void setResting(bool rest) const;
-
-
- virtual void setEnabled(bool enable) override;
-
-
- bool isRotationEnabled() const { return _rotationEnabled; }
-
-
- void setRotationEnable(bool enable);
-
-
- bool isGravityEnabled() const { return _gravityEnabled; }
-
-
- void setGravityEnable(bool enable);
-
-
- int getTag() const { return _tag; }
-
-
- void setTag(int tag) { _tag = tag; }
-
-
- Vec2 world2Local(const Vec2& point);
-
-
- Vec2 local2World(const Vec2& point);
-
- cpBody* getCPBody() const { return _cpBody; }
- virtual void onEnter() override;
- virtual void onExit() override;
- virtual void onAdd() override;
- virtual void onRemove() override;
-
- protected:
- PhysicsBody();
- virtual ~PhysicsBody();
- virtual bool init()override;
-
- virtual void setPosition(float positionX, float positionY);
- virtual void setRotation(float rotation);
- virtual void setScale(float scaleX, float scaleY);
-
- void update(float delta)override;
-
- void removeJoint(PhysicsJoint* joint);
- void updateDamping() { _isDamping = _linearDamping != 0.0f || _angularDamping != 0.0f; }
- void addToPhysicsWorld();
- void removeFromPhysicsWorld();
- void beforeSimulation(const Mat4& parentToWorldTransform, const Mat4& nodeToWorldTransform, float scaleX, float scaleY, float rotation);
- void afterSimulation(const Mat4& parentToWorldTransform, float parentRotation);
- protected:
- std::vector<PhysicsJoint*> _joints;
- Vector<PhysicsShape*> _shapes;
- PhysicsWorld* _world;
-
- cpBody* _cpBody;
- bool _dynamic;
- bool _rotationEnabled;
- bool _gravityEnabled;
- bool _massDefault;
- bool _momentDefault;
- float _mass;
- float _area;
- float _density;
- float _moment;
- float _velocityLimit;
- float _angularVelocityLimit;
- bool _isDamping;
- float _linearDamping;
- float _angularDamping;
- int _tag;
-
-
- bool _massSetByUser;
-
- bool _momentSetByUser;
-
- Vec2 _positionOffset;
- float _rotationOffset;
- float _recordedRotation;
- double _recordedAngle;
-
-
- Vec3 _ownerCenterOffset;
-
- Vec2 _offset;
- float _recordScaleX;
- float _recordScaleY;
- float _recordPosX;
- float _recordPosY;
- friend class PhysicsWorld;
- friend class PhysicsShape;
- friend class PhysicsJoint;
- };
- NS_CC_END
- #endif
- #endif
|