读书人

Cocos2d-x Box2d札记:用VRope实现一个

发布时间: 2014-04-22 16:06:42 作者: rapoo

Cocos2d-x Box2d笔记:用VRope实现一个割绳子游戏

vrope是一个实现生动的绳子的类,很小,只有几K。

http://download.csdn.net/detail/cq361106306/4555483

然后我参照国外的一个cocos2d-iphone的例子做的cocos2d-x版本的

原文(英文)http://www.raywenderlich.com/14812/how-to-make-a-game-like-cut-the-rope-part-2

效果

Cocos2d-x Box2d札记:用VRope实现一个割绳子游戏

主要实现代码如下

创建只需要

//ropescreateRopeWithBody(groundBody,body1,b2Vec2(s.width * 0.15/PTM_RATIO, s.height * 0.8/PTM_RATIO),body1->GetLocalCenter(),1.1);createRopeWithBody(body1,groundBody,body1->GetLocalCenter(),b2Vec2(s.width * 0.85/PTM_RATIO, s.height * 0.8/PTM_RATIO),1.1);createRopeWithBody(body1,groundBody,body1->GetLocalCenter(),b2Vec2(s.width * 0.83/PTM_RATIO, s.height * 0.6/PTM_RATIO),1.1);


void HelloWorld::createRopeWithBody(b2Body *bodyA,b2Body *bodyB,b2Vec2 anchorA,b2Vec2 anchorB,float sag){b2RopeJointDef jd;jd.bodyA = bodyA;jd.bodyB = bodyB;jd.localAnchorA.Set(anchorA.x,anchorA.y);jd.localAnchorB.Set(anchorB.x,anchorB.y);// Max length of joint = current distance between bodies * sagfloat32 ropeLength = (bodyA->GetWorldPoint(anchorA) - bodyB->GetWorldPoint(anchorB)).Length() * sag;jd.maxLength = ropeLength;// Create jointb2RopeJoint *ropeJoint = (b2RopeJoint *)world->CreateJoint(&jd);VRope *newRope = new VRope(ropeJoint,ropeSpriteSheet);ropes.push_back(newRope);}

void HelloWorld::update(float dt){int32 velocityIterations = 8;int32 positionIterations = 1;// Instruct the world to perform a single step of simulation.world->Step(dt, velocityIterations, positionIterations);for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()){CCSprite *myActor = (CCSprite*)b->GetUserData();if (myActor){//Synchronize the AtlasSprites position and rotation with the corresponding bodymyActor->setPosition(CCPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO));myActor->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));}}// Update all the ropesstd::list<VRope*>::iterator pos;for (pos=ropes.begin();pos!=ropes.end();pos++){(*pos)->update(dt);(*pos)->updateSprites();}}


读书人网 >操作系统

热点推荐