本文来自http://blog.csdn.net/runaying ,引用必须注明出处!
温馨提醒:为了大家能更好学习,强烈推荐大家看看本人的这篇博客 Cocos2d-X权威指南笔记
物体之间的作用力,把 shape 链接到 body
///cocos2d-x-3.0alpha0/cocos2dx/physics //物体之间的作用力,把 shape 链接到 body #include "CCPhysicsSetting.h" #ifdef CC_USE_PHYSICS #ifndef __CCPHYSICS_BODY_H__ #define __CCPHYSICS_BODY_H__ #include "cocoa/CCObject.h" #include "cocoa/CCGeometry.h" #include <vector> NS_CC_BEGIN class Sprite; class PhysicsWorld; class PhysicsJoint; class PhysicsShape; class PhysicsShapeCircle; class PhysicsShapeBox; class PhysicsShapePolygon; class PhysicsShapeEdgeSegment; class PhysicsShapeEdgeBox; class PhysicsShapeEdgePolygon; class PhysicsShapeEdgeChain; class PhysicsBodyInfo; /** * physics 影响 body. * 它可以和一个或多个形状产生联系. */ class PhysicsBody : public Object//, public Clonable(可克隆的) { public: /** * @brief 创建一个包含 圆形的 body */ static PhysicsBody* createCircle(float radius); /** * @brief 创建一个包含 box形状的 body */ static PhysicsBody* createBox(Size size); /** * @brief 创建一个包含 polygon(多变形)的 body * points 是定义的点结构数组,沿顺时针缠绕的凸球面 */ static PhysicsBody* createPolygon(Point* points, int count); /** * @brief 创建一个包含 EdgeSegment(边缘片段)形状的 body */ static PhysicsBody* createEdgeSegment(Point a, Point b, float border = 1); /** * @brief 创建一个包含 EdgeBox(边缘方块)形状的 body */ static PhysicsBody* createEdgeBox(Size size, float border = 1); /** * @brief 创建一个包含 EdgePolygon(边缘多边形)形状的 body . */ static PhysicsBody* createEdgePolygon(Point* points, int count, float border = 1); /** * @brief 创建一个包含 EdgeChain(边链)形状的 body. */ static PhysicsBody* createEdgeChain(Point* points, int count, float border = 1); /** * @brief 把一个圆的形状连接到 body */ virtual PhysicsShapeCircle* addCircle(float radius, Point offset = Point(0, 0)); /** * @brief 把一个box的形状连接到 body */ virtual PhysicsShapeBox* addBox(Size size, Point offset = Point(0, 0)); /** * @brief 把一个多边形的形状连接到 body */ virtual PhysicsShapePolygon* addPolygon(Point* points, int count, Point offset = Point(0, 0)); /** * @brief 把一个边缘部分的形状连接到 body */ virtual PhysicsShapeEdgeSegment* addEdgeSegment(Point a, Point b, float border = 1); /** * @brief 把一个边缘框形状连接到 body */ virtual PhysicsShapeEdgeBox* addEdgeBox(Size size, float border = 1, Point offset = Point(0, 0)); /** * @brief 把一个边缘多边形的形状连接到 body * points 是定义的点结构数组,沿顺时针缠绕的凸球面. */ virtual PhysicsShapeEdgePolygon* addEdgePolygon(Point* points, int count, float border = 1); /** * @brief 附加一个边缘链形状到 body * points 是定义的点结构数组,沿顺时针缠绕的凸球面. */ virtual PhysicsShapeEdgeChain* addEdgeChain(Point* points, int count, float border = 1); /** * @brief 应用一个直接的力到 body. */ virtual void applyForce(Point force); /** * @brief 应用一个直接的力到 body. */ virtual void applyForce(Point force, Point offset); /** * @brief 应用一个连续的力到 body. */ virtual void applyImpulse(Point impulse); /** * @brief 应用一个连续的力到 body. */ virtual void applyImpulse(Point impulse, Point offset); /** * @brief 把一个力矩应用到 body. */ virtual void applyTorque(float torque); /* * @brief get the body shapes. */ inline std::vector<PhysicsShape*>& getShapes() { return _shapes; } /* * @brief get the first body shapes. */ inline PhysicsShape* getShape() { return _shapes.size() >= 1 ? _shapes.front() : nullptr; } /* * @brief remove a shape from body */ void removeShape(PhysicsShape* shape); /* * @brief remove all shapes */ void removeAllShapes(); /* * @brief get 添加 body 的 world. */ inline PhysicsWorld* getWorld() const { return _world; } /* * @brief 获取 body 的所有joints(接触点) */ inline const std::vector<PhysicsJoint*>* getJoints() const { return &_joints; } /* * @brief get sprite 为 aprite 设置的 body. */ inline Sprite* getOwner() const { return _owner; } void setCategoryBitmask(int bitmask); inline int getCategoryBitmask() const { return _categoryBitmask; } void setContactTestBitmask(int bitmask); inline int getContactTestBitmask() const { return _contactTestBitmask; } void setCollisionBitmask(int bitmask); inline int getCollisionBitmask() const { return _collisionBitmask; } /* * @brief get the body position. //位置 */ Point getPosition() const; /* * @brief get the body rotation. //旋转 */ float getRotation() const; /* * @brief 测试 body 是不是动态的 //body 是不是正在运动 * 动态会影响 body 重力. */ inline bool isDynamic() { return _dynamic; } /* * @brief set body 的动态. * 动态会影响 body 重力. */ void setDynamic(bool dynamic); /* * @brief set the body mass(质量). */ void setMass(float mass); /* * @brief get the body mass(质量). */ inline float getMass() { return _mass; } /* * @brief 角速度 damping(阻尼系数) */ void setAngularDamping(float angularDamping); /* * @brief get 角速度 damping(阻尼系数). */ inline float getAngularDamping() { return _angularDamping; } //virtual Clonable* clone() const override; protected: bool init(); bool initStatic(); virtual void setPosition(Point position); virtual void setRotation(float rotation); virtual void addShape(PhysicsShape* shape); protected: PhysicsBody(); virtual ~PhysicsBody(); protected: Sprite* _owner; std::vector<PhysicsJoint*> _joints; std::vector<PhysicsShape*> _shapes; PhysicsWorld* _world; PhysicsBodyInfo* _info; bool _dynamic; bool _massDefault; bool _angularDampingDefault; float _mass; float _area; float _density; float _angularDamping; int _categoryBitmask; int _contactTestBitmask; int _collisionBitmask; friend class PhysicsWorld; friend class PhysicsShape; friend class PhysicsJoint; friend class Sprite; }; NS_CC_END #endif // __CCPHYSICS_BODY_H__ #endif // CC_USE_PHYSICS