本文整理汇总了C++中PhysicsBody::setGravityEnable方法的典型用法代码示例。如果您正苦于以下问题:C++ PhysicsBody::setGravityEnable方法的具体用法?C++ PhysicsBody::setGravityEnable怎么用?C++ PhysicsBody::setGravityEnable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhysicsBody
的用法示例。
在下文中一共展示了PhysicsBody::setGravityEnable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: configPipeRandom
void PipeSprite::configPipeRandom()
{
Size visiableSize = Director::getInstance()->getVisibleSize();
Vec2 visiableOrigin = Director::getInstance()->getVisibleOrigin();
/**********************config upper pipe********************************/
mUpperPipe->setPosition(Vec2(0, mPipeAccessHeight + 60));
upperPipeExpand->setPosition(mUpperPipe->getPosition().x, mUpperPipe->getContentSize().height);
upperPipeExpand->setScaleY((visiableSize.height - mUpperPipe->getPosition().y - mUpperPipe->getContentSize().height) / upperPipeExpand->getContentSize().height);
/**********************config upper pipe********************************/
/**********************config bottom pipe********************************/
mBottomPipe->setPosition(Vec2(0, mPipeAccessHeight - 60));
bottomPipeExpand->setPosition(0, -mBottomPipe->getPosition().y + mLandHeight);
bottomPipeExpand->setScaleY((mBottomPipe->getPosition().y - mLandHeight) / bottomPipeExpand->getContentSize().height);
/**********************config bottom pipe********************************/
/****************add upper physics body*******************/
if (mUpperPipe->getPhysicsBody() != nullptr)
{
mUpperPipe->removeComponent(mUpperPipe->getPhysicsBody());
}
PhysicsBody * newUpperPipeBody = PhysicsBody::createBox(Size(mUpperPipe->getContentSize().width,
visiableSize.height - mUpperPipe->getPosition().y),
PhysicsMaterial(1.0, 1.0, 0));
newUpperPipeBody->setPositionOffset(Vec2(0, (visiableSize.height - mUpperPipe->getPosition().y - mUpperPipe->getContentSize().height) / 2));
newUpperPipeBody->setGravityEnable(false);
newUpperPipeBody->setDynamic(false);
newUpperPipeBody->setContactTestBitmask(1);
newUpperPipeBody->setCollisionBitmask(2);
mUpperPipe->setPhysicsBody(newUpperPipeBody);
/****************add upper physics body*******************/
/****************add bottom physics body*******************/
if (mBottomPipe->getPhysicsBody() != nullptr)
{
mBottomPipe->removeComponent(mBottomPipe->getPhysicsBody());
}
PhysicsBody * newBottomPipeBody = PhysicsBody::createBox(Size(mBottomPipe->getContentSize().width,
mBottomPipe->getPosition().y - mLandHeight + mBottomPipe->getContentSize().height),
PhysicsMaterial(1.0, 1.0, 0));
newBottomPipeBody->setPositionOffset(Vec2(0, -(mBottomPipe->getPosition().y - mLandHeight) / 2));
newBottomPipeBody->setGravityEnable(false);
newBottomPipeBody->setDynamic(false);
newBottomPipeBody->setContactTestBitmask(1);
newBottomPipeBody->setCollisionBitmask(2);
mBottomPipe->setPhysicsBody(newBottomPipeBody);
/****************add bottom physics body*******************/
}
示例2: addPhysics
void Person::addPhysics()
{
auto size = (this->getBoundingBox()).size;
log("%lf %lf", size.width, size.height);
auto material = PhysicsMaterial(100.0f, 0.01f, 1.0f);
if(_type == TYPE::HERO) size.width /= 2;
PhysicsBody *body = PhysicsBody::createBox(Size(size.width,size.height),material);
// body->addShape(PhysicsShapeBox::create(Size(size.width,size.height),material));
body->setCategoryBitmask(_type);
if(_type == TYPE::MONSTER)
{
body->setCollisionBitmask(TYPE::MONSTER | TYPE::BRICK | TYPE::GROUND |
TYPE::TANGH | TYPE::BULLET | TYPE::BOSS | TYPE::PLANK);
body->setContactTestBitmask(TYPE::MONSTER | TYPE::HERO | TYPE::BRICK |
TYPE::TANGH | TYPE::BULLET | TYPE::BOSS);
}
else if(_type == TYPE::HERO)
{
body->setCollisionBitmask( TYPE::HERO | TYPE::GROUND | TYPE::TANGH | TYPE::PLANK);
body->setContactTestBitmask(TYPE::MONSTER | TYPE::HERO | TYPE::GROUND |
TYPE::TANGH | TYPE::BULLET | TYPE::TRAP | TYPE::BOSS |
TYPE::BULLETENEMY | TYPE::PLANK | TYPE::BUFF);
}
else if(_type == TYPE::BOSS)
{
body->setCollisionBitmask( TYPE::HERO | TYPE::GROUND | TYPE::BOSS | PLANK);
body->setContactTestBitmask( TYPE::HERO | TYPE::GROUND | TYPE::BOSS);
}
body->setDynamic(true);
body->setLinearDamping(0.0f);
body->setRotationEnable(false);
body->setGravityEnable(true);
this->setPhysicsBody(body);
}
示例3: initBody
void SpeedUpProtect::initBody()
{
auto s = getContentSize();
Vec2 vec[7] =
{
Vec2(0,0),
Vec2(s.width * 0.1, s.height * 0.4),
Vec2(s.width * 0.26, s.height * 0.72),
Vec2(s.width * 0.5, s.height),
Vec2(s.width * 0.74, s.height * 0.72),
Vec2(s.width * 0.9, s.height * 0.4),
Vec2(s.width,0),
};
PhysicsBody* body = PhysicsBody::createPolygon(vec, 7, PHYSICSBODY_MATERIAL_DEFAULT, Vec2(-s.width*0.5,-s.height*0.5));
body->setGravityEnable(false);
body->setRotationEnable(false);
body->setDynamic(false);
body->setMass(10000000);
body->setCategoryBitmask(SELF_SCOPE_CATEGORYBITMASK);
body->setCollisionBitmask(SELF_SCOPE_COLLISIONBITMASK);
body->setContactTestBitmask(SELF_SCOPE_CONTACTTESTBITMASK);
setPhysicsBody(body);
setInvincible(true);
m_ContactPro.hurts = -Ene_Plane_Hp_max - 10;
}
示例4: initPhyscsObject
void ShowLayer::initPhyscsObject(Rect* m)
{
for (int i = 0; i < 26; i++)
{
Sprite* sp = Sprite::create();
auto maskLayer = LayerColor::create(Color4B(0, 0, 255, 200));
maskLayer->setContentSize(m[i].size);
maskLayer->setAnchorPoint(Vec2(0, 0));
sp->addChild(maskLayer, 15);
PhysicsBody* playerBody = PhysicsBody::createBox(m[i].size, PHYSICSBODY_MATERIAL_DEFAULT);
playerBody->setDynamic(false);
//设置质量
playerBody->getShape(0)->setMass(100);
//设置物体是否受重力系数影响
playerBody->setGravityEnable(false);
playerBody->setCategoryBitmask(3);
playerBody->setContactTestBitmask(3);
playerBody->setCollisionBitmask(3);
sp->setContentSize(m[i].size);
sp->setPosition(Vec2(m[i].getMidX(), m[i].getMidY()));
sp->setAnchorPoint(Vec2(0.0, 0.0));
sp->setPhysicsBody(playerBody);
this->addChild(sp);
}
}
示例5: init
bool HeroSprite::init() {
bool bRet = false;
do {
CC_BREAK_IF(!Sprite::initWithSpriteFrameName("heroStand_0001.png"));
Size heroSize = this->getContentSize();
PhysicsBody *body = PhysicsBody::createBox(heroSize-Size(0,16), PhysicsMaterial(0, 0, 0));
body->setLinearDamping(0.0f);
body->setDynamic(true);
body->setGravityEnable(true);
body->setTag(TAG_HERO_PHYS_BODY);
body->setCategoryBitmask(1<<2);
body->setContactTestBitmask(1<<0 | 1<<1);
body->setMass(50);
body->setRotationEnable(false);
this->setPhysicsBody(body);
mState = STATE_IDLE;
mRunAnimate = NULL;
mSmokeRunAnimate = NULL;
bRet = true;
} while(0);
return bRet;
}
示例6: init
bool Monster::init()
{
mutex = PTHREAD_MUTEX_INITIALIZER;
status = DONE;
auto snakeTexture = TextureCache::sharedTextureCache()->addImage(
basic.getCString());
float dWidth = snakeTexture->getContentSize().width / cntWidth;
float dHeight = snakeTexture->getContentSize().height / cntHeight;
this->setAnchorPoint(Vec2(0.5,32/dHeight));
PhysicsBody *body = PhysicsBody::createBox(Size(60,60));
body->setPositionOffset(Vec2(0,64/4-dHeight/4));
body->setRotationEnable(false);
body->setGravityEnable(false);
body->setContactTestBitmask(0xffffffff);
body->setCollisionBitmask(0xffffffff);
this->setPhysicsBody(body);
this->setTag(MONSTER);
auto listener = EventListenerCustom::create("main_action",
CC_CALLBACK_1(Monster::receive,this));
_eventDispatcher->addEventListenerWithFixedPriority(listener, 1);
return true;
return false;
}
示例7: init
bool LandSprite::init()
{
if (!Sprite::init())
{
return false;
}
Size visiableSize = Director::getInstance()->getVisibleSize();
Vec2 visiableOrigin = Director::getInstance()->getVisibleOrigin();
Sprite * landSprite1 = Sprite::create("assets/land.png");
landSprite1->setAnchorPoint(Vec2(0, 0));
landSprite1->setPosition(Vec2(visiableOrigin.x, visiableOrigin.y));
landSprite1->setScaleX(visiableSize.width / landSprite1->getContentSize().width / 2);
Sprite * landSprite2 = Sprite::create("assets/land.png");
landSprite2->setAnchorPoint(Vec2(0, 0));
landSprite2->setPosition(Vec2(visiableOrigin.x + visiableSize.width / 2, visiableOrigin.y));
landSprite2->setScaleX(visiableSize.width / landSprite2->getContentSize().width / 2);
Sprite * landSprite3 = Sprite::create("assets/land.png");
landSprite3->setAnchorPoint(Vec2(0, 0));
landSprite3->setPosition(Vec2(visiableOrigin.x + visiableSize.width, visiableOrigin.y));
landSprite3->setScaleX(visiableSize.width / landSprite3->getContentSize().width / 2);
Sprite * landSprite4 = Sprite::create("assets/land.png");
landSprite4->setAnchorPoint(Vec2(0, 0));
landSprite4->setPosition(Vec2(visiableOrigin.x + visiableSize.width * 1.5, visiableOrigin.y));
landSprite4->setScaleX(visiableSize.width / landSprite4->getContentSize().width / 2);
Node * landAnimNode = Node::create(); // node hold the land animation
landAnimNode->addChild(landSprite1);
landAnimNode->addChild(landSprite2);
landAnimNode->addChild(landSprite3);
landAnimNode->addChild(landSprite4);
MoveBy * landForward = MoveBy::create(5, Vec2(-visiableSize.width, 0));
MoveBy * landBackward = MoveBy::create(0.001, Vec2(visiableSize.width, 0));
Sequence * moveSeq = Sequence::create(landForward, 0.001, landBackward,NULL);
landSprite1->runAction(RepeatForever::create(moveSeq));
landSprite2->runAction(RepeatForever::create(moveSeq->clone()));
landSprite3->runAction(RepeatForever::create(moveSeq->clone()));
landSprite4->runAction(RepeatForever::create(moveSeq->clone()));
this->addChild(landAnimNode);
this->setAnchorPoint(Vec2(0, 0));
this->setContentSize(Size(visiableSize.width, landSprite1->getContentSize().height));
PhysicsBody * landPhysicsBody = PhysicsBody::createBox(Size(visiableSize.width, landSprite1->getContentSize().height), PhysicsMaterial(1, 1, 0));
landPhysicsBody->setGravityEnable(false);
landPhysicsBody->setDynamic(false);
landPhysicsBody->setContactTestBitmask(1);
landPhysicsBody->setCollisionBitmask(2);
this->setPhysicsBody(landPhysicsBody);
return true;
}
示例8: initPhysics
void Ball::initPhysics(){
PhysicsBody *spriteBody = PhysicsBody::createCircle(this->getSprite()->getBoundingBox().size.width , PHYSICSBODY_MATERIAL_DEFAULT);
spriteBody->setDynamic(true);
spriteBody->getShape(0)->setRestitution(1.0f);
spriteBody->getShape(0)->setFriction(0.0f);
spriteBody->getShape(0)->setDensity(1.0f);
spriteBody->getShape(0)->setMass(100);
spriteBody->setGravityEnable(false);
spriteBody->setVelocity(this->velocity);
this->getSprite()->setPhysicsBody(spriteBody);
}
示例9: initPhysics
void Hero::initPhysics() {
PhysicsBody* pbody = nullptr;
pbody = PhysicsBody::createBox(getSize());
pbody->setDynamic(true);
pbody->setRotationEnable(false);
pbody->setMoment(0);
pbody->setMass(0.8);
pbody->setContactTestBitmask(1);
pbody->setGravityEnable(false);
getSprite()->setPhysicsBody(pbody);
mEnableGravity = true;
mEnableForceField = true;
}
示例10: createKnife
Chopper* Chopper::createKnife(float v, float y)
{
Chopper* knife = Chopper::create();
knife->retain();
knife->chopper = Sprite::createWithSpriteFrameName("knife4.png");
knife->chopper->setScale(0.6);
PhysicsBody *body = PhysicsBody::create();
body->setGravityEnable(true);
//body->setGravityEnable(false);
body->setVelocity(Vec2(0, -v));
knife->chopper->setPhysicsBody(body);
knife->chopper->setPosition(Vec2(410, y));
return knife;
}
示例11: init
bool Rock::init()
{
if (!Sprite::initWithFile("rock.png"))
{
return false;
}
PhysicsBody* rockBody = PhysicsBody::createBox(this->getContentSize(), PhysicsMaterial(DEFAULT_ROCK_MATERIAL));
rockBody->setDynamic(false);
rockBody->setContactTestBitmask(ROCK_CONTACT_MASK);
rockBody->setCategoryBitmask(ROCK_CATEGORY);
rockBody->setCollisionBitmask(ROCK_COLLISION_MASK);
rockBody->setGravityEnable(false);
this->setPhysicsBody(rockBody);
return true;
}
示例12: addBallObstacle
/*
auto ball = Sprite::create("Ball.png");
PhysicsBody *ballbody = PhysicsBody::create();
ballbody->addShape(PhysicsShapeCircle::create(BIRD_RADIUS+5));
ballbody->setCategoryBitmask(ColliderTypeBird);
ballbody->setCollisionBitmask(ColliderTypeLand | ColliderTypePip);
ballbody->setContactTestBitmask(1);
ballbody->setDynamic(true);
ballbody->setLinearDamping(0.0f);
ballbody->setGravityEnable(true);
ball->setPhysicsBody(ballbody);
auto visibleSize = Director::getInstance()->getVisibleSize();
ball->setPosition(bird->getPositionX()+2*20,visibleSize.height);
addChild(ball);
*/
void GameLayer::addBallObstacle(float dt)
{
auto ball = Sprite::create("Ball.png");
PhysicsBody *ballbody = PhysicsBody::create();
ballbody->addShape(PhysicsShapeCircle::create(BIRD_RADIUS+5));
ballbody->setCategoryBitmask(ColliderTypeBall);
ballbody->setCollisionBitmask(ColliderTypeLand | ColliderTypePip | ColliderTypeBird | ColliderTypeBall);
ballbody->setContactTestBitmask(ColliderTypeBird);
ballbody->setDynamic(true);
ballbody->setLinearDamping(0.0f);
ballbody->setGravityEnable(true);
ball->setPhysicsBody(ballbody);
auto visibleSize = Director::getInstance()->getVisibleSize();
ball->setPosition(bird->getPositionX()+2*20,visibleSize.height);
addChild(ball);
}
示例13: init
bool Witch::init()
{
mWitch = CCSprite::createWithSpriteFrameName(Tex::Player_0);
this->addChild(mWitch);
CCLOG("Witch Width:%f", mWitch->getContentSize().width);
mWitch->setPosition(0, 0);
PhysicsBody *body = PhysicsBody::create();
body->setTag(1);
this->setPhysicsBody(body);
body->addShape(PhysicsShapeBox::create(Size(mWitch->getContentSize().width, mWitch->getContentSize().height)));
body->setDynamic(true);
body->setLinearDamping(0.0f);
body->setGravityEnable(false);
return true;
}
示例14: switch
BoundWall::BoundWall(WallType type, Size screenSize)
{
PhysicsBody* body = nullptr;
switch (type)
{
case UP:
this->setAnchorPoint(Vec2(0.5f, 0));
this->setPosition(Vec2(screenSize.width / 2, screenSize.height));
body = PhysicsBody::createBox(Size(screenSize.width,5), PhysicsMaterial(1, 0, 0));
break;
case DOWN:
this->setAnchorPoint(Vec2(0.5f, 1));
this->setPosition(Vec2(screenSize.width / 2, 0));
body = PhysicsBody::createBox(Size(screenSize.width, 5), PhysicsMaterial(1, 0, 0));
break;
case LEFT:
this->setAnchorPoint(Vec2(1, 0.5f));
this->setPosition(Vec2(0, screenSize.height / 2));
body = PhysicsBody::createBox(Size(5, screenSize.height), PhysicsMaterial(1, 0, 0));
break;
case RIGHT:
this->setAnchorPoint(Vec2(0, 0.5f));
this->setPosition(Vec2(screenSize.width, screenSize.height / 2));
body = PhysicsBody::createBox(Size(5, screenSize.height), PhysicsMaterial(1, 0, 0));
break;
default:
break;
}
body->setGravityEnable(false);
body->setDynamic(false);
body->setTag(Tags::GROUND);
body->setCollisionBitmask(true);
body->setContactTestBitmask(true);
this->setPhysicsBody(body);
}
示例15: callbackC
void LevelFifteen::callbackC(Node* sender)
{
auto visibleSize = Director::getInstance()->getVisibleSize();
auto pictureName = 1 + rand() % 27;
auto str = String::createWithFormat("%d.png", pictureName)->getCString();
auto ballTwo2 = Sprite::createWithSpriteFrameName(str);
ballTwo2->setPosition(Vec2(visibleSize.width / 2.0f, visibleSize.height - 90));
PhysicsBody* ballBodyTwo = nullptr;
if (pictureName <= 9)
{
ballBodyTwo = PhysicsBody::createBox(ballTwo2->getContentSize(), PHYSICSBODY_MATERIAL_DEFAULT);
}
else if (pictureName > 9 && pictureName <= 18)
{
ballBodyTwo = PhysicsBody::createCircle(ballTwo2->getContentSize().width / 2.0f, PHYSICSBODY_MATERIAL_DEFAULT);
}
else if (pictureName > 18 && pictureName <= 21)
{
Vec2 vec[] = {
Vec2(-22.50000, 22.00000),
Vec2(24.00000, 10.00000),
Vec2(-10.00000, -24.00000)
};
ballBodyTwo = PhysicsBody::createPolygon(vec, 3);
}
else if (pictureName > 21 && pictureName <= 24)
{
Vec2 vec[] = {
Vec2(-15.50000, 15.00000),
Vec2(15.50000, 6.00000),
Vec2(-7.00000, -15.50000)
};
ballBodyTwo = PhysicsBody::createPolygon(vec, 3);
}
else if (pictureName > 24 && pictureName <= 27)
{
Vec2 vec[] = {
Vec2(-8.00000, 8.00000),
Vec2(7.50000, 3.00000),
Vec2(-4.00000, -7.50000)
};
ballBodyTwo = PhysicsBody::createPolygon(vec, 3);
}
//是否设置物体为静态
//ballBodyTwo->setDynamic(false);
ballBodyTwo->getShape(0)->setRestitution(1.0f);
ballBodyTwo->getShape(0)->setFriction(0.0f);
ballBodyTwo->getShape(0)->setDensity(1.0f);
ballBodyTwo->setGravityEnable(false);
ballBodyTwo->setCategoryBitmask(2);// 分类掩码
ballBodyTwo->setCollisionBitmask(1 | 2 | 4 | 8);// 碰撞掩码
ballBodyTwo->setContactTestBitmask(1 | 8);// 接触测试掩码
ballBodyTwo->addMass(1000.0f);
int flag = CCRANDOM_0_1() * 2;
auto num = 0;
switch (flag)
{
case 0:
num = -1;
break;
case 1:
num = 1;
break;
default:
break;
}
auto force1 = Vec2(CCRANDOM_0_1()*10000.0f*num, -300000.0f);
ballBodyTwo->applyImpulse(force1);
ballTwo2->setPhysicsBody(ballBodyTwo);
this->addChild(ballTwo2);
}