天天看點

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

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

3.0以後最box2d和chipmunk這兩個實體引擎進行了封裝,使用起來非常的便利。

官方連結位址:英文版

泰然網:中文教程

offset:重心點

velocity:速度

dadamping:阻尼

rerestitution:彈力

mamaterial:材質

mass:品質

moment:力矩,當他碰到另一個剛體時候 ,會産生一股扭轉力,做旋轉運動

body:剛體,表示實體世界中的抽象實體,附帶有實體屬性

shape:剛體的形狀,同一個body可以附加多個shape 該shape們不會發生碰撞

joint:關節,可以連接配接>=2個剛體

1.physicsBody

/** 建立一個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

/** 通過shape 取得body */
    inline PhysicsBody* getBody() const { return _body; }
    /** 傳回shape的類型 */
    inline Type getType() const { return _type; }
    /** 傳回shape的面積 */
    inline float getArea() const { return _area; }
    /** 取得moment 力矩 */
    inline float getMoment() const { return _moment; }
    /** Set moment, it will change the body's moment this shape attaches */
    void setMoment(float moment);//設定力矩
    inline void setTag(int tag) { _tag = tag; }//設定标簽tag
    inline int getTag() const { return _tag; }//取得tag标簽
    
    /**擷取品質 */
    inline float getMass() const { return _mass; }
    /** Set mass, it will change the body's mass this shape attaches */											
    void setMass(float mass);//設定品質
    inline float getDensity() const { return _material.density; }//density為密度
    void setDensity(float density);//擷取密度
    inline float getRestitution() const { return _material.restitution; }//擷取彈性
    void setRestitution(float restitution);//設定彈性
    inline float getFriction() const { return _material.friction; }//friction為摩擦力
    void setFriction(float friction);//設定摩擦力
    const PhysicsMaterial& getMaterial() const { return _material; }//Material為材質
    void setMaterial(const PhysicsMaterial& material);設定材質
    
    /** 傳回預設力矩  其值為0  */
    virtual float calculateDefaultMoment() { return 0.0f; }
    /** 取得重心 初始值為zero */
    virtual Point getOffset() { return Point::ZERO; }
    /** 擷取shape的重心點  */
    virtual Point getCenter() { return getOffset(); }
    /**shape是否包含該點 */
    bool containsPoint(const Point& point) const;
    
    /** 改變重心點 */
    static void recenterPoints(Point* points, int count, const Point& center = Point::ZERO);
    /** 取得多邊形的重心點 */
    static Point getPolyonCenter(const Point* points, int count);
    
    /**
     * 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).
     */
    inline void setCategoryBitmask(int bitmask) { _categoryBitmask = bitmask; }
    inline int getCategoryBitmask() const { return _categoryBitmask; }
    /**
     * 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).
     */
    inline void setContactTestBitmask(int bitmask) { _contactTestBitmask = bitmask; }
    inline int getContactTestBitmask() const { return _contactTestBitmask; }
    /**
     * 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).
     */
    inline void setCollisionBitmask(int bitmask) { _collisionBitmask = bitmask; }
    inline int getCollisionBitmask() const { return _collisionBitmask; }
    
    void setGroup(int group);
    inline int getGroup() { return _group; }
           
cocos2d-x 3.0開發筆記---實體引擎封裝 Physics深入學習