@Refetizm wrote:
I needed a time-independent spawn system. So I decided to use collision detection. I added Physics Body to my spawn boxes and added a node to the game screen. When the box contacts the node, a new box is created immediately behind it, and the same process is repeated when the newly formed box contacts the node. However, the collision detection code I wrote does not work properly. Sometimes it doesn’t detect collision. Sometimes it works properly. Sometimes it also doesn’t detect the collision’s re-running after working properly for a while. Where is the problem and how can i solve it?
Codes:
spawnNode = Node::create(); spawnNode->setTag(SPAWN_TAG); auto spawnBody = PhysicsBody::createBox(Size(1, visibleSize.height), PhysicsMaterial(0, 1, 0)); spawnBody->setCollisionBitmask(GAME_SPAWN_TRIGGER); spawnBody->setContactTestBitmask(true); spawnBody->setDynamic(false); spawnNode->setPhysicsBody(spawnBody); spawnNode->setPosition(Point(visibleSize.width - spawnerSprite->getContentSize().width, visibleSize.height / 2 + origin.y));
auto buildingBody = PhysicsBody::createBox(this->getContentSize(), PhysicsMaterial(0, 1, 0)); buildingBody->setCollisionBitmask(BUILDING_SPAWN_TRIGGER); buildingBody->setContactTestBitmask(true); buildingBody->setDynamic(false); this->setPhysicsBody(buildingBody);
bool HelloWorld::onSpawnContactBegin(cocos2d::PhysicsContact &spawnContact) { PhysicsBody* a = spawnContact.getShapeA()->getBody(); PhysicsBody* b = spawnContact.getShapeB()->getBody(); if(a->getCollisionBitmask() == BUILDING_SPAWN_TRIGGER && b->getCollisionBitmask() == GAME_SPAWN_TRIGGER || a->getCollisionBitmask() == GAME_SPAWN_TRIGGER && b->getCollisionBitmask() == BUILDING_SPAWN_TRIGGER) { generateBuildings(); } return true; }
Posts: 2
Participants: 2
