Auto-change the physics of an object after a change of his size

Hello everyone, I am new to Corona and I am trying to create my first game, can somebody answer to 

this question here, check the link. I posted it on Stackoverflow, if it is not ok tell me to rewrite it here.

ON9Qe.png

SaibC.png

Hi @avraammauridis,

Scaling doesn’t change the size of the physics body, only the image representation of the object. So, you’ll need to remove and re-create the body (or, create the body 50% the size in the start if you know in advance that the scale will be 0.5). You can rotate a physics body, of course, and the body will change along with the rotation… but scaling will not mesh.

Also, I should note, a common practice of users new to Corona is to move “groups” of physics objects by placing them all inside a display group and moving the group’s X/Y coordinates as a unit. This will not work with physics bodies and collisions, because the Box2D calculations work with the understanding that the objects all share a common 0,0 reference point in the world. So, if you want to move groups of physics bodies around, just note this behavior.

Take care,

Brent Sorrentino

Hi @avraammauridis,

Scaling doesn’t change the size of the physics body, only the image representation of the object. So, you’ll need to remove and re-create the body (or, create the body 50% the size in the start if you know in advance that the scale will be 0.5). You can rotate a physics body, of course, and the body will change along with the rotation… but scaling will not mesh.

Also, I should note, a common practice of users new to Corona is to move “groups” of physics objects by placing them all inside a display group and moving the group’s X/Y coordinates as a unit. This will not work with physics bodies and collisions, because the Box2D calculations work with the understanding that the objects all share a common 0,0 reference point in the world. So, if you want to move groups of physics bodies around, just note this behavior.

Take care,

Brent Sorrentino

How do I scale the physics body then?

function generateCrate() local crate = display.newImage("crate.png"); crate.x = display.contentCenterX + math.random(-200,200); crate.y = -50; crate.rotation = math.random(1,360); crate:scale(1.5,1.5); --Scaling only scales image doesnt scale physics body for some reason physics.addBody(crate, { density = 2.5, friction = .2, bounce = .1 }); print("generating crate"); end

Ok so i called the displayObject’s scale method before I created the physicsBody. However, the physicsBody still didnt scale properly

What did I do wrong this time?

Hi @atrizhong,

In this case, Box2D is still considering the pre-scaled size, but you can work around this by using a custom “shape” body instead of relying on auto-trace (in this case it’s still a basic square, just defined as a shape with the four corners). So, it might look like this:

[lua]

local crate = display.newImage(“crate.png”);

crate.x = display.contentCenterX + math.random(-200,200);

crate.y = -50;

crate.rotation = math.random(1,360);

local scaleX,scaleY = 1.5,1.5;

crate:scale(scaleX,scaleY);

local nw, nh = crate.width*scaleX*0.5, crate.height*scaleY*0.5;

physics.addBody(crate, { density = 2.5, friction = .2, bounce = .1, shape={-nw,-nh,nw,-nh,nw,nh,-nw,nh} });

print(“generating crate”);

[/lua]

If you’re not already familiar with Box2D rules in Corona, remember that all shapes must have their points declared in clockwise order, and concave bends are not allowed. Just a piece of advice as you move forward with your projects. :slight_smile:

Happy New Year,

Brent

How do I scale the physics body then?

function generateCrate() local crate = display.newImage("crate.png"); crate.x = display.contentCenterX + math.random(-200,200); crate.y = -50; crate.rotation = math.random(1,360); crate:scale(1.5,1.5); --Scaling only scales image doesnt scale physics body for some reason physics.addBody(crate, { density = 2.5, friction = .2, bounce = .1 }); print("generating crate"); end

Ok so i called the displayObject’s scale method before I created the physicsBody. However, the physicsBody still didnt scale properly

What did I do wrong this time?

Hi @atrizhong,

In this case, Box2D is still considering the pre-scaled size, but you can work around this by using a custom “shape” body instead of relying on auto-trace (in this case it’s still a basic square, just defined as a shape with the four corners). So, it might look like this:

[lua]

local crate = display.newImage(“crate.png”);

crate.x = display.contentCenterX + math.random(-200,200);

crate.y = -50;

crate.rotation = math.random(1,360);

local scaleX,scaleY = 1.5,1.5;

crate:scale(scaleX,scaleY);

local nw, nh = crate.width*scaleX*0.5, crate.height*scaleY*0.5;

physics.addBody(crate, { density = 2.5, friction = .2, bounce = .1, shape={-nw,-nh,nw,-nh,nw,nh,-nw,nh} });

print(“generating crate”);

[/lua]

If you’re not already familiar with Box2D rules in Corona, remember that all shapes must have their points declared in clockwise order, and concave bends are not allowed. Just a piece of advice as you move forward with your projects. :slight_smile:

Happy New Year,

Brent