Object Collision remove

10 replies [Last post]
Blickon
User offline. Last seen 3 weeks 5 days ago. Offline
Joined: 23 Jan 2011

Hello corona community,
i need some help here, i am trying to make a stars system, when my ball collide with star object she remove the star, but when she collides with one block she removes the block too and i only want to remove the star objects, not the block objects, how i can make it if my ball have this function:

1
2
3
4
function spawner:collision (event)
        event.other:removeSelf()
        end
         ball1:addEventListener("collision", spawner)

Replies

Satheesh's picture
Satheesh
User offline. Last seen 3 hours 51 min ago. Offline
Joined: 25 May 2011

Keep names for your stars.

1
2
starObject = display.newImage("star.png")
starObject.name = "star"

Now filter out these stars in your listener.

1
2
3
4
5
6
7
function spawner:collision (event)
   if event.other.name=="star"
       return
   end
   event.other:removeSelf()
end
ball1:addEventListener("collision", spawner)

Or you can use collision filters for your stars.
http://developer.anscamobile.com/forum/2010/10/25/collision-filters-helper-chart
But the first method is straight forward!

Blickon
User offline. Last seen 3 weeks 5 days ago. Offline
Joined: 23 Jan 2011

oh thank you very much, it will resolve a lot of my problems :)

Blickon
User offline. Last seen 3 weeks 5 days ago. Offline
Joined: 23 Jan 2011

can i have physics for per exemple, my ball have physics.setGravity( 0,9.8 ) and my objects have physics.setGravity( 0,0 ), it is possible?
because i need to have gravity on the ball but dont want gravity on the objects, because if the objects are dynamic with gravity they are affected, but if they are kinematic or static, they arent affected by gravity but cant be removed by collision with the ball, how can i resolve it?

Satheesh's picture
Satheesh
User offline. Last seen 3 hours 51 min ago. Offline
Joined: 25 May 2011

For Corona to detect collisions, you need at least ONE of the colliding bodies to be dynamic. Since your ball object is dynamic,
you can keep your other objects as static or kinematic. Rest assured, collisions with the ball will be detected. :)

Sven.Lua
User offline. Last seen 17 hours 13 min ago. Offline
Joined: 17 Jun 2011

I think you can set gravity only for the whole "world", but you can make some bodies weight zero - it's famous as the weight watchers hack ;)

Blickon
User offline. Last seen 3 weeks 5 days ago. Offline
Joined: 23 Jan 2011

thanks for both :) how can i make some bodies to weight zero?

Satheesh's picture
Satheesh
User offline. Last seen 3 hours 51 min ago. Offline
Joined: 25 May 2011

1
physics.addBody(object,"static",{density=0})

That should do!

Blickon
User offline. Last seen 3 weeks 5 days ago. Offline
Joined: 23 Jan 2011

yeh i know it, but if it is dynamic and have gravity, is impossible to have the object stopped ?

Blickon
User offline. Last seen 3 weeks 5 days ago. Offline
Joined: 23 Jan 2011

Satheesh, i tried the code that you give me, and it isnt work, something is wrong, if i use my function:

1
2
3
4
function ball1:collision (event)
   event.other:removeSelf()
        end
         ball1:addEventListener("collision",ball1)

the game run on the simulator, and make what i want, but without any filter, because i only want to remove thing with "star" name

and if i use your function, the game dont run :/ try it to see plz, it will not run, but if you replace the function by the function above, will run

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
module(..., package.seeall)
 function new()
   
    local physics = require "physics"
    physics.start()
    physics.setDrawMode( "hybrid" )     
    physics.setGravity( 0,0 )
 
      
  
 
   
     
      local ball1 = display.newCircle( 80, 120, 20 ) 
      ball1:setFillColor( 0, 255, 0 ) 
      physics.addBody(ball1,"static")--{ density=2, bounce=0.3, radius=25});
      
      ball1.x = 250; ball1.y = 100
 
     local function touch(event)
        if event.phase == "ended" then
          physics.setGravity( 0,9.8 )
          ball1.bodyType = "dynamic"
       
          
        end
 
        
        end
            ball1:addEventListener( "touch", touch )
 
          block1 = display.newRect( 50, 50, 50, 50 )
block1.name = "star"
          block1:setFillColor( 255, 255, 255, 100 )
             physics.addBody(block1, "kinematic")-- { density=2, friction=0, bounce=0 })
        block1.x = 250 block1.y= 250 
 
 
       function ball1:collision (event)
       if event.other.name=="star"
    return
  end
   event.other:removeSelf()
end
        end
         ball1:addEventListener("collision",ball1)
end

boxie
User offline. Last seen 1 week 2 days ago. Offline
Joined: 27 Dec 2011

oh, glad to see your question was answered

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.