collision detection doesn't work inside scene:show - lua

I'm trying to detect collision on two object inside my scene:show function
This is my first set of objects which have a collision listener on it
for i = 1, table.maxn(layout.playgrid) do
physics.addBody( leftholes[i], "static" )
sceneGroup:insert(3,leftholes[i])
leftholes[i].name = "hole"
leftholes[i]:addEventListener( "collision", lis )
physics.addBody( rightholes[i], "static")
sceneGroup:insert(3,rightholes[i])
rightholes[i].name = "hole"
rightholes[i]:addEventListener( "collision", lis )
physics.addBody( topholes[i], "static" )
sceneGroup:insert(3,topholes[i])
topholes[i].name = "hole"
topholes[i]:addEventListener( "collision", lis )
physics.addBody(bottomholes[i], "static")
sceneGroup:insert(3,bottomholes[i])
bottomholes[i]:addEventListener( "collision", lis )
bottomholes[i].name = "hole"
end
and this is the object which will collision with the hole object
hand = display.newImage("assets/hand.png",350,490)
hand.name = "hand"
physics.addBody( hand, "static")
and that my collision listener
local function lis(event)
if event.phase == "began" and event.object2.name=="hand" then
print( "detected" )
local function tra( )
trans = display.newImage('assets/gray.png',indicesToOuterCordinate(layout.finalx,layout.finaly,layout.finalside,false))
physics.addBody( trans, "static")
sceneGroup:insert(trans)
hand:toFront()
end
end
end

Just check whether your are adding your collision listener with the RuntimeEvent listener .
If you does not add your collision with runtime event listeners your will not be detected .
local function onCollision( event )
if ( event.phase == "began" ) then
print( "began: " .. event.object1.myName .. " and " .. event.object2.myName )
elseif ( event.phase == "ended" ) then
print( "ended: " .. event.object1.myName .. " and " .. event.object2.myName )
end
end
Runtime:addEventListener( "collision", onCollision )

Related

Corona Touch Listener

I am learning corona sdk
This is the following code
when the two objects overlaps it becomes 1 objects(Moves together), i have no clue whats going on...
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
-- Your code here
local physics = require( "physics" )
physics.start()
local crate1 = display.newImage( "crate.png", display.contentCenterX, 100 )
crate1.name = "crate1"
local crate2 = display.newImage( "crate.png", display.contentCenterX + 100, 100 )
crate2.name = "crate2"
crate1:scale(0.2, 0.2)
crate2:scale(0.2, 0.2)
local bodyTouched = function(event)
if(crate1.name == event.target.name and event.phase == "moved")
then
print("Moved " , event.target.name )
crate1.x = event.x
crate1.y = event.y
elseif (crate2.name == event.target.name and event.phase == "moved")
then
print( "Moved ", event.target.name )
crate2.x = event.x
crate2.y = event.y
end
end
physics.addBody( crate1, "static")
physics.addBody( crate2, "static")
crate1:addEventListener( "touch", bodyTouched )
crate2:addEventListener( "touch", bodyTouched )
Please refer to the Corona SDK documentation.
https://docs.coronalabs.com/daily/guide/events/detectEvents/index.html#TOC
Section: Understanding Hit Events
In general you should read the documentation on anything you use.
Your touch event will propagate through all display objects that intersect with the event coordinate. The event will trigger all registered event listeners on its way.
To avoid having multiple listeners triggered your event listener bodyTouch has to return true which will tell Corona that you handled the event and no further listeners should be invoked.

corona + collision not working

I am learning Corona and I am trying to use the collision event.
The target of the following program is that the rock is moving towards to the car using transition.to. And print a line to report the collision where it occurs.
However, it does not work. Moreover, I even cannot receive the message "enterenter", what is the reason why the program cannot even get into the enter function?
My code is as follow. Thank you in advance.
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require "physics"
physics.start(); physics.pause()
local function onLocalCollision( self, event )
if ( event.phase == "began" ) then
print( self.myName .. ": collision began with " .. event.other.myName )
elseif ( event.phase == "ended" ) then
print( self.myName .. ": collision ended with " .. event.other.myName )
end
end
local widget = require "widget"
function scene:create( event )
print("entercreate")
local sceneGroup = self.view
local backgrd = display.newImage("background2.png",0,260)
backgrd:scale(3,3)
local car = display.newImage("car2.png",80,270)
physics.addBody(car,"static")
car.myName="Car"
local rock = display.newImage("rock.jpg",520,280)
rock:scale(0.05,0.05)
physics.addBody(rock,"static")
rock.myName="rock"
sceneGroup:insert(backgrd)
sceneGroup:insert(car)
sceneGroup:insert(rock)
transition.to(backgrd,{time=24000, x=-1800,onComplete=endScroll})
transition.to(rock,{time=4000, delay=2500,x=-40})
end
function scene:enter( event )
print("enterenter")
physics.start()
local sceneGroup = self.view
Runtime:addEventListener( "collision", onLocalCollision )
end
scene:addEventListener( "create", scene )
scene:addEventListener( "enter", scene )
return scene
Working code with a local and a global (Runtime) listener:
local widget = require "widget"
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require "physics"
physics.start()
physics.setGravity( 0,0 )
local function onGlobalCollision( event )
local target = event.object1
local other = event.object2
if ( event.phase == "began" ) then
print( "GLOBAL: " .. target.name .. ": collision began with " .. other.name )
elseif ( event.phase == "ended" ) then
print( "GLOBAL: " .. target.name .. ": collision ended with " .. other.name )
end
end
local function onLocalCollision( event )
local target = event.target
local other = event.other
if ( event.phase == "began" ) then
print( "LOCAL: " .. other.name .. ": collision began with " .. target.name )
elseif ( event.phase == "ended" ) then
print( "LOCAL: " .. other.name .. ": collision ended with " .. target.name )
end
end
function scene:create( event )
print("scene:create")
local sceneGroup = self.view
local car = display.newRect( 100, 100, 100, 100 )
car.name = "car"
physics.addBody( car )
local rock = display.newRect( 250, 250, 100, 100 )
rock.name = "rock"
rock:setFillColor( 0.5, 0.5, 0.5 )
physics.addBody( rock )
rock:addEventListener( "collision", onLocalCollision )
sceneGroup:insert(car)
sceneGroup:insert(rock)
transition.to(rock,{time=4000, x = -40, y = -100})
Runtime:addEventListener( "collision", onGlobalCollision )
end
function scene:enter( event )
print("scene:enter")
local sceneGroup = self.view
end
scene:addEventListener( "create", scene )
scene:addEventListener( "enter", scene )
return scene
The problem that you have with your code is that the two bodies are static and two static bodies can't collide with each other and that there is nothing called scene:enter in composer (so that's why it doesn't gets called and therefor not the onLocalListener).
Some body types will — or will not — collide with other body types. In
a collision between two physical objects, at least one of the objects
must be dynamic, since this is the only body type which collides with
any other type. See the Physics Bodies guide for details on body
types.
https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html
Here is some information about the Composer API and you'll that you should probably use scene:show:
https://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/

Perspective Virtual Camera library issue Corona SDK

Hello all I am currently developing an application that I am going to use the perspective library with. I have imported the library and have written the correct code it seems but to no avail my camera is not working properly. I am attempting to get the camera to follow the main character you are moving around with the buttons. I added him and the background and a "tree" to the camera and set him as the focus but it still will not follow him. Here is my code please tell me what I have done wrong here and how to fix it.
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local perspective = require( "perspective" )
local camera = perspective.createView()
local physics = require( "physics" )
--physics.setDrawMode( "hybrid" )
local function start( event )
if( event.phase == "ended" ) then
end
end
local bg
local floor
local leftwall
local rightwall
local player
local button1
local button2
local pausebtn
local tree
local function b1( event )
if( event.phase == "ended" ) then
player:applyLinearImpulse( -.01, -.1 )
end
end
local function b2( event )
if( event.phase == "ended" ) then
player:applyLinearImpulse( .01, -.1 )
end
end
local function playerCollision( event )
if( event.phase == "began" ) then
storyboard.gotoScene( "gameover" )
end
end
local function pause( event )
if( event.phase == "ended" ) then
storyboard.showOverlay( "pause" )
physics.pause()
end
end
function scene:createScene( event )
local group = self.view
physics.start()
bg = display.newImage( "bg.png", display.contentWidth/2, display.contentHeight/2, display.contentWidth, display.contentHeight )
camera:add(bg, 3, false)
player = display.newImage( "player.png", display.contentWidth/2, display.contentHeight/2, display.contentWidth, display.contentHeight )
player.collision = playerCollision
camera:add(player, 1, true)
tree = display.newImage( "tree.png", 200, 200)
camera:add(tree, 2, false)
floor = display.newRect( 285, 375, 570, 1 )
button1 = display.newImage( "button1.png", 50, 275 )
button2 = display.newImage( "button2.png", 515, 275 )
leftwall = display.newRect( 0, -1000, 1, 5000 )
rightwall = display.newRect( 570, 300, 1, 5000 )
pausebtn = display.newImage( "pausebtn.png", 540, 30 )
-- Physics
physics.addBody( player )
physics.addBody( floor )
floor.bodyType = "static"
physics.addBody( rightwall )
rightwall.bodyType = "static"
physics.addBody( leftwall )
leftwall.bodyType = "static"
group:insert(bg)
group:insert(player)
group:insert(floor)
group:insert(button1)
group:insert(button2)
group:insert(rightwall)
group:insert(leftwall)
group:insert(pausebtn)
group:insert(tree)
end
function scene:enterScene( event )
print( "game" )
button1:addEventListener( "touch", b1 )
button2:addEventListener( "touch", b2 )
player:addEventListener( "collision", playerCollision )
pausebtn:addEventListener( "touch", pause )
end
function scene:exitScene()
player = nil
camera:cancel()
end
function scene:destroyScene( event )
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
return scene
Looks like you are missing the call to camera:track() (which should probably go in the enterScene handler). You might also need camera:setBounds(false).
If this still doesn't fix issue, remove all code from your post that is not required in order to duplicate the problem. If you can boil it down to a small example that I can run, I can try and post update.

Collision Event in Corona

I am wondering how to make my collision work correctly in Corona. Here is what I have so far:
local function onLocalCollision( event )
if ( event.phase == "began" ) then
print( "began: " .. event.object1.myName .. " and " .. event.object2.myName )
test = display.newRect(screenW - 50, halfH, 100, screenH)
elseif ( event.phase == "ended" ) then
print( "ended: " .. event.object1.myName .. " and " .. event.object2.myName )
end
lilPig.collision = onLocalCollision
lilPig:addEventListener( "collision", lilPig )
endOfScreen.collision = onLocalCollision
endOfScreen:addEventListener( "collision", endOfScreen )
If you could help me that would be great! Thanks in advance!
I think, firstly you should add some property with your COLLISION objects, like:
lilPig.myName = "lilPig"
endOfScreen.myName = "endOfScreen"
so, we can directly check them in Collision listener and also if we have more Collision's after that, we can easily check by another condition.
we can also implement it by RunTime listener.
--------
RuntimeListener("collision", onCollision )
--------
function onCollision( event)
if (event.phase == "began") then
print("COLLISION: ".. event.object1.myName .. " & ".. event.object2.myName)
if (event.object1.myName == "lilPig" and event.object2.myName == "endOfScreen") or
(event.object1.myName == "endOfScreen" and event.object2.myName == "lilPig") then
print("Your code of block for collision event")
end

Collision detection : myName nil value

I'm trying to set up collision for my game with code from Corona SDK sample called "Collision Detection".
I'm getting this realy strange error every time ball collides with something -
Runtime error
...er 1\documents\corona projects\earthyball\level1.lua:114: attempt to concatenate field 'myName' (a nil value) stack traceback:
[C]: ?
...er 1\documents\corona projects\earthyball\level1.lua:114: in function <...er 1\documents\corona prRuntime error
...er 1\documents\corona projects\earthyball\level1.lua:118: attempt to concatenate field 'myName' (a nil value) stack traceback:
[C]: ?
This is my code
display.setStatusBar(display.HiddenStatusBar)
local storyboard = require "storyboard"
local scene = storyboard.newScene()
local physics = require "physics"
physics.start()
physics.setScale( 60 )
physics.setGravity( 0, 12 )
system.setAccelerometerInterval( 100 )
function scene:createScene(event)
local screenGroup = self.view
bkg = display.newImage("bkg.png")
screenGroup:insert(bkg)
finish = display.newImage("finishflag.png")
finish.x = 400
finish.y = 260
finish.myName ="flag"
physics.addBody(finish,"static",element)
blockl1 = display.newImage("blockl1.png")
blockl1:setReferencePoint(display.CentreReferencePoint)
blockl1.x = 165
blockl1.y = 230
physics.addBody(blockl1,"static",element)
screenGroup:insert(blockl1)
l1block2 = display.newImage("l1block2.png")
l1block2:setReferencePoint(display.CentreReferencePoint)
l1block2.x = 300
l1block2.y = 90
physics.addBody(l1block2,"static",element)
screenGroup:insert(l1block2)
line1 = display.newImage("line1.png")
line1:setReferencePoint(display.CentreReferencePoint)
line1.x = 230
line1.y = 300
physics.addBody(line1, "static", element)
line1.myName = "pod"
screenGroup:insert(line1)
line2 = display.newImage("line1.png")
line2:setReferencePoint(display.CentreReferencePoint)
line2.x = 230
line2.y = 10
physics.addBody(line2, "static", element)
line2.myName = "pod2"
screenGroup:insert(line2)
line1Ver = display.newImage("lineVer.png")
line1Ver:setReferencePoint(display.CentreReferencePoint)
line1Ver.x = 20
line1Ver.y = 230
physics.addBody(line1Ver, "static", element)
screenGroup:insert(line1Ver)
line2Ver = display.newImage("lineVer.png")
line2Ver:setReferencePoint(display.CentreReferencePoint)
line2Ver.x = 440
line2Ver.y = 230
physics.addBody(line2Ver, "static", element)
screenGroup:insert(line2Ver)
ball = display.newImage("ball.png")
ball.x = 50
ball.y = 30
physics.addBody(ball,"dynamic",{density=3.1, bounce=0.6, friction=0.5, radius=17})
finish.myName ="ball"
screenGroup:insert(ball)
end
function scene:enterScene(event)
finish.collision = onLocalCollision
finish:addEventListener( "collision", finish )
line1.collision = onLocalCollision
line1:addEventListener( "collision", line1 )
line2.collision = onLocalCollision
line2:addEventListener( "collision", line2 )
ball.collision = onLocalCollision
ball:addEventListener( "collision", ball )
end
function onLocalCollision( self, event )
if ( event.phase == "began" ) then
print( self.myName .. ": collision began with " .. event.other.myName )
elseif ( event.phase == "ended" ) then
print( self.myName .. ": collision ended with " .. event.other.myName )
end
end
function onTilt( event )
physics.setGravity( ( 12.5 * event.xGravity ), ( -12.5 * event.yGravity ) )
end
function scene:exitScene(event)
end
function scene:destroyScene(event)
end
Runtime:addEventListener( "accelerometer", onTilt )
element = { friction=0.5, bounce=0.3 }
scene:addEventListener("createScene",scene)
scene:addEventListener("enterScene",scene)
scene:addEventListener("exitScene",scene)
scene:addEventListener("destroyScene",scene)
return scene
Thanks for helping!
Your all objects are a part of physics. That means, they all collide with each other. Thats why in every collision, it is possible to call a collision event listener. What I'm trying to saying is that, you have a collision event listener on your ball object, line1, line2 and finish. And you think that if there is a collision pnly between these objects your event listener will be called. But for example, when ball object collides with line2ver, line1ver etc., your event listener ball() is calling and that function trys to reach line1ver.myName which does not exist.
So, you must wheter add .myName part to all your objects, or just didn't print .myName part.
And a little tip to you:
I think you want to detect collisions with ball - other objects. For that purpose, you can use just one collision event listener, which is ball()
Hope it helps...

Resources