cocos2d-x3.0 新的物理引擎之详解setCategoryBitmask()、setContactTestBitmask()、setCollisionBitmask()

转载请注明出处:游戏开发实验室http://blog.csdn.net/u010019717/article/details/32942641

我在编写游戏的时候遇到了这个问题,  物理引擎其他的内容还好理解,  就这三个函数就是没找到有人详细的解释一下。  我不知道这个都没弄明白,游戏是怎么做出来的。那我就不吐糟了,      下面的所有内容都是我的个人推断。不知道正不正确。    反正我目前是这么理解的。


我们先来看看这三个函数的定义:

/**
     * 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).
     */

/**定义了这个物理刚体是属于哪个类别的掩码。在一个场景中的每个物理刚体可以分配给达到 32 不同的类别(参数int bitmask是int类型4个字节32位),每个对应有32位中的1位掩码。您的游戏中您定义使用的掩码值。联同的 collisionBitMask 和 contactTestBitMask 的属性,定义哪些物理刚体彼此之间进行交互和何时你接收到这些交互作用的通知。默认值为的 0xFFFFFFFF (所有的位都被设置)。    
     */
    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).
     */

    /**
    一个掩码,它定义了哪些类别的刚体与此物理刚体产生交集(相互作用)的通知。当两个刚体共有同一个空间时,通过执行逻辑与运算每个刚体的类别掩码被检测测试反对其他的刚体的接触掩码。如果任一比较结果在一个非零值,一个 PhysicsContact 对象被创建并传递到物理世界的委托。为获得最佳性能,仅设置您感兴趣的互动相互作用的接触掩码位。默认值为 0x00000000 (所有位均被清除)。
     */
    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).
     */

    /**

一个掩码,它定义了哪些类别的物理刚体可以与这物理刚体发生碰撞。当两个物理刚体互相接触时,可能会发生冲突。这个刚体的碰撞掩码被相比其他刚体的类别掩码通过执行按位逻辑与运算。如果结果是一个非零值,则这一刚体被受碰撞。每个刚体独立地选择是否愿意受其他刚体的影响。例如,您可能会使用这来避免碰撞计算使对刚体的速度的变化可以忽略。默认值为的 0xFFFFFFFF (所有的位设置)。
     */
    inline void setCollisionBitmask(int bitmask) { _collisionBitmask = bitmask; }
    inline int getCollisionBitmask() const { return _collisionBitmask; }


每个函数说了这么多,那么具体是什么意思呢?  看看下面的例子:

box1->getPhysicsBody()->setCategoryBitmask(0x01); // 0001
box1->getPhysicsBody()->setContactTestBitmask(0x04); // 0100
box1->getPhysicsBody()->setCollisionBitmask(0x03); // 0011

box2->getPhysicsBody()->setCategoryBitmask(0x02);    // 0010
box2->getPhysicsBody()->setContactTestBitmask(0x08); // 1000
box2->getPhysicsBody()->setCollisionBitmask(0x01);   // 0001

box3->getPhysicsBody()->setCategoryBitmask(0x04);    // 0100
box3->getPhysicsBody()->setContactTestBitmask(0x01); // 0001
box3->getPhysicsBody()->setCollisionBitmask(0x06);   // 0110

 box1 和 box2 发生碰撞
 box1 box3  不会
 box2 box3 也不会

为什么呢?    解释如下:

box1的类别掩码 00000000 00000000 00000000 00000001

可接到通知         00000000 00000000 00000000 00000100

允许撞我            00000000 00000000 00000000 00000011


box2的类别掩码  00000000 00000000 00000000 00000010

可接到通知         00000000 00000000 00000000 00001000

允许撞我             00000000 00000000 00000000 00000001


box3的类别掩码  00000000 00000000 00000000 00000100

可接到通知          00000000 00000000 00000000 00000001

允许撞我             00000000 00000000 00000000 00000110


现在做运算呗:

box1的允许撞我     00000000 00000000 00000000 00000011    与box2的类别掩码做按位与运算

box2的类别掩码     00000000 00000000 00000000 00000010

结果为:                00000000 00000000 00000000 00000010      不为0,  box1会撞到box2撞到。

同理:

box2的允许撞我     00000000 00000000 00000000 00000001    与box1的类别掩码做按位与运算

box1的类别掩码     00000000 00000000 00000000 00000001

结果为:                00000000 00000000 00000000 00000001      不为0,  box2会撞到box1撞到。

他们会相互受到撞击的。


box2的允许撞我与box3的类别掩码  按位与运算为0;

box3的允许撞我与box2的类别掩码  按位与运算为0;


box1的允许撞我与box3的类别掩码  按位与运算为0;

box3的允许撞我与box1的类别掩码  按位与运算为0;

所以:

box1 and box2 发生碰撞
but the box1 box3  不会
the box2 box3 也不会





  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值