PhysicsBody SCALE Down: Cocos2d-x

@SonarSystems

I’ve created a physics body using physicsbOdy Editor which gives file in json format.

I have scaled down my sprites suppose 0.5,
how shall I scale down my physics body attached to it…
I am not able to do it as the function setScale available for it is protected function
virtual void setScale (float scale)
Also, I cannot delete and recreate physicsBody because it is not a circle or rectangle whose sisze is simply length or radius. So deleting won’t help me.
And this is highly needed when we are creating for multiple screens…

HOW TO ACHIEVE IT IF I’VE SCALED DOWNMY SPRITE!!
Infact, why doesnot it is automatically handled :smiley: I mean what will large physics body do alone when sprite is scaled down!!

Does it not work, as the json files coordinates are normalised.

The scale of a physics body is normally independent of the scale of the sprites that represent it on screen.

e.g.

Your physics ball is 1 meter across - and on the iPhone 4 you draw it as 32 pixels diameter.
On the iPhone 6 you may need to draw it as 64 pixels diameter to make it appear the same size on the screen - but the physics body is still 1m diameter.

The ratio between the two is usually held in PTM_RATIO i think - so when running on one device this value may be 32, on the other it would be 64.

This works for different screen resolutions on different devices - it does not handle the case where you want an individual object to scale in-game.

What I think is confusing is the PhysicsEditor uses pixels to define the physics body size which (IMHO) is confusing.

@SonarSystems
Hey, it didn’t work. I mean it was just a statement whose autocomplete was given… So, I cannot mistake there… I hope :smiley:
@Maxxx
So how shall I do that… It means that Can I not develop multi -screen resolution games using physicsBody Editor just because I cannot scale it according to my scale ratio?

Or is there any other way to make custom physics body which I can scale up and down depending upon my scale factor!! Don’t say Physics Editor(Codeweb) it is not free :smiley:

PhysicsBody has setScale methods but they are deprecated, try them

Hey, thanks for replying…Did it work?
Because I tried it at the first. It didn’t work so I thought of asking here!!
I’ll try it asap

Thanks :smile:

We haven’t tried it, let us know your progress.

I tried it… It did nothing… Not even error

Instead of scaling it try setting the content size of the sprite.

Hey thanks a lot, it worked… setContentSize
BUT the only problem I faced is that it has shifted the position of sprite on the screen while keeping its physicsBody as it is meant to be. I don’t know why it change the position of the sprite alone, even when I’ve set any kind of anchor.
Moreoever, it has just reduced the size of physicsBody and positioned it as it was supposed to be.

CAN SOMEONE CHECK IT HOW TO FIX IT? I AM NOT ABLE TO DO IT EVEN BY CHANGING THE ANCHORS OF THE SPRITE TO WHICH THE BODY IS ATTACHED???

Thanks a lot

Hey… with your last suggestion, I was able to scale down physics body created by physiscBody editor.
In the your physics tutorials, I followed it and made a circle sprite around a round CloseNormal.png
Here is my progress and doubts alongwith!!!

  1. I create 2 physics bodies- one using physicsBodyEditr and other using createCircle(the way u told in tutorials)
  2. I was able to scale down the one I was created using physicsBodyEditor and changed the anchor point so that it could align the physisc body exactly on my sprite…
  3. But for circle physics Body… suppose its sprite1 and its physics body is sprite1Body(which is circle physics body)
    There is not effect of setting anchor point for sprite1 to whatever

Hello

We don’t quite understand the question, could you elaborate please.

Regards
Sonar Systems

@SonarSystems

Oh yes… here is my all code for understanding. Note: the values carefully so that you would be able to explain me better.
Now, I am able to achieve what I wanted in the NOWs code I would like you to focus on BEFORE code of s2 and compare it with other code.

Here is the code to give you better picture:

For s1

BEOFRE: // This below code was setting the s1Body a little shifted to left and below of the sprite s1 So I had to change anchor Point of s1(see NOW code of s1)
And also the position of the s1 is according to s1->setPosition(Point(visibleSize.width/2, visibleSize.height/3));


auto s1= Sprite::create("s1.png");
s1->setPosition(Point(visibleSize.width/2, visibleSize.height/3));
s1->setScale(0.5);

//NOTE: by deafult anchor is set to 0.5,0.5, so no need to change it … NO PROBLEM NOW FOR s1

MyBodyParser::getInstance()->parseJsonFile(“s1.json”);
auto s1Body = MyBodyParser::getInstance()->bodyFormJson(s1,“Name”,PhysicsMaterial(10,0.2,1));
s1->setPhysicsBody(s1Body);


NOW: // Exactly what I wanted . NO PROBLEM WITH THIS CODE


auto s1= Sprite::create("s1.png");

// Dividing it by 8 is weird by it actually helped
s1->setPosition(Point(visibleSize.width/2-s1->getContentSize().width/8, visibleSize.height/3));
s1->setScale(0.5);
s1->setAnchorPoint(Vec2(0.25,0.25));
s1->setContentSize(Size(s1->getContentSize().width/2,s1->getContentSize().height/2));

MyBodyParser::getInstance()->parseJsonFile("s1.json");
auto s1Body = MyBodyParser::getInstance()->bodyFormJson(s1,"Name",PhysicsMaterial(10,0.2,1));
 s1->setPhysicsBody(s1Body);

For s2


BEFORE: //This code is placing s2Body a shifted from the center of s2 and nothing is happening even if I change setAnchor to anything
// I WANT TO MAKE THIS WORK THE WAY IT WORKED FOR s1


auto s2= Sprite::create("s2.png");
s2->setPosition(Point(visibleSize.width/2, 3*visibleSize.height/4));
s2->setScale(0.5);

// s2->setAnchorPoint(Vec2(5,5)); // Uncomment and set it to Anything and it won’t change even a single thing on my screen

s2->setContentSize(Size(s2->getContentSize().width/2,s2->getContentSize().height/2));

// NOTE: I am dividing by 2 because it is radius and although I’ve set scale of s2 to half but I’ve set mycontentSize to half too.
// So, overall effect is that it is giving me a circle of same size as of scaled down s1.

auto s2Body= PhysicsBody::createCircle(s2->getContentSize().width/2,PhysicsMaterial(0,1,0));
s2->setPhysicsBody(s2Body);


NOW: //Natural way of setting a physics body and it worked to exactly what I want


auto s2= Sprite::create("s2.png");
s2->setPosition(Point(visibleSize.width/2, 3*visibleSize.height/4));
s2->setScale(0.5);

//Be deafault achor for s2 is 0.5,0.5 so no need to change

// I am dividing by 4 because it is radius and also I’ve already reduced the s2 scale to 0.5

auto s2Body= PhysicsBody::createCircle(s2->getContentSize().width/4,PhysicsMaterial(0,1,0));
s2->setPhysicsBody(s2Body);

NOTE: In BEFORE CODE OF s2 The position of s2Body is appearing according to
s2->setPosition(Point(visibleSize.width/2, 3*visibleSize.height/4)); but the actual sprite s2 is little shifted to right and above And settingAnchoring is not letting change anything. I think setAnchor is working for s1 but not s2 :smiley: LOL

Thanks again… :smile:

Still don’t understand the issue

@SonarSystems
I actually wanted to say that creating body using json and using createCircle gave me different experience. Why!! Here is the reason.
I created sprite and attached physics body from json to it and then set scale and content size of sprite half so that both physics body and sprite could be reduced to half of their original size. BUT i had to change the anchor point of this sprite to 0.25,0.25 to set the physics body exactly at the center of the sprite because the physics body got shifted from its original location which is supposed to be the center of the sprite. That might have happened because setting content size to half would have also changed the default anchor of 0.5,0.5 to 0.25,0.25 that is half

BUT BUT… I created a new sprite, attached physics body made by cp…Circle and set the content size and scale of the sprite to half so that both could of their half of their original size. I got the size as half. NOW, whatever I do to anchor of this sprite, no change happens. I mean neither sprite moves nor its physics body… BUT THIS EXACT THING HAPPENED IN PREVIOUS SPRITE CASE!! So, with this method I am not able to set the physics body at the center of this sprite.

Anyways, thanks for spending such long time on my post… Although, I was able to achieve it through experimenting but I didn’t know why this issue happened…

If you still have issues with circle bodies just create the circle body using the Physics Editor, not the ideal solution but it is a solution.

Regards
Sonar Systems

Sorry for not replying for so long- I don’t recall seeing an email…

For any reasonable sized game I would always take the approach that the physics world is a model of the game which you are going to draw somehow.

So, I create my physics world add on my physics bodies and let the physics engine do its stuff.

Each frame, I look to where my physics objects are, decide if they need to be drawn (i.e. is my viewing window showing an area of the physics world that is inhabited by that physics body) and, if they do, I draw them at the scale appropriate to the device I am using.

I never* scale a physics body.

An example
I have a ball sitting in a room. The room is a box 10m square. the ball is 1m diameter.
I create the physics bodies, set up the physics world.

When I come to draw the ball, I know the resolution of my device, so I know the sprite representing the ball, and I calculate where to draw it. Say on the iPad I am using 32 pixels to represent 1m - so easy peasy I am using my 32 pixel sprite

On another device I may use 64 or 128 pixels to represent a meter. Again, easy peasy - I just use the appropriate sprite (one that is 64 or 128 pixels in diameter) and it all just works.

I guess it’s a matter of perspective; I think of it as being a physics model of the ‘real’ world that I then need to draw on some screen or other - I may just draw a portion of the physics world, and I may scale my sprites however I want to best represent the world on that device.

What PhysicsEditor and the Physics Sprite in cocos both encourage (wrongly IMHO in all but the simplest of games) is for the developer to treat things at a pixel level with a physics ‘helper’ - that is, adding a physics body to a sprite, rather than using a sprite to represent a physics body

Separation of the model from the view is a Good Thing - for example in PooperPig I zoom my view in and out all the time - but this does not affect my physics objects or world - just the view of it I portray to the user.

  • the only exception is if an object in my world needs to change size - for example, if the ball needs to shrink relative to the rest of the world, I would need to adjust the size of the physics object. I haven’t ever come across the need to do so - but if I did I may well opt to simply have a second physics object defined at the alternate size. (together with a second sprite, of course)