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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-- 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} )

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 :)

Thanks. It does make sense, but i have no idea how to implement it. So ill look around. Thanks

Would you be able to help out with the code. Thanks

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

Hey Ayena, apologies, the time difference can cause a bit of a delay in response sometimes - luckily Ignis is spot on in his instructions :)

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.

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...

Hey thanks
here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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} )

Seems like you're close to getting this to work. I would change your "shapePressed" function to this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

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.
"

views:1381 update:2012/1/10 9:16:25
corona forums © 2003-2011