cocos 物理引擎 设置掩码无效

body1->setCategoryBitmask(0x01); 
body1->setContactTestBitmask(0x01); 
body1->setCollisionBitmask(0x01);     

body2->setCategoryBitmask(0x01); 
body2->setContactTestBitmask(0x01); 
body2->setCollisionBitmask(0x01);    

onContactBegin 有监听到
但是两个物体碰撞会穿透 这是为什么 只单独设置ContactTestBitmask 也有这种情况 物体会穿透

没人吗 问题还没解决

是不是刚体设置成sensor了?

没有,按照你说尝试去设置也不行额。。

对了,你body上没shape吧?

这样创建的
auto sprite = Sprite::create(“hero.png”);
auto body1 = PhysicsBody::createBox(Size(width,height));
sprite:setPhysicsBody(body1)

cocos3.15 完整代码在这

#include “HelloWorldScene.h”

USING_NS_CC;

Scene* HelloWorld::createScene()
{
// ‘scene’ is an autorelease object
auto scene = Scene::createWithPhysics();
//test
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

// 'layer' is an autorelease object
auto layer = HelloWorld::create();

// add layer as a child to scene
scene->addChild(layer);

// return the scene
return scene;

}

// on “init” you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

auto box = PhysicsBody::createEdgeBox(visibleSize, PHYSICSSHAPE_MATERIAL_DEFAULT, 3.0f);
auto sharpe = Node::create();
sharpe->setPhysicsBody(box);
sharpe->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
addChild(sharpe);

auto blockbody = PhysicsBody::createBox(Size(55,35));
blockbody->setDynamic(false);
auto blocksprite = Sprite::create("block.png");
blocksprite->setPosition(300, 200);
blocksprite->setPhysicsBody(blockbody);
addChild(blocksprite);


auto hero = Sprite::create("hero_item_ari.png");
hero->setPosition(300, 100);
hero->setTag(1);
addChild(hero);

auto herobody = PhysicsBody::createBox(Size(123,125));
hero->setPhysicsBody(herobody);

herobody->setCategoryBitmask(0x01); //0001  
herobody->setContactTestBitmask(0x01); //0001
herobody->setCollisionBitmask(0x01);    //0011  

blockbody->setCategoryBitmask(0x01); //0010 
blockbody->setContactTestBitmask(0x01); //0011
blockbody->setCollisionBitmask(0x01);    //0001  

auto listener = EventListenerTouchOneByOne::create();

listener->onTouchBegan = [&](Touch* touch,Event* event)
{
	this->getChildByTag(1)->getPhysicsBody()->setVelocity(Vec2(0,100));
	return true;
};

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);


auto physicslistener = EventListenerPhysicsContact::create();
physicslistener->onContactBegin = [](PhysicsContact& contact)
{	
	
	CCLOG("onContactBegin");
	return false;
};

_eventDispatcher->addEventListenerWithSceneGraphPriority(physicslistener, this);

return true;

}

不设置这些掩码 是正常 设置了之后 可以监听碰撞了 但是物体会穿透了

别设置contacttestbitmask试试

不设置就没任何问题。 搞晕了
官方写的
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.

  • @param bitmask An integer number, the default value is 0xFFFFFFFF (all bits set).

setCollisionBitmask 设置根本没用啊

也就是只设置ContactTestBitmask 物品就能穿透了

搞懂了就好了

好难用,感觉怎么设置都不对。。。

你好,我现在遇到一个类似的问题,
我既不能穿透又需要碰撞事件需要怎么设置,
设置ContactTestBitmask 穿透,但是有事件
不设置就不会穿透,但是没事件

不知道这里有没有挖坟的说法,我也遇到了楼里一模一样的问题,掩码都对,不绑定监听事件就不会穿透,绑定了就会出现穿透,禁止autoStep之类的都没效果,最后终于找到原因了:
碰撞开始的消息处理函数onContactBegin里,我有个分支返回了非true值。
而根据网上的资料:

cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN
它是碰撞刚发生时触发的事件,并且在此次碰撞中只会被调用一次。我们可以通过返回 true 或者 false 来决定物体是否发生碰撞。
需要注意的是,当这个事件的回调函数返回 flase 时,EVENT_PHYSICS_CONTACT_PRESOLVE和EVENT_PHYSICS_CONTACT_POSTSOLVE将不会被触发,但EVENT_PHYSICS_CONTACT_SEPERATE必定会触发。

cc.Handler.EVENT_PHYSICS_CONTACT_PRESOLVE
它在碰撞接触后的每帧都会调用。同样它可以通过返回 true 或者 false 来决定物体是否发生碰撞。

这个真的是不容易注意到的一点……

返回了true,也还是穿透,这个问题困扰太久了,晕