Physics Object not reacting to Collisions

So… I started learning Corona last week, and now I’ve hit a problem.
I’m making a small game where you’re supposed to fly a ship through an asteroid field. Currently all I have a re place holders though.

Now here’s the issue I’m having. The ship and the asteroids don’t seem to register collisions with each other.
Here’s the code I’m using, and I know, it’s very amateurish, but please disregard that.
Anyone that could help?

[code]
_W = display.contentWidth
_H = display.contentHeight

–local level1 = (require “level1”)

local physics = (require “physics”)
–local backgroundMusic = audio.loadStream(“background.mp3”)
–local backgroundMusicChannel = audio.play( backgroundMusic, { channel=1, loops=-1, fadein=5000 } )
local background = display.newGroup ()
local rocks = display.newGroup()
Dead = false
shipExist = false
physics.start()
physics.setGravity( 0, 0 )
local rock
local ship
physics.setDrawMode (“hybrid”)

–[[
local map1

level1 = function (event)
map1 = display.newImage(“map1.png”)
map2 = display.newImage(“map2.png”, 3600, 0)
–map1.yScale = 0.5
physics.addBody (map1, “static”, {isSensor=true})
physics.addBody (map2, “static”, {isSensor=true})
end

level1 ()
]]–

for i = 1, 10000 do – use 1 - 10000 objects
x = math.random (0, _W*100) – random place on width, between 0 and max
y = math.random (0, _H) – random place on height between 0 and max
z = math.random (1, 3) – random size between 1 and 3 pixels
star = display.newCircle(background, x, y, z ) – Object itself
t = math.random (100, 255) – random RGB content
r = math.random (100, 255) – random RGB content
e = math.random (100, 255) – random RGB content
star: setFillColor ( t, r, e ) – the color of each random object in a random color
end

for i = 1, 500 do – use 1 - 10000 objects
x = math.random (_W*1.1, _W*10) – random place on width, between 0 and max
y = math.random (0, _H) – random place on height between 0 and max
z = math.random (10, 15) – random size between 1 and 3 pixels
rock = display.newCircle( rocks, x, y, z ) – Object itself
physics.addBody (rock, “static”)
transition.to(rock, {time = 100000, alpha=1, x=(-20000)})
–map1.x = map1.x-3
–map2.x = map2.x-3

end

for i = 1, 300 do – use 1 - 10000 objects
x = math.random (_W*10, _W*20) – random place on width, between 0 and max
y = math.random (0, _H) – random place on height between 0 and max
z = math.random (10, 15) – random size between 1 and 3 pixels
rock = display.newCircle( rocks, x, y, z ) – Object itself
physics.addBody (rock, “static”)
transition.to(rock, {time = 100000, alpha=1, x=(-20000)})
–map1.x = map1.x-3
–map2.x = map2.x-3

end
background:insert(star)
background:insert(rock)
Runtime:addEventListener (“enterFrame”,
function()
–transition.to(rocks, {time = 10000, alpha=0, x=(-10000)})
background.x = background.x-5
–map1.x = map1.x-3
–map2.x = map2.x-3

end
)

createShip = function (event)
ship = display.newCircle (rocks, 0, 0, 7)
ship.x = _H*.5
ship.y = _W*.5
shipExist = true
physics.addBody (ship, “static”)
end

createShip(event)

–rocks1:insert(rock1)
–rocks2:insert(rock2)
ship:addEventListener(“collision”,
function(event)
print(“Ouch”)
end
)

Runtime:addEventListener(“touch”,
function(event)
if Dead == true then
return
else
if shipExist == true then
ship.x = event.x
ship.y = event.y
end
end
end
)
[blockcode] [import]uid: 197831 topic_id: 33452 reply_id: 333452[/import]

Since your rocks are already static bodies - i recommend you try making your ship dynamic. Also - from a practice point of view most of the collision listeners are table listeners which give you access to the self (ship) object which might be useful…

Summary:
physics.addBody (ship, “dynamic”)

AND

local function onCollision(self,event)
print (“Ouch”)
end

ship.collision = onCollision
ship:addEventListener(“collision”,ship) [import]uid: 55009 topic_id: 33452 reply_id: 132932[/import]

Hello,
For collisions to occur, at least one body must be “dynamic”. Two static bodies can not sense a collision (nor can two kinematic ones, as far as I know).

You might also want to refer to my “collision filter helper chart” post, to fine-tune which objects collide with other objects:
http://developer.coronalabs.com/forum/2010/10/25/collision-filters-helper-chart

Best of luck!
Brent Sorrentino [import]uid: 200026 topic_id: 33452 reply_id: 132955[/import]

I actually got it to work an hour or two after I posted.
Both Ship and Rock is now Dynamic, and gravity is set to 0 to keep them from moving around.

Thanks for the help though :slight_smile: [import]uid: 197831 topic_id: 33452 reply_id: 132960[/import]

Since your rocks are already static bodies - i recommend you try making your ship dynamic. Also - from a practice point of view most of the collision listeners are table listeners which give you access to the self (ship) object which might be useful…

Summary:
physics.addBody (ship, “dynamic”)

AND

local function onCollision(self,event)
print (“Ouch”)
end

ship.collision = onCollision
ship:addEventListener(“collision”,ship) [import]uid: 55009 topic_id: 33452 reply_id: 132932[/import]

Hello,
For collisions to occur, at least one body must be “dynamic”. Two static bodies can not sense a collision (nor can two kinematic ones, as far as I know).

You might also want to refer to my “collision filter helper chart” post, to fine-tune which objects collide with other objects:
http://developer.coronalabs.com/forum/2010/10/25/collision-filters-helper-chart

Best of luck!
Brent Sorrentino [import]uid: 200026 topic_id: 33452 reply_id: 132955[/import]

I actually got it to work an hour or two after I posted.
Both Ship and Rock is now Dynamic, and gravity is set to 0 to keep them from moving around.

Thanks for the help though :slight_smile: [import]uid: 197831 topic_id: 33452 reply_id: 132960[/import]