1. 程式人生 > >cocos2d-x 3.0開發筆記---物理引擎封裝 Physics深入學習

cocos2d-x 3.0開發筆記---物理引擎封裝 Physics深入學習

 /** 建立一個body  mass和moment為預設值  */
    static PhysicsBody* create();
    /** 建立一個質量為mass的body moment為預設值. */
    static PhysicsBody* create(float mass);
    /** 建立一個body 併為mass 和moment賦值 */
    static PhysicsBody* create(float mass, float moment);
    /**建立一個shape為圓形的body */
    static PhysicsBody* createCircle(float radius, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
    /** 建立一個shape為四邊形的body. */
    static PhysicsBody* createBox(const Size& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
    /**建立一個動態多邊形剛體,多邊形的頂點存放在Point array[ ]中  示例:Point array[ ]={ point(1,1),point(2,2)}  注意:頂點必須按順時針存放,並且圖形為凸狀,不能是凹的*/
    static PhysicsBody* createPolygon(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
    
    /** 建立一個靜態的線狀剛體. */
    static PhysicsBody* createEdgeSegment(const Point& a, const Point& 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 Point& offset = Point::ZERO);
    /** 建立一個靜態多邊形剛體. */
    static PhysicsBody* createEdgePolygon(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
    /** 建立一個鏈條狀剛體 */
    static PhysicsBody* createEdgeChain(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
    
    /*
   新增一個shape  mass和moment賦值true
     */
    virtual PhysicsShape* addShape(PhysicsShape* shape, bool addMassAndMoment = true);
    /*通過shape移除shape*/
    void removeShape(PhysicsShape* shape, bool reduceMassAndMoment = true);
    /*通過tag移除shape*/
    void removeShape(int tag, bool reduceMassAndMoment = true);
    /* 移除body的所有shape */
    void removeAllShapes(bool reduceMassAndMoment = true);
    /* 獲取body的shapes */
    inline const Vector<PhysicsShape*>& getShapes() const { return _shapes; }
    /* 獲取第一個shape. */
    inline PhysicsShape* getFirstShape() const { return _shapes.size() >= 1 ? _shapes.at(0) : nullptr; }
    /* 通過tag從body中獲取shape */
    PhysicsShape* getShape(int tag) const;
    
    /**給body施加一個循序漸進的力,物體會受加速度影響,越來越快,像火車一樣*/
    virtual void applyForce(const Vect& force);
    /** offset為偏移度 指碰到物體時 body旋轉 偏移 一般設為預設值 值越大 旋轉越快 偏移角度越大*/
    virtual void applyForce(const Vect& force, const Point& offset);
    /** 重置施加在body上的力  清0了. */
    virtual void resetForces();
    /** 不會產生力,直接與body的速度疊加 產生新的速度. */
    virtual void applyImpulse(const Vect& impulse);
    /** Applies a continuous force to body. */
    virtual void applyImpulse(const Vect& impulse, const Point& offset);
    /**施加一個扭轉力到剛體上  就像向前翻轉一塊大石頭一樣. */
    virtual void applyTorque(float torque);
    
    /** 設定剛體的速度*/
    virtual void setVelocity(const Vect& velocity);
    /** 獲取剛體的速度 */
    virtual Point getVelocity();
    /** 設定剛體角速度 就是單位時間內轉動的弧度*/
    virtual void setAngularVelocity(float velocity);
    /** 通過一個區域性點獲取剛體的角速度*/
    virtual Point getVelocityAtLocalPoint(const Point& point);
    /** 通過世界點獲取剛體的角速度*/
    virtual Point getVelocityAtWorldPoint(const Point& point);
    /** 獲取剛體的角速度 */
    virtual float getAngularVelocity();
    /** 設定速度的極限值*/
    virtual void setVelocityLimit(float limit);
    /**獲取速度的極限值 */
    virtual float getVelocityLimit();
    /** 設定角速度極限值 */
    virtual float getAngularVelocityLimit();
    
    /** 從world中移除body */
    void removeFromWorld();
    
    /** 獲取world */
    inline PhysicsWorld* getWorld() const { return _world; }
    /**獲取body的所有關節 */
    inline const std::vector<PhysicsJoint*>& getJoints() const { return _joints; }
    
    /** 取得body設定的sprite. */
    inline Node* getNode() const { return _node; }
    
    /**
     * A mask that defines which categories this physics body belongs to.
     * Every physics body in a scene can be assigned to up to 32 different categories, each corresponding to a bit in the bit mask. You define the mask values used in your game. In conjunction with the collisionBitMask and contactTestBitMask properties, you define which physics bodies interact with each other and when your game is notified of these interactions.
     * The default value is 0xFFFFFFFF (all bits set).
     */沒搞懂
    void setCategoryBitmask(int bitmask);
    /** 
     * A mask that defines which categories of bodies cause intersection notifications with this physics body.
     * When two bodies share the same space, each body’s category mask is tested against the other body’s contact mask by performing a logical AND operation. If either comparison results in a non-zero value, an PhysicsContact object is created and passed to the physics world’s delegate. For best performance, only set bits in the contacts mask for interactions you are interested in.
     * The default value is 0x00000000 (all bits cleared).
     */
    void setContactTestBitmask(int bitmask);
    /**
     * A mask that defines which categories of physics bodies can collide with this physics body.
     * When two physics bodies contact each other, a collision may occur. This body’s collision mask is compared to the other body’s category mask by performing a logical AND operation. If the result is a non-zero value, then this body is affected by the collision. Each body independently chooses whether it wants to be affected by the other body. For example, you might use this to avoid collision calculations that would make negligible changes to a body’s velocity.
     * The default value is 0xFFFFFFFF (all bits set).
     */
    void setCollisionBitmask(int bitmask);
    /** get the category bit mask */
    inline int getCategoryBitmask() const { return _categoryBitmask; }
    /** get the contact test bit mask */
    inline int getContactTestBitmask() const { return _contactTestBitmask; }
    /** get the collision bit mask */
    inline int getCollisionBitmask() const { return _collisionBitmask; }
    
    /** 
     * set the group of body
     * Collision groups let you specify an integral group index. You can have all fixtures with the same group index always collide (positive index) or never collide (negative index)
     * it have high priority than bit masks
     */
    void setGroup(int group);
    /** get the group of body */
    inline int getGroup() const { return _group; }
    
    /** 獲取body座標 */
    Point getPosition() const;
    /** 獲取body角度. */
    float getRotation() const;
    
    /**判斷body是否靜止*/
    inline bool isDynamic() const { return _dynamic; }
    /**設定body狀態  false為靜態 true為動態*/
    void setDynamic(bool dynamic);
    
    /**設定mass值 如果需要增加mass  有addmass方法  不要在這裡做加減 */
    void setMass(float mass);
    /** 取得mass. */
    inline float getMass() const { return _mass; }
    /**
     * @brief add mass to body.
     * if _mass(mass of the body) == PHYSICS_INFINITY, it remains.
     * if mass == PHYSICS_INFINITY, _mass will be PHYSICS_INFINITY.
     * if mass == -PHYSICS_INFINITY, _mass will not change.
     * if mass + _mass <= 0, _mass will equal to MASS_DEFAULT(1.0)
     * other wise, mass = mass + _mass;
     */增加質量
    void addMass(float mass);
    
    /**
     * @brief set the body moment of inertia.
     * @note if you need add/subtract moment to body, don't use setMoment(getMoment() +/- moment), because the moment of body may be equal to PHYSICS_INFINITY, it will cause some unexpected result, please use addMoment() instead.
     */設定力矩
    void setMoment(float moment);
    /** 獲取慣性的力矩. */
    inline float getMoment(float moment) const { return _moment; }
    /**
     * @brief add moment of inertia to body.
     * if _moment(moment of the body) == PHYSICS_INFINITY, it remains.
     * if moment == PHYSICS_INFINITY, _moment will be PHYSICS_INFINITY.
     * if moment == -PHYSICS_INFINITY, _moment will not change.
     * if moment + _moment <= 0, _moment will equal to MASS_DEFAULT(1.0)
     * other wise, moment = moment + _moment;
     */增加力矩
    void addMoment(float moment);
    /** 取得線性阻尼 */
    inline float getLinearDamping() const { return _linearDamping; }
    /** 
     * 設定阻尼值
     *它用來模擬body在氣體或者液體中的摩擦力
     *取值範圍是 0.0f to 1.0f. 
     */
    inline void setLinearDamping(float damping) { _linearDamping = damping; }
    /** 獲取角阻尼 */
    inline float getAngularDamping() const { return _angularDamping; }
    /**
     * 設定角阻尼
     * 它用來模擬body在氣體或者液體中的角阻尼
     * the value is 0.0f to 1.0f.
     */
    inline void setAngularDamping(float damping) { _angularDamping = damping; }
    
    /** 判斷body是否是 休息狀態 */
    bool isResting() const;
    /** 
     *判斷body能否在物理世界中模擬
     */
    inline bool isEnabled() const { return _enable; }
    /**
  設定body能否在物理世界中模擬
     */
    void setEnable(bool enable);
    
    /** whether the body can rotation */
    inline bool isRotationEnabled() const { return _rotationEnable; }
    /**設定能否旋轉*/
    void setRotationEnable(bool enable);
    
    /** 判斷body是否受引力影響 */
    inline bool isGravityEnabled() const { return _gravityEnable; }
    /** 設定body是否受引力影響 */
    void setGravityEnable(bool enable);
    
    /** 取得body 的tag值 */
    inline int getTag() const { return _tag; }
    /** 設定body tag值*/
    inline void setTag(int tag) { _tag = tag; }
    
    /** 轉換 世界點 到 區域性點  類似 世界座標和 區域性座標的轉換*/
    Point world2Local(const Point& point);
    /** 轉換區域性座標到 世界座標 */
    Point local2World(const Point& point);

2.PhysicsShape