Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Auto-change the physics of an object after a change of his size
Started by avraammauridis Jun 04 2013 11:55 PM

- - - - -
4 replies to this topic
physics scale
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

avraammauridis

[GLOBAL: userInfoPane.html]
avraammauridis
  • Observer

  • 8 posts
  • Corona SDK

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



[TOPIC: post.html]
#2

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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



[TOPIC: post.html]
#3

atrizhong

[GLOBAL: userInfoPane.html]
atrizhong
  • Enthusiast

  • 95 posts
  • Corona SDK

How do I scale the physics body then?



[TOPIC: post.html]
#4

atrizhong

[GLOBAL: userInfoPane.html]
atrizhong
  • Enthusiast

  • 95 posts
  • Corona SDK

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?



[TOPIC: post.html]
#5

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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:
 
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");
 
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. :)
 
Happy New Year,
Brent



[topic_controls]
[/topic_controls]