博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2d-x3.0 解释具体的新的物理引擎setCategoryBitmask()、setContactTestBitmask()、setCollisionBitmask()...
阅读量:4552 次
发布时间:2019-06-08

本文共 4515 字,大约阅读时间需要 15 分钟。

转载请注明出处:游戏开发实验室

我在编写游戏的时候遇到了这个问题。  物理引擎其它的内容还好理解。  就这三个函数就是没找到有人具体的解释一下。  我不知道这个都没弄明确。游戏是怎么做出来的。那我就不吐糟了,      以下的全部内容都是我的个人判断。

不知道正不对。    反正我眼下是这么理解的。

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

/**

     * 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);    // 0010box2->getPhysicsBody()->setContactTestBitmask(0x08); // 1000box2->getPhysicsBody()->setCollisionBitmask(0x01);   // 0001box3->getPhysicsBody()->setCategoryBitmask(0x04);    // 0100box3->getPhysicsBody()->setContactTestBitmask(0x01); // 0001box3->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 也不会




版权声明:本文博客原创文章。博客,未经同意,不得转载。

转载于:https://www.cnblogs.com/blfshiye/p/4680085.html

你可能感兴趣的文章
C teaching
查看>>
分隔指定内容,提取章节数
查看>>
this point
查看>>
leetcode 30 Substring with Concatenation of All Words
查看>>
验证登录信息是否合法
查看>>
线程池
查看>>
git版本控制器的基本使用
查看>>
Redis 笔记与总结4 set 和 zset 类型
查看>>
jQuery Ajax 回调函数中调用$(this)的问题 [ 转 ]
查看>>
thymeleaf:字符串拼接+输出单引号
查看>>
springboot:集成fastjson(教训)
查看>>
网络流 Edmons-Karp 算法讲解
查看>>
「NOIP2018模拟9.10」公约数 - 找规律 - gcd
查看>>
使用java理解程序逻辑(15)
查看>>
bzoj 1879 状压dp
查看>>
python 一些特殊用法和坑
查看>>
WIFI密码破解全攻略
查看>>
c++string各种函数
查看>>
errno.h含义
查看>>
字典树(模型体)
查看>>