Collision Problems

I have created shape and when you press once it gets bigger, and so on 5 times. But after it gets bigger the collision works only from a middle of shape.

here is my code

-- add shape touches her  
local i = 1  
local function shapePressed()  
 if i \> 5 then return end   
 transition.to(shape, {time=10, xScale = shape.xScale + 0.3, yScale = shape.yScale + 0.3 })  
 i = i+1  
end  
  
-- add collision to the shapes  
local function onCollision (self , event)  
 if (event.phase == "began" ) then  
  
 end  
end  
  
 -- add shape  
  
 shape = display.newImage("levels/shape.png", 45,45)  
 shape.x = 240  
 shape.y =140  
 shape.collison = onCollision  
 group:insert(shape)  
 -- event listeners   
 shape:addEventListener("tap", shapePressed)  
 shape:addEventListener("collision", shape)  
 ground:addEventListener("collision", ground)  
  
 --add physics bodys  
 physics.addBody(ground, "static",{density = 3.0 , friction = 0.5, bounce = 0.1 } )  
 physics.addBody(shape, {density = 3.0 , friction = 0.5, bounce = 0} )  
  

Is something wrong here. Please help [import]uid: 11559 topic_id: 20036 reply_id: 320036[/import]

Scaling the image will not make the physics body bigger unfortunately.

Each time the image gets bigger you should remove and readd the body.

Make sense?

Peach :slight_smile: [import]uid: 52491 topic_id: 20036 reply_id: 78242[/import]

Thanks. It does make sense, but i have no idea how to implement it. So ill look around. Thanks [import]uid: 11559 topic_id: 20036 reply_id: 78360[/import]

Would you be able to help out with the code. Thanks [import]uid: 11559 topic_id: 20036 reply_id: 78433[/import]

Hi Ayena,

Just use “physics.removeBody( body )” followed by “physics.addBody()”. This should be allowed within the same function call, but it’s possible that Corona will fuss if you try that in the same step (same game cycle). There are many actions involving physics bodies which can’t be applied in the exact same step in which they occurred… i.e. if you have 2 bodies colliding, Corona doesn’t want you to change one of those bodies (physically) in the exact same collision step. This is actually more of a Box2D limitation versus a Corona one, but one that we developers must adhere to. The easiest and recommended solution is to perform your action using a very small delayed timer of even 10 milliseconds, which will cause Corona to perform that action in the next game cycle.

So, for your purposes, you should be able to remove the original physics body in the first step and resize the object, then if necessary (if you receive a warning in the console), add the new body after a small timer. The new body should automatically resize to the new bigger body, but of course you can set the parameters yourself.

Brent Sorrentino
Ignis Design [import]uid: 9747 topic_id: 20036 reply_id: 78435[/import]

Hey Ayena, apologies, the time difference can cause a bit of a delay in response sometimes - luckily Ignis is spot on in his instructions :slight_smile: [import]uid: 52491 topic_id: 20036 reply_id: 78446[/import]

Thank you very much for your help. I managed do implement it with your help. But here is how it works now (touch 1 works, touch 2 not, touch 3 works, touch 4 not, touch 5 works) I have no idea why. [import]uid: 11559 topic_id: 20036 reply_id: 78519[/import]

Can you please post your current code pertaining to the tap and listening functions? That’s odd behavior but there’s probably a simple reason for it…
[import]uid: 9747 topic_id: 20036 reply_id: 78561[/import]

Hey thanks
here is the code

local i = 1  
local function shapePressed()  
 physics.removeBody(shape)  
 physics.addBody(shape)  
if i \> 5 then return end   
 transition.to(shape, {time=10, xScale = shape.xScale + 0.3, yScale = shape.yScale + 0.3 })   
 i = i+1   
end  
  
 -- Add rope to the scene  
 rope =display.newImageRect ( "levels/rope.png", 480, 9)  
 rope.x = 240  
 rope.y = 110  
 group:insert(rope)  
  
-- Squere shape event listener  
 shape:addEventListener("tap", shapePressed)  
 -- Squere shape physics body  
 physics.addBody(shape, {density = 3.0 , friction = 0.5, bounce = 0.3} )  

When i turn physics.setDrawMode(“hybrid”) i can clearly see that physics body is not changing size. I must be doing something wrong . Im new to this
Thanks again
[import]uid: 11559 topic_id: 20036 reply_id: 78563[/import]

Seems like you’re close to getting this to work. I would change your “shapePressed” function to this…

local i = 1  
local function shapePressed()  
  
 physics.removeBody(shape)  
 if i \> 5 then return end  
  
 local function addNewBody()  
 physics.addBody(shape, {density = 3.0 , friction = 0.5, bounce = 0.3} )  
 end  
  
 transition.to(shape, {time=100, xScale = shape.xScale + 0.3, yScale = shape.yScale + 0.3, onComplete=addNewBody })  
 --if you want an "instant snap" scaling, not a transition, use the following 3 lines instead...  
 --shape.xScale = shape.xScale + 0.3  
 --shape.yScale = shape.yScale + 0.3  
 --timer.performWithDelay( 10, addNewBody )  
 i = i+1   
end  

This approach accomplishes a couple things which you might like. First of all, it allows you to increase the time of the scaling transition to provide a smooth scaling effect (if you want this). Then, adding the “onComplete” parameter to the transition call delays the addition of the new physics body until after it scales up. This should give Box2D plenty of time to establish the new body size after removing the first one.

Depending on what happens to your physics bodies after they reach the “maximum size” (5 increments up?), you might also consider removing the tap listener entirely when it reaches that maximum size. If the body can never again reduce in size (nor increase any further), then the tap listener becomes pointless and should be removed to improve your code slightly.

Hope this helps!
Brent Sorrentino
Ignis Design [import]uid: 9747 topic_id: 20036 reply_id: 78578[/import]

Thanks man, but not working.

I get no error, but i get this warning "WARNING: physics.removeBody() given a display object that is not a physics object.
" [import]uid: 11559 topic_id: 20036 reply_id: 78580[/import]

Can you see a body being added or removed in the “hybrid” mode? Does the error occur on the first tap of the object, or second? [import]uid: 9747 topic_id: 20036 reply_id: 78618[/import]

When using hybrid mod I can clearly see that body is not changing at all. I used print function to see if it executing add and remove body. It does. I really don’t know what’s wrong with it. I have already spend so much time and still nothing. Thanks for the help man. [import]uid: 11559 topic_id: 20036 reply_id: 78619[/import]

I tried it myself, and the same result. I would file this as bug in the Bug Reports forum, because Corona should be re-creating the new physics body using the updated (scaled) object size. This obviously works when the body is first created, but for some reason Corona is retaining the same physics size after the body is removed. It also doesn’t seem to matter whether the body is removed before or after the scale; the result is the same.

In the meantime, I have a potential workaround for you. Are your physics objects rectangles or circles? If so, you can specifically set the new physics body size when it’s added. For a rectangle shape, you’ll need to use the “shape” definition parameters, setting the 4 corners to the new size. For a circle it would be easier; you simply calculate the new size and specify that in the new “radius” parameter.

Note that when you create a “default” rectangular physics body in Corona, it simply uses the object’s coordinates to draw a box around that shape, thus bounding the object in a fitted rectangle. You can achieve the exact same thing by defining those coordinates using a Box2D “shape”, but typically it’s not necessary. In your scaling case it would be necessary.

For example, you might have a square physics shape with corner coordinates (in relation to the objects center point):
Upper-left X/Y: -10,-10
Upper-right X/Y: 10,-10
Lower-left X/Y: -10,10
Lower-right X/Y: 10,10

So then assume the NEW body size is scaled up by 120%. Those new coordinates will become:
-12,-12
12,-12
-12,12
12,12

And you can set those new coordinates using the “shape={}” parameter when you create the new physics body. Workaround achieved! Of course, if your physics bodies are complex shapes like pentagons or even triangles, then it all becomes immensely more complex, but there would be a math formula to recalculate the new bounding coordinates (I just don’t want to ponder it now, ha ha).

Brent Sorrentino [import]uid: 9747 topic_id: 20036 reply_id: 78628[/import]

Thanks man. I have a work arround now, i used new group display and its working now. Thanks [import]uid: 11559 topic_id: 20036 reply_id: 79017[/import]