Scaling & flipping bodies

General discussion about the R.U.B.E editor
Post Reply
Eddy8555
Posts: 42
Joined: Sat Apr 27, 2013 3:01 pm

Scaling & flipping bodies

Post by Eddy8555 »

Is there a way to scale bodies through the rube loader machinations? I know how to scale a sprite, and I've seen the tutorial on how to destroy the shape and recreate it. But at first glance it seems a lot harder to scale a shape with multiple vertices, and all my rube objects have them. So is there a simpler way?

Also, in RUBEImageInfo there's a flip attribute, but setting it to true or false doesn't seem to have any effect. How to make it work?
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Scaling & flipping bodies

Post by iforce2d »

Cocos2d, right? It would be good if you could preface your topics with a mention of what OS/SDK etc you are using, or even tag the thread title with it to make it easier for people to figure out what is going on.

Scaling of bodies is really scaling of fixtures, since bodies have no real size. And scaling of fixtures really just amounts to moving the vertices. You can use the S key to scale things the same as for images.

oh... you're talking about in the loader, right. Well, you would have to iterate over all the bodies to be scaled, and iterate over all their fixtures and all the vertices of those fixtures, and transform them by some matrix. So I guess technically that's a 'yes', but a 'no' if you're looking for an easy way :p

iirc since the flip attribute does not change after loading like some other attributes of a sprite do (position, angle etc), the sample loader is only making use of the flip once, at the time of loading. The sprite color and scale are treated the same too.

I have a vague memory of leaving out the flip attribute from the very first cocos2d sample I uploaded, then I realized and amended it. If you got the source code during that time, you might have a version that ignores the flip setting. The relevant code is in the afterLoadProcessing method of RUBELayer.mm, you should have this section in there:

Code: Select all

        // these will not change during simulation so we can set them now
        [sprite setScale:img->scale / [sprite contentSizeInPixels].height * contentScaleFactor];
        [sprite setScaleX:[sprite scale] * img->aspectScale];
        [sprite setFlipX:img->flip];
        [sprite setColor:ccc3(img->colorTint[0], img->colorTint[1], img->colorTint[2])];
        [sprite setOpacity:img->colorTint[3]];
Eddy8555
Posts: 42
Joined: Sat Apr 27, 2013 3:01 pm

Re: Scaling & flipping bodies

Post by Eddy8555 »

The scaling was relatively painless...

Code: Select all


 double scaleBy = 1.1;
 if(scale) {
     if(scaleDown) {
         scaleBy = 0.9;
      }

 [touchedImgInfo->sprite setScale:[touchedImgInfo->sprite scaleX]*scaleBy];

 b2Fixture *f = touchedImgInfo->body->GetFixtureList();
     while(f != NULL) {
          if(f->GetType() == b2Shape::e_polygon) {
               b2PolygonShape *shape = (b2PolygonShape*)f->GetShape();
               int count = shape->GetVertexCount();

               b2Vec2 newVs[8]; // the max length
               for(int i=0; i<count; i++) {
                   newVs[i] = shape->GetVertex(i);
                   newVs[i].Set(newVs[i].x*scaleBy, newVs[i].y*scaleBy);
               }
               shape->Set(newVs, count);
          }
                    
         f = f->GetNext();
    }
                
    b2dJsonImage *img = json.getImageByName([touchedImgInfo->name UTF8String]);
    img->scale = img->scale * scaleBy;
}
Post Reply