本文整理汇总了C++中PhysicsSprite::setPhysicsBody方法的典型用法代码示例。如果您正苦于以下问题:C++ PhysicsSprite::setPhysicsBody方法的具体用法?C++ PhysicsSprite::setPhysicsBody怎么用?C++ PhysicsSprite::setPhysicsBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhysicsSprite
的用法示例。
在下文中一共展示了PhysicsSprite::setPhysicsBody方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: CCLOG
void Box2DTestLayer::addNewSpriteAtPosition(CCPoint p)
{
CCLOG("Add sprite %0.2f x %02.f",p.x,p.y);
CCNode* parent = getChildByTag(kTagParentNode);
//We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is
//just randomly picking one of the images
int idx = (CCRANDOM_0_1() > .5 ? 0:1);
int idy = (CCRANDOM_0_1() > .5 ? 0:1);
PhysicsSprite *sprite = new PhysicsSprite();
sprite->initWithTexture(m_pSpriteTexture, CCRectMake(32 * idx,32 * idy,32,32));
sprite->autorelease();
parent->addChild(sprite);
sprite->setPosition( CCPointMake( p.x, p.y) );
// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
sprite->setPhysicsBody(body);
}
示例2: addNewSpriteAtPosition
void HelloWorld::addNewSpriteAtPosition(CCPoint p)
{
CCLOG("Add sprite %0.2f x %02.f",p.x,p.y);
CCNode* parent = getChildByTag(kTagParentNode);
//We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is
//just randomly picking one of the images
int idx = (CCRANDOM_0_1() > .5 ? 0:1);
int idy = (CCRANDOM_0_1() > .5 ? 0:1);
PhysicsSprite *sprite = new PhysicsSprite();
sprite->initWithTexture(m_pSpriteTexture, CCRectMake(32 * idx,32 * idy,32,32));
sprite->autorelease();
parent->addChild(sprite);
sprite->setPosition( CCPointMake( p.x, p.y) );
sprite->setOpacity(32);
// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
sprite->setPhysicsBody(body);
// Create a rope joint so that the
// block will "swing" from the top of the
// scene.
// Calculate the local position of the
// top of screen in the local space
// of the ground box.
CCSize scrSize = CCDirector::sharedDirector()->getWinSize();
b2Vec2 groundWorldPos = b2Vec2((scrSize.width/2)/PTM_RATIO,(scrSize.height)/PTM_RATIO);
b2Vec2 groundLocalPos = m_pGround->GetLocalPoint(groundWorldPos);
// Now create the main swinging joint.
b2RopeJointDef jointDef;
jointDef.bodyA = m_pGround;
jointDef.bodyB = body;
jointDef.localAnchorA = groundLocalPos;
jointDef.localAnchorB = b2Vec2(0.0f,0.0f);
jointDef.maxLength = (groundWorldPos-body->GetWorldCenter()).Length();
jointDef.collideConnected = true;
world->CreateJoint(&jointDef);
// Now create a second/third rope to "constrain" the swing.
// These one will be attached to the side of the screen.
b2Vec2 groundWorldPos2 = b2Vec2(0,body->GetWorldCenter().y);
b2Vec2 groundLocalPos2 = m_pGround->GetLocalPoint(groundWorldPos2);
jointDef.localAnchorA = groundLocalPos2;
// Setting the length of the side rope...
// What we want to do here is use the distance from the center as
// the length of the rope. Then add length to it so that the box
// can have at least one full swing which ever side it is on (left or
// right half of the scene).
float32 distToCenter = (fabs(scrSize.width/2 - (body->GetWorldCenter().x)*PTM_RATIO))/PTM_RATIO;
jointDef.maxLength = distToCenter + (scrSize.width/2)/PTM_RATIO;
world->CreateJoint(&jointDef);
// Now for the last rope, other side of the scene.
// Now create a second/third rope to "constrain" the swing.
// These one will be attached to the side of the screen.
b2Vec2 groundWorldPos3 = b2Vec2((scrSize.width)/PTM_RATIO,body->GetWorldCenter().y);
b2Vec2 groundLocalPos3 = m_pGround->GetLocalPoint(groundWorldPos3);
jointDef.localAnchorA = groundLocalPos3;
world->CreateJoint(&jointDef);
}