No collision between some objects

Why there isn’t any collision with “theFloor” and with “ceiling”. Probably the questions is elementary but i’m a new young developer.

Here the code:
 

function scene:createScene(event) local screenGroup = self.view local background = display.newImage("bg.png") screenGroup:insert(background) ceiling = display.newImage("invisibleTile.png") ceiling:setReferencePoint(display.BottomLeftReferencePoint) ceiling.x = 0 ceiling.y = -5 ceiling.name = "ceiling" physics.addBody(ceiling, "static", {density=0.1, friction=0.2, bounce=0.1}) screenGroup:insert(ceiling) theFloor = display.newImage("invisibleTile.png") theFloor:setReferencePoint(display.BottomLeftReferencePoint) theFloor.x = 0 theFloor.y = 350 theFloor.name = "theFloor" physics.addBody(theFloor, "static", {density=0.1, friction=0.2, bounce=0.1}) screenGroup:insert(theFloor) jetSpriteSheet = sprite.newSpriteSheet("jet.png", 50, 17) jetSprites = sprite.newSpriteSet(jetSpriteSheet, 1, 4) sprite.add(jetSprites, "jets", 1, 4, 4000, 0) jet = sprite.newSprite(jetSprites) jet.x = -80 jet.y = 100 jet:prepare("jets") jet:play() jet.collided = false jet.name = "jetPlayer" physics.addBody(jet, "static", {density = 0.1, friction = 0.2, bounce = 0.1, radius = 12}) screenGroup:insert(jet) jetintro = transition.to(jet, {time = 2000, x=100, onComplete=jetReady}) mine1 = display.newImage("mine.png") mine1.name = "mine" mine1.x = 500 mine1.y = math.random(90,260) mine1.speed = math.random(2,6) mine1.initY = mine1.y mine1.amp = math.random(20,140) mine1.angle = math.random(1,360) physics.addBody(mine1, "static", {density = 0.1, friction = 0.2, bounce = 0.1, radius = 12}) screenGroup:insert(mine1) mine2 = display.newImage("mine.png") mine2.name = "mine" mine2.x = 500 mine2.y = math.random(90,260) mine2.speed = math.random(2,6) mine2.initY = mine2.y mine2.amp = math.random(20,140) mine2.angle = math.random(1,360) physics.addBody(mine2, "static", {density = 0.1, friction = 0.2, bounce = 0.1, radius = 12}) screenGroup:insert(mine2) mine3 = display.newImage("mine.png") mine3.name = "mine" mine3.x = 500 mine3.y = math.random(90,260) mine3.speed = math.random(2,6) mine3.initY = mine3.y mine3.amp = math.random(20,140) mine3.angle = math.random(1,360) physics.addBody(mine3, "static", {density = 0.1, friction = 0.2, bounce = 0.1, radius = 12}) screenGroup:insert(mine3) end function moveMines(self, event) if self.x \< -50 then self.x = 500 self.y = math.random(90,260) self.speed = math.random(2,6) self.amp = math.random(20,140) self.angle = math.random(1,360) else self.x = self.x - self.speed self.angle = self.angle + 0.1 self.y = self.amp \* math.sin(self.angle) + self.initY end end function jetReady() jet.bodyType = "dynamic" end function activateJet(self, event) if self.y \> 30 then self:applyForce(0, -1.2, self.x, self.y) end end function touchScreen(event) if event.phase == "began" then jet.enterFrame = activateJet Runtime:addEventListener("enterFrame", jet) end if event.phase == "ended" then Runtime:removeEventListener("enterFrame", jet) end end function gameOver() storyboard.gotoScene( "restart", "fade", 400) end function onCollision(event) if event.phase == "began" then if event.object1.name == "jetPlayer" then if event.object2.name == "ceiling" then print ("hit") print (event.object1.name) print (event.object2.name) elseif event.object2.name == "theFloor" then print ("hit") print (event.object1.name) print (event.object2.name) elseif event.object2.name == "mine" then print ("hit") print (event.object1.name) print (event.object2.name) end end end end function scene:enterScene(event) storyboard.purgeScene("start") storyboard.purgeScene("restart") Runtime:addEventListener("touch", touchScreen) city1.enterFrame = scrollCity Runtime:addEventListener("enterFrame", city1) city2.enterFrame = scrollCity Runtime:addEventListener("enterFrame", city2) city3.enterFrame = scrollCity Runtime:addEventListener("enterFrame", city3) city4.enterFrame = scrollCity Runtime:addEventListener("enterFrame", city4) mine1.enterFrame = moveMines Runtime:addEventListener("enterFrame", mine1) mine2.enterFrame = moveMines Runtime:addEventListener("enterFrame", mine2) mine3.enterFrame = moveMines Runtime:addEventListener("enterFrame", mine3) Runtime:addEventListener("collision", onCollision) end function scene:exitScene(event) Runtime:removeEventListener("touch", touchScreen) Runtime:removeEventListener("enterFrame", city1) Runtime:removeEventListener("enterFrame", city2) Runtime:removeEventListener("enterFrame", city3) Runtime:removeEventListener("enterFrame", city4) Runtime:removeEventListener("enterFrame", mine1) Runtime:removeEventListener("enterFrame", mine2) Runtime:removeEventListener("enterFrame", mine3) Runtime:removeEventListener("collision", onCollision) end function scene:destroyScene(event) end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene

“static” objects don’t generate collisions with other “static” objects.  Perhaps you want your mines to be dynamic.

Jet collides with all mines but it doesn’t collide with “ceiling” and “theFloor”

That’s pretty strange, cause as Rob mentioned, static objects do not interact with each other.

But that doens’t matter. I would suggest to make both, the Jet an the Mines dynamic objects.

Use mine.isSensor = true to prevent the jet being pushed around by the mines.

And use jet.isFixedRotation = true to prevent the jet to spin around trough a collision or applying force.

Set both values AFTER you added the body.

Greetings Torben

I try it but it’s the same. Jet it was already a “dynamic” object. Jet collides with mines without any problem. The mystery is always the “ceiling” and “theFloor”, they don’t collide with the Jet and i can’t understand why

P.s: i’m sorry about my perhaps “uncorrect english” but i’m italian and i know it at school level

Ah, ok.

So, what do you mean by they don’t collide? Do they pass trough each other or do you just get no collision event?

I would generally suggest using a collision listener attached to a specific object than to runtime.

function jet:collision(event) &nbsp;&nbsp; local phase = event.phase &nbsp;&nbsp; local other = event.other &nbsp; &nbsp;&nbsp; if phase == "began" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if other.name == "ceiling" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif other.name == "theFloor" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif other.name == "mine" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp; end end &nbsp; jet:addEventListener("collision")

The Jet touch “ceiling” and “theFloor” but the corona simulator output don’t print anything.

Why if i use your listener i have a runtime error on jet? "attemp to index global jet (a nil value)

Hi Nico,

The error you’re getting is a scope error. “jet” is not declared as a local variable outside of the “createScene” function, so Lua doesn’t know what that variable/object is.

I suggest that you watch this video on “Scope”, located here:

http://www.coronalabs.com/resources/tutorials/corona-basics/

Take care,

Brent

I watch the video but i have some doubts. Could you make me an example in my specific case?

I understand the error! When jet collides with mines it’s considered “object1” and mines are considered “object2”. When jet collides with “ceiling” or “theFloor” it’s considered “object2” and they’re considered “object1” so the if was false.

if event.object1.name == "jetPlayer" then

“static” objects don’t generate collisions with other “static” objects.  Perhaps you want your mines to be dynamic.

Jet collides with all mines but it doesn’t collide with “ceiling” and “theFloor”

That’s pretty strange, cause as Rob mentioned, static objects do not interact with each other.

But that doens’t matter. I would suggest to make both, the Jet an the Mines dynamic objects.

Use mine.isSensor = true to prevent the jet being pushed around by the mines.

And use jet.isFixedRotation = true to prevent the jet to spin around trough a collision or applying force.

Set both values AFTER you added the body.

Greetings Torben

I try it but it’s the same. Jet it was already a “dynamic” object. Jet collides with mines without any problem. The mystery is always the “ceiling” and “theFloor”, they don’t collide with the Jet and i can’t understand why

P.s: i’m sorry about my perhaps “uncorrect english” but i’m italian and i know it at school level

Ah, ok.

So, what do you mean by they don’t collide? Do they pass trough each other or do you just get no collision event?

I would generally suggest using a collision listener attached to a specific object than to runtime.

function jet:collision(event) &nbsp;&nbsp; local phase = event.phase &nbsp;&nbsp; local other = event.other &nbsp; &nbsp;&nbsp; if phase == "began" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if other.name == "ceiling" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif other.name == "theFloor" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif other.name == "mine" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp; end end &nbsp; jet:addEventListener("collision")

The Jet touch “ceiling” and “theFloor” but the corona simulator output don’t print anything.

Why if i use your listener i have a runtime error on jet? "attemp to index global jet (a nil value)

Hi Nico,

The error you’re getting is a scope error. “jet” is not declared as a local variable outside of the “createScene” function, so Lua doesn’t know what that variable/object is.

I suggest that you watch this video on “Scope”, located here:

http://www.coronalabs.com/resources/tutorials/corona-basics/

Take care,

Brent

I watch the video but i have some doubts. Could you make me an example in my specific case?

I understand the error! When jet collides with mines it’s considered “object1” and mines are considered “object2”. When jet collides with “ceiling” or “theFloor” it’s considered “object2” and they’re considered “object1” so the if was false.

if event.object1.name == "jetPlayer" then