本文整理汇总了C++中PhysicsBody类的典型用法代码示例。如果您正苦于以下问题:C++ PhysicsBody类的具体用法?C++ PhysicsBody怎么用?C++ PhysicsBody使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PhysicsBody类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: setPhysicsBody
void PFMPlayer::reload()
{
if(_preset == NULL)
return;
for(int i=0;i<_preset->components.size();i++)
{
PFMComponent* component = _preset->components[i];
if(component->componentClass == "Sprite")
{
PFMSpriteComponent* spriteComponent = dynamic_cast<PFMSpriteComponent*>(component);
PFMSprite* sprite = PFMSprite::createWithComponent(spriteComponent);
Size size = sprite->getBoundingBox().size;
PhysicsBody* physicsBody = PhysicsBody::createBox(sprite->getBoundingBox().size);
physicsBody->setCollisionBitmask(0);
physicsBody->setCategoryBitmask(PFMCollideMaskBitsPlayer);
physicsBody->setContactTestBitmask(PFMCollideMaskBitsEnemyBullet | PFMCollideMaskBitsEnemy);
setPhysicsBody(physicsBody);
addChild(sprite);
}
else if(component->componentClass == "BulletGun")
{
PFMBulletGunComponent* bulletGunComponent = dynamic_cast<PFMBulletGunComponent*>(component);
PFMBulletGun* bulletGun = PFMBulletGun::createWithComponent(bulletGunComponent);
bulletGun->isHostByPlayer = true;
bulletGun->isAutoShoot = true;
addChild(bulletGun);
}
// else if([component isMemberOfClass:[AstMissleLauncherComponent class]])
// {
// AstMissleLauncher* missleLauncher = [[AstMissleLauncher alloc]initWithSession:self.session];
// [self addNode:missleLauncher.rootNode];
// [missleLauncher setComponent:(AstMissleLauncherComponent*)component];
// }
}
}
示例2:
PhysicsForceField::~PhysicsForceField()
{
for (auto bm : _gravitySources) {
PhysicsBody* body = bm.first;
body->release();
}
}
示例3: getPhysicsBody
bool PFMPlayer::onContactBegin(cocos2d::PhysicsContact &contact)
{
if(contact.getShapeA()->getBody() == getPhysicsBody() ||
contact.getShapeB()->getBody() == getPhysicsBody())
{
PhysicsBody* other = contact.getShapeA()->getBody() == getPhysicsBody()?contact.getShapeB()->getBody() : contact.getShapeA()->getBody();
switch (other->getTag()) {
case PFMPhysicsBodyTypeEnemyBullet:
{
PFMBullet* bullet = dynamic_cast<PFMBullet*>(other->getNode());
if(bullet != NULL)
{
health -= bullet->damage;
}
other->getOwner()->removeFromParentAndCleanup(true);
break;
}
case PFMPhysicsBodyTypeEnemy:
other->getOwner()->removeFromParentAndCleanup(true);
break;
default:
break;
}
}
return true;
}
示例4: onContactBegin
bool GameScene::onContactBegin(cocos2d::PhysicsContact &contact) {
PhysicsBody *a = contact.getShapeA()->getBody();
PhysicsBody *b = contact.getShapeB()->getBody();
if ( (SNAKE_COLLISION_BITMASK == a->getCollisionBitmask() && FOOD_COLLISION_BITMASK == b->getCollisionBitmask()) ||
(SNAKE_COLLISION_BITMASK == b->getCollisionBitmask() && FOOD_COLLISION_BITMASK == a->getCollisionBitmask()) ) {
srand(time(NULL));
auto random = rand() % 370 + 30;
auto random2 = rand() % 270 + 30;
auto moveTo = MoveTo::create(0, Vec2(random, random2));
food->runAction(moveTo);
int tempX = snakeParts.back()->getPositionX();
int tempY = snakeParts.back()->getPositionY();
Sprite* tempSprite = Sprite::create("snake.png");
switch (direction) {
case LEFT:
tempSprite->setPosition(tempX + 10, tempY);
break;
case RIGHT:
tempSprite->setPosition(tempX - 10, tempY);
break;
case UP:
tempSprite->setPosition(tempX, tempY + 10);
break;
case DOWN:
tempSprite->setPosition(tempX, tempY - 10);
break;
}
this->addChild(tempSprite);
snakeParts.push_back(tempSprite);
}
return true;
}
示例5: PhysicsBody
void CW::CeilingTerrain::Init(void)
{
PhysicsBody* b = new PhysicsBody();
VerletPoint* V1 = new VerletPoint(0, 0);
VerletPoint* V2 = new VerletPoint(60, 0);
VerletPoint* V3 = new VerletPoint(60, 100);
VerletPoint* V4 = new VerletPoint(0, 100);
b->AddVertex(V1);
b->AddVertex(V2);
b->AddVertex(V3);
b->AddVertex(V4);
for (VerletPoint* p : b->Vertices)
{
p->IsStatic = true;
}
blocks.push_back(b);
for (int i=0;i<20;++i)
{
AddBlock();
}
}
示例6: CCASSERT
PhysicsBody *PEShapeCache::getPhysicsBodyByName(const std::string name)
{
BodyDef *bd = bodyDefs.at(name);
CCASSERT(bd != nullptr, "Body not found");
PhysicsBody *body = PhysicsBody::create(bd->mass, bd->momentum);
body->setPositionOffset(bd->anchorPoint);
for (auto fd : bd->fixtures)
{
if (fd->fixtureType == SHAPE_CIRCLE)
{
auto shape = PhysicsShapeCircle::create(fd->radius, PhysicsMaterial(0.0f, fd->elasticity, fd->friction), fd->center);
shape->setGroup(fd->group);
// shape->setCategoryBitmask(fd->collisionType);
body->addShape(shape);
}
else if (fd->fixtureType == SHAPE_POLYGON)
{
for (auto polygon : fd->polygons)
{
auto shape = PhysicsShapePolygon::create(polygon->vertices, polygon->numVertices, PhysicsMaterial(0.0f, fd->elasticity, fd->friction), fd->center);
shape->setGroup(fd->group);
// shape->setCategoryBitmask(fd->collisionType);
body->addShape(shape);
}
}
}
return body;
}
示例7: CCRANDOM_0_1
void SceneFlyGame::addBlock(float)
{
int r = CCRANDOM_0_1() * 5;
if (r == 0)
{
// 加砖头
r = CCRANDOM_0_1() * 6 + 4;
Size size = Size(16, 16 * r);
Node * node = Node::create();
addChild(node);
node->setContentSize(size);
for (int i = 0; i < r; ++i)
{
Sprite * sprite = Sprite::create("superMarioMap.png", Rect(1, 1, 16, 16));
node->addChild(sprite);
sprite->setPosition(0, i * 16);
sprite->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
}
node->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
PhysicsBody * body = PhysicsBody::createEdgeBox(size);
node->setPhysicsBody(body);
body->setContactTestBitmask(1);
// 设置node的位置
r = CCRANDOM_0_1() * 2;
node->setPositionX(winSize.width + 16);
if (r == 0)
node->setPositionY(size.height / 2);
else
node->setPositionY(winSize.height - size.height / 2);
// 加入数组
m_blocks.pushBack(node);
}
}
示例8: CCLOG
void PhysicsWorld::removeBody(PhysicsBody* body)
{
if (body->getWorld() != this)
{
CCLOG("Physics Warnning: this body doesn't belong to this world");
return;
}
// destory the body's joints
for (auto joint : body->_joints)
{
// set destroy param to false to keep the iterator available
removeJoint(joint, false);
PhysicsBody* other = (joint->getBodyA() == body ? joint->getBodyB() : body);
other->removeJoint(joint);
// test the distraction is delaied or not
if (_delayRemoveJoints.size() > 0 && _delayRemoveJoints.back() == joint)
{
joint->_destoryMark = true;
}
else
{
delete joint;
}
}
body->_joints.clear();
removeBodyOrDelay(body);
_bodies.eraseObject(body);
body->_world = nullptr;
}
示例9: joinToWorld
void Monster::joinToWorld(Node* parent)
{
Sprite* sp = Sprite::createWithSpriteFrameName(m_sModelPath.asString().c_str());
/* 创建刚体 */
PhysicsBody* body = PhysicsBody::createBox(sp->getContentSize());
body->setCategoryBitmask(1); // 0001
body->setCollisionBitmask(1); // 0001
body->setContactTestBitmask(1); // 0001
/* 精灵居中 */
sp->setPosition(Point(sp->getContentSize().width * 0.5f, sp->getContentSize().height * 0.5f));
/* 精灵作为Monster的表现,添加到Monster身上 */
this->addChild(sp);
/* 设置怪物Tag类型 */
this->setTag(ObjectTag_Monster);
/* 精灵作为Monster的表现,Monster本身没有大小,所以要设置一下大小 */
this->setContentSize(sp->getContentSize());
/* 刚体添加到Monster本身,而不是精灵身上 */
this->setPhysicsBody(body);
/* 设置坐标 */
this->setPosition(Point(getiPosX(), getiPosY()));
/* Monster加入到物理世界 */
parent->addChild(this);
}
示例10: onTouchBegan
bool BaseDemo::onTouchBegan(Touch* touch, Event* event)
{
auto location = touch->getLocation();
auto shapeArr = _world->getShapes(location);
PhysicsBody* body = nullptr;
for(auto& obj : shapeArr)
{
if((obj->getBody()->getTag() & DRAG_BODYS_TAG) != 0)
{
body = obj->getBody();
break;
}
}
if(body != nullptr)
{
Node* mouse = Node::create();
mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY));
mouse->getPhysicsBody()->setDynamic(false);
mouse->setPosition(location);
this->addChild(mouse);
PhysicsJointPin* joint = PhysicsJointPin::construct(mouse->getPhysicsBody(), body, location);
joint->setMaxForce(5000.0f * body->getMass());
_world->addJoint(joint);
_mouses.insert(std::make_pair(touch->getID(), mouse));
return true;
}
return false;
}
示例11: onContactBegin
bool PlayerComponent::onContactBegin(PhysicsContact& contact)
{
if (!is_endGame)
{
PhysicsBody *a = contact.getShapeB()->getBody();
if ((a->getCollisionBitmask() == OBSTACLE_ID) || (a->getCollisionBitmask() == SPIKESBORDER_ID))
{
is_onTouch = false;
is_fall = false;
is_endGame = true;
// display 'dialog box'
this->getOwner()->addChild(MyDialogBox::create(
{
Label::create("Your Score:", "Calibri", 30),
Label::create(StringUtils::toString(((ScoreCounterComponent*)this->getOwner()->getComponent(SCORECOUNTER_NAME))->getScore()), "Calibri", 120)
},
{
DialogBoxButton("TRY AGAIN", TextHAlignment::LEFT, CC_CALLBACK_1(GameScene::goToGameScene, ((GameScene*)this->getOwner()))),
DialogBoxButton("BACK TO\n MAIN MENU", TextHAlignment::RIGHT, CC_CALLBACK_1(GameScene::goToMainMenuScene, ((GameScene*)this->getOwner())))
}
), 10);
}
}
return false;
}
示例12: resetObject
void Physics::step(void) {
dynamicWorld->stepSimulation(1.f/60.f);
graphics->animate();
int numManifolds = dynamicWorld->getDispatcher()->getNumManifolds();
for (int i = 0; i < numManifolds; i++) {
btPersistentManifold* contactManifold = dynamicWorld->getDispatcher()->getManifoldByIndexInternal(i);
btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());
btBroadphaseProxy* obA_proxy = obA->getBroadphaseHandle();
btBroadphaseProxy* obB_proxy = obB->getBroadphaseHandle();
if (obA_proxy->m_collisionFilterGroup & obB_proxy->m_collisionFilterMask) {
if (obA_proxy->m_collisionFilterGroup == COL_PENGUIN && obB_proxy->m_collisionFilterGroup == COL_KILLBOX) {
PhysicsBody* object = reinterpret_cast<PhysicsBody*>(obA->getUserPointer());
resetObject(object);
} else if (obA_proxy->m_collisionFilterGroup == COL_PENGUIN && obB_proxy->m_collisionFilterGroup == COL_GOAL) {
nextStage();
graphics->playSound(2);
} else if (obA_proxy->m_collisionFilterGroup == COL_PENGUIN && obB_proxy->m_collisionFilterGroup == COL_CHECKPOINT) {
btVector3 checkpoint2 = obB->getWorldTransform().getOrigin();
btBoxShape* shape = reinterpret_cast<btBoxShape*>(obB->getCollisionShape());
checkpoint2 += btVector3(0, shape->getHalfExtentsWithoutMargin().y(), 0);
checkpoint2 += btVector3(0, 25, 0);
PhysicsBody* object = reinterpret_cast<PhysicsBody*>(obA->getUserPointer());
btRigidBody* body = object->getBody();
btVector3 translate = body->getCenterOfMassPosition();
if (checkpoint2 != checkpoint && translate.y() >= checkpoint2.y()) {
graphics->playSound(1);
checkpoint = checkpoint2;
}
}
}
}
}
示例13: CCLOG
bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
{
CCLOG("%s", "touch");
auto location = touch->getLocation();
auto arr = _scene->getPhysicsWorld()->getShapes(location);//从物理世界得到多边形
PhysicsBody *body = nullptr;
for (auto &obj : arr)
{
if ((obj->getBody()->getTag() & DRAG_BODYS_TAG) != 0) //得到刚体
{
body = obj->getBody();
break;
}
}
if (body != nullptr)
{
//创建一个刚体
Node *mouse = Node::create();
mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY));
mouse->getPhysicsBody()->setDynamic(false);
mouse->setPosition(location);
this->addChild(mouse);
body->setLinearDamping(0.0f);
//用图钉关节与点中刚体绑定 赋予力 可以拖动
PhysicsJointPin *joint = PhysicsJointPin::construct(mouse->getPhysicsBody(), body, location);
joint->setMaxForce(5000.0f * body->getMass());
_scene->getPhysicsWorld()->addJoint(joint);
_mouses.insert(std::make_pair(touch->getID(), mouse));
return true;
}
return false;
}
示例14: collisionBeginCallback
bool PhysicsWorld::collisionBeginCallback(PhysicsContact& contact)
{
bool ret = true;
PhysicsShape* shapeA = contact.getShapeA();
PhysicsShape* shapeB = contact.getShapeB();
PhysicsBody* bodyA = shapeA->getBody();
PhysicsBody* bodyB = shapeB->getBody();
std::vector<PhysicsJoint*> jointsA = bodyA->getJoints();
// check the joint is collision enable or not
for (PhysicsJoint* joint : jointsA)
{
if (std::find(_joints.begin(), _joints.end(), joint) == _joints.end())
{
continue;
}
if (!joint->isCollisionEnabled())
{
PhysicsBody* body = joint->getBodyA() == bodyA ? joint->getBodyB() : joint->getBodyA();
if (body == bodyB)
{
contact.setNotificationEnable(false);
return false;
}
}
}
// bitmask check
if ((shapeA->getCategoryBitmask() & shapeB->getContactTestBitmask()) == 0
|| (shapeA->getContactTestBitmask() & shapeB->getCategoryBitmask()) == 0)
{
contact.setNotificationEnable(false);
}
if (shapeA->getGroup() != 0 && shapeA->getGroup() == shapeB->getGroup())
{
ret = shapeA->getGroup() > 0;
}
else
{
if ((shapeA->getCategoryBitmask() & shapeB->getCollisionBitmask()) == 0
|| (shapeB->getCategoryBitmask() & shapeA->getCollisionBitmask()) == 0)
{
ret = false;
}
}
if (contact.isNotificationEnabled())
{
contact.setEventCode(PhysicsContact::EventCode::BEGIN);
contact.setWorld(this);
_eventDispatcher->dispatchEvent(&contact);
}
return ret ? contact.resetResult() : false;
}
示例15: Point
void HitMeScene::hitMeFire(Ref* sender)
{
Point velocity_delta = Point(CCRANDOM_0_1(), CCRANDOM_0_1()) * 300;
velocity_delta = (CCRANDOM_0_1() < 0.5f)? velocity_delta : -velocity_delta;
PhysicsBody* body = _hitItem->getPhysicsBody();
body->setVelocity(body->getVelocity() + velocity_delta);
body->setAngularVelocity(body->getAngularVelocity() + 5.0f * CCRANDOM_0_1());
}