cocos2dx+box2d:
Hello. I actually have a problem.
I did a bit of rope and she behaves normally. But if the object tie two ropes, it somehow as it stops abruptly. Can anyone understand why this is happening and can help? I recorded a video with two ropes, which shows that the object sharply ostanavliaetsya. And the video with one rope, where all is well:
One rope:http://youtu.be/Yh4x8qrgmgo
Two rope:
http://youtu.be/iRPVPLrC9ZQ
Because of what the object may stop so abruptly?
Add Rope:
GameScene::createRope(blk1,groundBody,blk1->GetLocalCenter(), cc_to_b2Vec(580, screen.height/1) ,1.0);
GameScene::createRope(groundBody,blk1,cc_to_b2Vec(380, screen.height/2), blk1->GetLocalCenter() ,1.1);
Create Rope:
void GameScene::createRope(b2Body *bodyA, b2Body *bodyB, b2Vec2 anchorA, b2Vec2 anchorB, float sag)
{
b2RopeJointDef jd;
jd.bodyA = bodyA;
jd.bodyB = bodyB;
jd.localAnchorA = anchorA;
jd.localAnchorB = anchorB;
jd.collideConnected = true;
// Max length of joint = current distance between bodies * sag
float32 ropeLength = (bodyA->GetWorldPoint(anchorA)- bodyB->GetWorldPoint(anchorB)).Length()*sag;
jd.maxLength = ropeLength;
// Create joint
b2RopeJoint *ropeJoint = (b2RopeJoint *)world->CreateJoint(&jd);
VRope *newRope = new VRope(ropeJoint, _ropeSpriteSheet);
_ropes.push_back(newRope);
}
http://youtu.be/Lchc1II3cjY - The ropes are arranged at the same height. Essentially the same object should swing up and down a bit
In fact, the rope taut, but the sprite does not look stretched, and therefore it seems that the object should swing. I do not know how to fix it ...
create Rope:
void VRope::createRope(const CCPoint& pointA, const CCPoint& pointB,float distance)
{
// 16; //12; //increase value to have less segments per rope, decrease to have more segments
int segmentFactor = 20;
numPoints = (int) distance/segmentFactor;
CCPoint diffVector = ccpSub(pointB,pointA);
float multiplier = distance / (numPoints-1);
antiSagHack = 0.1f; //HACK: scale down rope points to cheat sag. set to 0 to disable, max suggested value 0.1
for(int i=0;i<numPoints;i++) {
CCPoint tmpVector = ccpAdd(pointA, ccpMult(ccpNormalize(diffVector), multiplier*i*(1-antiSagHack)));
VPoint *tmpPoint = new VPoint();
tmpPoint->setPos(tmpVector.x, tmpVector.y);
vPoints.push_back(tmpPoint);
}
for(int i=0;i<numPoints-1;i++) {
VStick* tmpStick = new VStick(vPoints[i], vPoints[i+1]);
vSticks.push_back(tmpStick);
}
if(spriteSheet) {
for(int i=0;i<numPoints-1;i++) {
VPoint* point1 = vSticks[i]->getPointA();
VPoint* point2 = vSticks[i]->getPointB();
CCPoint stickVector = ccpSub(ccp(point1->x,point1->y),ccp(point2->x,point2->y));
float stickAngle = ccpToAngle(stickVector);
float f = spriteSheet->getTextureAtlas()->getTexture()->getPixelsHigh() / CC_CONTENT_SCALE_FACTOR();
CCRect r = CCRectMake(0, 0, multiplier, f);
CCSprite* tmpSprite = CCSprite::createWithTexture(spriteSheet->getTexture(), r);
ccTexParams params = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
tmpSprite->getTexture()->setTexParameters(¶ms);
tmpSprite->setPosition(ccpMidpoint(ccp(point1->x, point1->y), ccp(point2->x, point2->y)));
tmpSprite->setRotation(-1 * CC_RADIANS_TO_DEGREES(stickAngle));
spriteSheet->addChild(tmpSprite);
ropeSprites.push_back(tmpSprite);
}
}
}
if I increase the number of segments that the rope will be stretched, but not look beautiful. and yes I cut by joint, so cutting is not accurate ....
If you increase the number of segments (reduced number of points and the rope looks like a stick)
http://youtu.be/hn8ALt2P8pE
If you increase the number of segments (reduced number of points and the rope looks like a stick) And if you reduce the surprising number of segments, it is not really too sagging look. What am I doing wrong? All code on the ropes
http://github.com/Banck/Rope
Help me please
See Question&Answers more detail:
os