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

cocos2d-x节点(b2World.h)API

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

/// world 类管理所有的物理实体,动态仿真,异步查询

/// World 也包含也包含高效的内存管理设施。

///cocos2d-x-3.0alpha0/external/Box2D/Dynamics
/// world 类管理所有的物理实体,动态仿真,异步查询
/// World 也包含也包含高效的内存管理设施。


#ifndef B2_WORLD_H
#define B2_WORLD_H

#include <Box2D/Common/b2Math.h>
#include <Box2D/Common/b2BlockAllocator.h>
#include <Box2D/Common/b2StackAllocator.h>
#include <Box2D/Dynamics/b2ContactManager.h>
#include <Box2D/Dynamics/b2WorldCallbacks.h>
#include <Box2D/Dynamics/b2TimeStep.h>

struct b2AABB;
struct b2BodyDef;
struct b2Color;
struct b2JointDef;
class b2Body;
class b2Draw;
class b2Fixture;
class b2Joint;

/// world 类管理所有的物理实体,动态仿真,异步查询
/// World 也包含也包含高效的内存管理设施。
class b2World
{
public:
    
    /// 构建一个 world 对象.
    /// @param gravity world 重力矢量.
    b2World(const b2Vec2& gravity);

    /// 销毁 world.所有物理实体将被销毁并释放所有的堆内存
    ~b2World();

    /// 注册销毁侦听器。你拥有这个监听器,必须把它保持在一定范围
    void SetDestructionListener(b2DestructionListener* listener);

    /// 注册接触过滤器来提供特定的碰撞控制权。
    /// 其它情况使用默认的过滤器 (b2_defaultFilter).你拥有这个监听器,必须把它保持在一定范围
    void SetContactFilter(b2ContactFilter* filter);

    /// 注册一个接触事件监听. 你拥有这个监听器,必须把它保持在一定范围
    void SetContactListener(b2ContactListener* listener);

    /// 注册一个例程来调试绘图. 调用调试绘制函数里面的 b2World::DrawDebugData 方法. 你拥有这个调试对象,你必须把它保持在一定范围
    void SetDebugDraw(b2Draw* debugDraw);

    /// 根据给定的定义创建一个刚体.没有参考的定义将被保留。
    /// @warning 此功能被锁定在回调过程中
    b2Body* CreateBody(const b2BodyDef* def);

    /// 根据给定的定义销毁一个刚体. 没有参考的定义将被保留。
    /// is retained. 此功能被锁定在回调过程中
    /// @warning 这会自动删除所有相关的形状和joints(接头)
    /// @warning 此功能被锁定在回调过程中
    void DestroyBody(b2Body* body);

    /// 创建一个joints(接头)一起约束 bodies . 没有参考的定义将被保留。
    /// is retained. 这可能会导致 body 停止碰撞
    /// @warning 此功能被锁定在回调过程中
    b2Joint* CreateJoint(const b2JointDef* def);

    /// 销毁一个joints(接头). 这可能会导致 body 开始碰撞
    /// @warning 此功能被锁定在回调过程中
    void DestroyJoint(b2Joint* joint);

    /// 获取一个时间步. 进行碰撞检测,集成了约束求解。
    /// @param timeStep 时间模拟量这不应该发生变化
    /// @param velocityIterations for the velocity(速度)constraint(约束)solver(运算器).
    /// @param positionIterations for the position constraint(约束)solver(运算器).
    void Step(    float32 timeStep,
                int32 velocityIterations,
                int32 positionIterations);

    ///手动清除所有 body的力缓存. 默认情况下,每次调用后,body 会被自动清除
    /// 通过调用 SetAutoClearForces 修改默认行为.
    ///此功能用于支持分步执行,分布经常被用于在一个固定大小的时间步长下动态改变帧速率。
    /// 当你执行分布操作后会禁用自动清除力的功能,而是在所有字步骤一次游戏循环完成后调用 ClearForces
    /// @see SetAutoClearForces
    void ClearForces();

    ///调用该函数来绘制形状和其他调试绘制数据
    void DrawDebugData();

    /// 查询 world 所有的  fixture(定制器)潜在的 aabb 重叠
    /// @param callback 用户实现的回调类.
    /// @param aabb the query box.
    void QueryAABB(b2QueryCallback* callback, const b2AABB& aabb) const;

    /// 所有的 fixture(定制器)的光线路径的world 光线投射. 你是否回调控制得到 最近点,任意点, n-points 
    /// 光线投射忽略形状(包含起始点)
    /// @param callback a user implemented callback class.
    /// @param point1 the ray starting point
    /// @param point2 the ray ending point
    void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2) const;

    ///获取 world body 列表.  使用 b2Body::GetNext 获取 world body 列表里的下一个 body,并返回该body;
    /// 一个 NULL body 表示列表的末尾
    /// @return the head of the world body list.
    b2Body* GetBodyList();
    const b2Body* GetBodyList() const;

    /// 获取 world joints(接头)列表. 使用 b2Joint::GetNext  获取 world joints(接头)列表里的下一个 joints(接头),并返回该joints(接头);
    ///  一个 NULL joints(接头) 表示列表的末尾
    /// @return the head of the world joint list.
    b2Joint* GetJointList();
    const b2Joint* GetJointList() const;

    /// 获取 world 接触列表. With the returned contact, 使用 b2Contact::GetNext  获取 world 接触列表里的下一个接触,并返回该接触);
    /// 一个 NULL 接触 表示列表的末尾
    /// @return world 接触列表的头
    /// @warning 接触在一个时间步的中间创建和销毁
    /// 使用b2ContactListener以避免丢失的接触
    b2Contact* GetContactList();
    const b2Contact* GetContactList() const;

    /// Enable/disable sleep.
    void SetAllowSleeping(bool flag);
    bool GetAllowSleeping() const { return m_allowSleep; }

    /// Enable/disable 热启动. For testing.
    void SetWarmStarting(bool flag) { m_warmStarting = flag; }
    bool GetWarmStarting() const { return m_warmStarting; }

    /// Enable/disable 连续物理. For testing.
    void SetContinuousPhysics(bool flag) { m_continuousPhysics = flag; }
    bool GetContinuousPhysics() const { return m_continuousPhysics; }

    /// Enable/disable 连续的物理单步. For testing.
    void SetSubStepping(bool flag) { m_subStepping = flag; }
    bool GetSubStepping() const { return m_subStepping; }

    /// Get broad-phase 代理的数量.
    int32 GetProxyCount() const;

    /// Get joints(接头)的数量.
    int32 GetBodyCount() const;

    /// Get joints(接头)的数量.
    int32 GetJointCount() const;

    ///获取接触的数量 (每一个都可以有0个或多个接触点).
    int32 GetContactCount() const;

    //获取动态树的高度。
    int32 GetTreeHeight() const;

    ///获得平衡的动态树。
    int32 GetTreeBalance() const;

    /// 获取动态树的深度. 越小越好。最小值为1。
    float32 GetTreeQuality() const;

    /// Change 全局重力矢量
    void SetGravity(const b2Vec2& gravity);
    
    /// Get 全局重力矢量
    b2Vec2 GetGravity() const;

    /// world 是否被锁定(在中间的一个时间步长)
    bool IsLocked() const;

    /// Set 每一个时间步后控制自动清除力量的标志。
    void SetAutoClearForces(bool flag);

    /// 获取每一个时间步后控制自动清除力量的标志。
    bool GetAutoClearForces() const;

    /// 为了测试获取接触管理
    const b2ContactManager& GetContactManager() const;

    /// 获取当前配置文件。
    const b2Profile& GetProfile() const;

    /// 把 world 的阻尼输出到日志文件中.
    /// @warning 每一个时间步都应该调用这个 outside
    void Dump();

private:

    // m_flags
    enum
    {
        e_newFixture    = 0x0001,
        e_locked        = 0x0002,
        e_clearForces    = 0x0004
    };

    friend class b2Body;
    friend class b2Fixture;
    friend class b2ContactManager;
    friend class b2Controller;

    void Solve(const b2TimeStep& step);
    void SolveTOI(const b2TimeStep& step);

    void DrawJoint(b2Joint* joint);
    void DrawShape(b2Fixture* shape, const b2Transform& xf, const b2Color& color);

    b2BlockAllocator m_blockAllocator;
    b2StackAllocator m_stackAllocator;

    int32 m_flags;

    b2ContactManager m_contactManager;

    b2Body* m_bodyList;
    b2Joint* m_jointList;

    int32 m_bodyCount;
    int32 m_jointCount;

    b2Vec2 m_gravity;
    bool m_allowSleep;

    b2DestructionListener* m_destructionListener;
    b2Draw* m_debugDraw;

    //这个是用于计算的时间步长的比率,以支持一个可变的时间步长。
    float32 m_inv_dt0;

    // 这些都为调试 solver(运算器).
    bool m_warmStarting;
    bool m_continuousPhysics;
    bool m_subStepping;

    bool m_stepComplete;

    b2Profile m_profile;
};

inline b2Body* b2World::GetBodyList()
{
    return m_bodyList;
}

inline const b2Body* b2World::GetBodyList() const
{
    return m_bodyList;
}

inline b2Joint* b2World::GetJointList()
{
    return m_jointList;
}

inline const b2Joint* b2World::GetJointList() const
{
    return m_jointList;
}

inline b2Contact* b2World::GetContactList()
{
    return m_contactManager.m_contactList;
}

inline const b2Contact* b2World::GetContactList() const
{
    return m_contactManager.m_contactList;
}

inline int32 b2World::GetBodyCount() const
{
    return m_bodyCount;
}

inline int32 b2World::GetJointCount() const
{
    return m_jointCount;
}

inline int32 b2World::GetContactCount() const
{
    return m_contactManager.m_contactCount;
}

inline void b2World::SetGravity(const b2Vec2& gravity)
{
    m_gravity = gravity;
}

inline b2Vec2 b2World::GetGravity() const
{
    return m_gravity;
}

inline bool b2World::IsLocked() const
{
    return (m_flags & e_locked) == e_locked;
}

inline void b2World::SetAutoClearForces(bool flag)
{
    if (flag)
    {
        m_flags |= e_clearForces;
    }
    else
    {
        m_flags &= ~e_clearForces;
    }
}

/// 获取每一个时间步后控制自动清除力量的标志。
inline bool b2World::GetAutoClearForces() const
{
    return (m_flags & e_clearForces) == e_clearForces;
}

inline const b2ContactManager& b2World::GetContactManager() const
{
    return m_contactManager;
}

inline const b2Profile& b2World::GetProfile() const
{
    return m_profile;
}

#endif


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐