cocos2d-x节点(b2RevoluteJoint.h)API

时间:2020-12-29 00:05:22

本文来自http://blog.csdn.net/runaying ,引用必须注明出处!

cocos2d-x节点(b2RevoluteJoint.h)API

温馨提醒:为了大家能更好学习,强烈推荐大家看看本人的这篇博客 Cocos2d-X权威指南笔记

// Revolute(旋转)joints(接头)需要一个共同点来约束它们的 bodies,它们可以围绕这个点*旋转

///cocos2d-x-3.0alpha0/external/Box2D/Dynamics/Joints
// Revolute(旋转)joints(接头)需要一个共同点来约束它们的 bodies,它们可以围绕这个点*旋转


#ifndef B2_REVOLUTE_JOINT_H
#define B2_REVOLUTE_JOINT_H

#include <Box2D/Dynamics/Joints/b2Joint.h>

/// Revolute(旋转)joints(接头)定义. 添加进 joints(接头)的 bodies需要定义一个 锚点
/// 定义使用本地锚点,所以初始配置可以稍微违反约束。你还需要为 joints(接头)限制制定一个初始的相对角度
/// 这有助于保存和加载游戏,本地锚点的测量是根据 body's 的原点,而不是质量中心,因为:
/// 1. 你可能不知道质量中心再哪.
/// 2. 如果你从 body 上 add/remove shapes,会重新计算质量,joints(接头)也会被破坏
struct b2RevoluteJointDef : public b2JointDef
{
    b2RevoluteJointDef()
    {
        type = e_revoluteJoint;
        localAnchorA.Set(0.0f, 0.0f);
        localAnchorB.Set(0.0f, 0.0f);
        referenceAngle = 0.0f;
        lowerAngle = 0.0f;
        upperAngle = 0.0f;
        maxMotorTorque = 0.0f;
        motorSpeed = 0.0f;
        enableLimit = false;
        enableMotor = false;
    }

    /// Initialize the bodies, anchors, and reference angle using a world
    /// anchor point.
    void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor);

    /// 相对与 bodyA's 原点的本地锚点.
    b2Vec2 localAnchorA;

    /// 相对与 bodyB's 原点的本地锚点.
    b2Vec2 localAnchorB;

    /// The bodyB angle minus(减去) bodyA angle in the reference state (radians).        
    float32 referenceAngle;

    /// 一个启用 joints(接头) limits 的标识.
    bool enableLimit;

    /// The lower(底部) angle for the joint limit (radians).
    float32 lowerAngle;

    /// The upper(顶部) angle for the joint limit (radians).
    float32 upperAngle;

    /// A flag to enable the joint motor.
    bool enableMotor;

    /// 所需的(电机)转速. Usually in radians per second.
    float32 motorSpeed;

    /// torque (电机)的最大力矩来实现所需的(电机)转速。
    /// Usually in N-m.
    float32 maxMotorTorque;
};
//  Revolute(旋转)joints(接头)需要一个共同点来约束它们的 bodies,它们可以围绕这个点*旋转
/// joints(接头)角围绕共享点相对转动 。你可以使用 joints(接头)指定角度的上限和下限来限制相对转动
/// 你可以使用 motor 来驱动,围绕共享点相对旋转,电机
/// motor 由最大力矩提供,所以不会产生无穷的力量
class b2RevoluteJoint : public b2Joint
{
public:
    b2Vec2 GetAnchorA() const;
    b2Vec2 GetAnchorB() const;

    /// 相对与 bodyA's 原点的本地锚点.
    const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }

    /// 相对与 bodyB's 原点的本地锚点.
    const b2Vec2& GetLocalAnchorB() const  { return m_localAnchorB; }

    /// Get the reference angle.        //参考
    float32 GetReferenceAngle() const { return m_referenceAngle; }

    /// Get the current joint angle in radians.
    float32 GetJointAngle() const;

    /// Get the current joint angle speed in radians per second.        //获取当前 joints(接头)角速度 弧度每秒
    float32 GetJointSpeed() const;

    /// Is the joints(接头) limit enabled?
    bool IsLimitEnabled() const;

    /// Enable/disable the joints(接头) limit.
    void EnableLimit(bool flag);

    /// Get the lower(底部) joints(接头) limit in radians.
    float32 GetLowerLimit() const;

    /// Get the upper(顶部) joints(接头) limit in radians.
    float32 GetUpperLimit() const;

    /// Set the joints(接头) limits in radians.
    void SetLimits(float32 lower, float32 upper);

    /// joints(接头) motor 是否启用了.        //电机
    bool IsMotorEnabled() const;

    /// Enable/disable the joints(接头) motor.        //电机
    void EnableMotor(bool flag);

    /// Set  motor 的速度 弧度每秒     //电机
    void SetMotorSpeed(float32 speed);
    float32 GetMotorSpeed() const;

    /// Set motor 的最大力矩, usually in N-m.        //电机
    void SetMaxMotorTorque(float32 torque);
    float32 GetMaxMotorTorque() const { return m_maxMotorTorque; }

    /// 使用给定的相反时间步,获取反作用 力
    /// Unit is N.
    b2Vec2 GetReactionForce(float32 inv_dt) const;

//    使用给定的相反时间步,获取反作用力矩来限制 joints(接头)
       /// Unit is N*m.
    float32 GetReactionTorque(float32 inv_dt) const;
//使用给定的相反时间步,获取当前 motor 力矩      //电机
    /// Unit is N*m.
    float32 GetMotorTorque(float32 inv_dt) const;

    /// 把阻尼输出转储到 b2Log.
    void Dump();

protected:
    
    friend class b2Joint;
    friend class b2GearJoint;

    b2RevoluteJoint(const b2RevoluteJointDef* def);

    void InitVelocityConstraints(const b2SolverData& data);
    void SolveVelocityConstraints(const b2SolverData& data);
    bool SolvePositionConstraints(const b2SolverData& data);

    // Solver shared
    b2Vec2 m_localAnchorA;
    b2Vec2 m_localAnchorB;
    b2Vec3 m_impulse;
    float32 m_motorImpulse;

    bool m_enableMotor;
    float32 m_maxMotorTorque;
    float32 m_motorSpeed;

    bool m_enableLimit;
    float32 m_referenceAngle;
    float32 m_lowerAngle;
    float32 m_upperAngle;

    // Solver temp
    int32 m_indexA;
    int32 m_indexB;
    b2Vec2 m_rA;
    b2Vec2 m_rB;
    b2Vec2 m_localCenterA;
    b2Vec2 m_localCenterB;
    float32 m_invMassA;
    float32 m_invMassB;
    float32 m_invIA;
    float32 m_invIB;
    b2Mat33 m_mass;            // effective mass for point-to-point constraint.
    float32 m_motorMass;    // effective mass for motor/limit angular constraint.
    b2LimitState m_limitState;
};

inline float32 b2RevoluteJoint::GetMotorSpeed() const
{
    return m_motorSpeed;
}

#endif