Collision Detection - Please HELP

Yes I know it works fine by it self. But as I told you, the problem occurred if the bullet was removed somewhere else before reaching the target point (y=460). therefore this problem occurred as the transision function will try to execute the onComplete function for a nil object (bullet already removed prior to reaching y=460)

Really have been working on it for many hours.

I really would appreciate if I uploaded the code for you just to talk a look on it [import]uid: 11038 topic_id: 3907 reply_id: 12663[/import]

try something like this

[lua]bullet.myTransition = transition.to(bullet, …etc etc… onComplete=removeBullet)[/lua]

and in your spaceship/bullet collision try something like this

[lua]local obj1 = event.object1
if(obj==“bullet”) then

if(obj1.myTransition ~= nil) then
obj1.myTransition.cancel()
obj1:removeSelf()[/lua]

[import]uid: 6645 topic_id: 3907 reply_id: 12665[/import]

WOAH! Quote from above, “static bodies don’t move, and don’t interact with each other; examples of static objects would include the ground, or the walls of a pinball machine”.

So how can I have a body that shouldn’t be subject to gravity but be able to be moved. Imagine a if you will a ‘ceiling’ that a ball would bounce off of and cannot pass though but could be lowered making the ‘ceiling’ lower?

Would they be kinematic?

I think that’s just fixed a bug :slight_smile: [import]uid: 9371 topic_id: 3907 reply_id: 12668[/import]

set it to “kinematic” and try this

[lua] – if round ball hits line
if(obj2.is==“round” and obj1.is==“line”) then
transition.to(obj1, {time=0, y=obj1.y+1})
removeBall(obj2)
end

for some reason (presumably how corona deals with safe event times), you cannot set obj1.y=obj1.y + 1, but you can use a transition [import]uid: 6645 topic_id: 3907 reply_id: 12685[/import]

actually it can still be static

[lua]local physics = require(“physics”)
local rnd = math.random

physics.start()

local divider = display.newLine(0,300,320,300)
divider:setColor(255,0,0)
divider.width=1
divider.alpha=0.2

local divider2 = display.newLine(0,400,320,400)
divider2:setColor(255,0,0)
divider2.width=1
divider2.alpha=0.2

local line = display.newRect(0,300,170,20)
line:setFillColor(255,0,0)
line.is=“line”
line.isPlatform=true
physics.addBody(line, “static”, {density=1})

local function removeBall(obj)
Runtime:removeEventListener(“enterFrame”, obj)
obj:removeSelf()
end
local function lineUp(event)
transition.to(line, {time=50, y=310})
end

local function globalCollision(event)

local obj1 = event.object1
local obj2 = event.object2

– if round ball hits line
if(obj2.is==“round” and obj1.is==“line”) then
transition.to(obj1, {time=100, y=obj1.y+10, onComplete=lineUp})
removeBall(obj2)
end

end
local function newBall()

local ball

local radius = rnd(50)+5
local pick = math.floor(rnd(2))

if(pick==1) then

ball = display.newCircle(rnd(280)+20, 20, radius)
physics.addBody(ball, “dynamic”, {density=0.5, bounce=0.5,radius=radius})
ball.is=“round”

elseif(pick==2) then

ball = display.newRect(rnd(280)+20, 20,4,4)
physics.addBody(ball, “dynamic”, {density=1, bounce=0.1})
ball.is=“square”

end

ball:setFillColor(0,255,0)
ball.isBullet=true

– to be safe remove balls if they fall off the lower part of the screen
– otherwise they will continue falling forever
ball.enterFrame=function(self, event)
local ypos = self.y
if(ypos > 300) then self:setFillColor(255,0,0) end
if(ypos > 400) then removeBall(self) end
end

Runtime:addEventListener(“enterFrame”, ball)

end
Runtime:addEventListener(“collision”, globalCollision)
timer.performWithDelay(100,newBall,-1)[/lua] [import]uid: 6645 topic_id: 3907 reply_id: 12686[/import]

It didn’t work it still display the same message, here is the fixes applied as per your suggestion:-

function shotSpaceShip( event )
if (gameLevels[nextLevel].isDeffenceEnabled) then
– Add a Bounce
local xSpeedInSeconds = gameLevels[nextLevel].nemesisBulletShotSpeedInSeconds
local item = random(1,2)
local nemesisToShot = objects[random(1,#objects)]
if nemesisToShot~=nil then
repeat
nemesisToShot = objects[random(1,#objects)]
until (nemesisToShot.y ~= nil)
end
local nemesisX, nemesisY = nemesisToShot.x, nemesisToShot.y
local nemesisBullet = display.newLine( nemesisX, nemesisY, nemesisX, nemesisY+8 )
nemesisBullet:setColor( 255, 255, 255, 255 )
nemesisBullet.width = 2
nemesisBullet.objectName = “nemesisBullet”
nemesisBullet.collision = bulletVsSpaceShipCollision
nemesisBullet:addEventListener( “collision”, nemesisBullet )
physics.addBody(nemesisBullet, “static”,{bounce = 0.3, fraction=1.0, denisty=0.0 })
nemesisBullet.collision = onCollision
nemesisBullet:addEventListener( “collision”, nemesisBullet )
nemesisGroup:insert(nemesisBullet)
nemesisBullet.myTransition=transition.to(nemesisBullet, {time=xSpeedInSeconds, delay=10, alpha=1.0,
transition=easing.outQuad,y=480,onComplete=removeThisBullet})
– to be safe remove balls if they fall off the lower part of the screen
– otherwise they will continue falling forever
elseif (cancelTransition) and (remainingSouls < 1 ) then
timer.cancel(event.source)
end
return nemesisBullet
end
[/blockcode]

here is the onCollision function:-
[blockcode]
function onCollision( self, event )
–Self : flyingObject & Other: bullet, Self : bounce & Other: bullet, Self : spaceShip & Other: bounce, Self : spaceShip & Other: flyingObject
if ( self.objectName ~= nil ) and (event.other.objectName ~= nil) then
--print("Total Killed: "…totalKilled … " Self : "…self.objectName … " & Other: "…event.other.objectName)
ipointerX = event.other.x
ipointerY = event.other.y
local showScore = true
if ( event.phase == “began”) then
if (self.objectName == “nemesisBullet”) and (event.other.objectName==“bullet”) then
transisioself.
– remove both bullets
--event.other:removeSelf()
levelCounter = levelCounter + gameLevels[nextLevel].bulletScore
--self:removeSelf()
elseif ( self.objectName ~= “spaceShip” ) and ( event.other.objectName ~= “floorObject” ) then
totalKilled = totalKilled + 1
end
nemesisObject.text = "Killed Nemesis: " … totalKilled
if ((self.objectName == “bounce”) and (event.other.objectName == “bullet”)) then
local showObjectScore= showCounter(self)
levelCounter = levelCounter + gameLevels[nextLevel].bounceScore
self:removeSelf() – remove Bounce object from group
local bounceClearance = clearBounces(self)
event.other:removeSelf() – remove Bullet
elseif ( self.objectName ~= “spaceShip” ) and ( event.other ~= “floorObject” ) then
levelCounter = levelCounter + gameLevels[nextLevel].nemesisScore
end
if ((self.objectName == “flyingObject” ) and ( event.other.objectName == “bullet”)) then
local showObjectScore= showCounter(self)
gameTotalObjects = gameTotalObjects - 1
self:removeSelf() – remove flyingObject
event.other:removeSelf() – remove Bullet
if ( gameTotalObjects == 0 ) then – Completed Level
local completeLevel = completeGameLevel()
end
elseif ( (self.objectName == “spaceShip” ) and ( event.other.objectName == “flyingObject”) ) then
local showObjectScore= showCounter(event.other)
event.other:removeSelf()
self:stop();self:removeSelf()
destroySpaceShip()
elseif ((self.objectName == “spaceShip”) and (event.other.objectName == “bounce”) ) then
local showObjectScore= showCounter(event.other)
self:stop();self:removeSelf()
destroySpaceShip()
levelCounter = levelCounter + gameLevels[nextLevel].bounceScore
elseif ((self.objectName == “spaceShip”) and (event.other.objectName == “nemesisBullet”) ) then
local showObjectScore= showCounter(event.other)
local myobj1 = event.other.objectName
if(myobj1==“bulletNemesis”) then
if(myobj1.myTransition ~= nil) then
myobj1.myTransition.cancel()
myobj1:removeSelf()
end
end
if event.other ~= nil then event.other:removeSelf() end
self:stop();self:removeSelf()
destroySpaceShip()
end
scoreObject.text = "Score: "…levelCounter
end
end
end
[/blockcode]

Thanks man [import]uid: 11038 topic_id: 3907 reply_id: 12721[/import]

here is the onCollision function:-
[blockcode]
function shotSpaceShip( event )
if (gameLevels[nextLevel].isDeffenceEnabled) then
– Add a Bounce
local xSpeedInSeconds = gameLevels[nextLevel].nemesisBulletShotSpeedInSeconds
local item = random(1,2)
local nemesisToShot = objects[random(1,#objects)]
if nemesisToShot~=nil then
repeat
nemesisToShot = objects[random(1,#objects)]
until (nemesisToShot.y ~= nil)
end
local nemesisX, nemesisY = nemesisToShot.x, nemesisToShot.y
local nemesisBullet = display.newLine( nemesisX, nemesisY, nemesisX, nemesisY+8 )
nemesisBullet:setColor( 255, 255, 255, 255 )
nemesisBullet.width = 2
nemesisBullet.objectName = “nemesisBullet”
nemesisBullet.collision = bulletVsSpaceShipCollision
nemesisBullet:addEventListener( “collision”, nemesisBullet )
physics.addBody(nemesisBullet, “static”,{bounce = 0.3, fraction=1.0, denisty=0.0 })
nemesisBullet.collision = onCollision
nemesisBullet:addEventListener( “collision”, nemesisBullet )
nemesisGroup:insert(nemesisBullet)
nemesisBullet.myTransition=transition.to(nemesisBullet, {time=xSpeedInSeconds, delay=10, alpha=1.0,
transition=easing.outQuad,y=480,onComplete=removeThisBullet})
– to be safe remove balls if they fall off the lower part of the screen
– otherwise they will continue falling forever
elseif (cancelTransition) and (remainingSouls < 1 ) then
timer.cancel(event.source)
end
return nemesisBullet
end
[/blockcode]
here is the onCollision function:-
[blockcode]
function onCollision( self, event )
–Self : flyingObject & Other: bullet, Self : bounce & Other: bullet, Self : spaceShip & Other: bounce, Self : spaceShip & Other: flyingObject
if ( self.objectName ~= nil ) and (event.other.objectName ~= nil) then
–print("Total Killed: "…totalKilled … " Self : "…self.objectName … " & Other: "…event.other.objectName)
ipointerX = event.other.x
ipointerY = event.other.y
local showScore = true
if ( event.phase == “began”) then
if (self.objectName == “nemesisBullet”) and (event.other.objectName==“bullet”) then
transisioself.
– remove both bullets
–event.other:removeSelf()
levelCounter = levelCounter + gameLevels[nextLevel].bulletScore
–self:removeSelf()
elseif ( self.objectName ~= “spaceShip” ) and ( event.other.objectName ~= “floorObject” ) then
totalKilled = totalKilled + 1
end
nemesisObject.text = "Killed Nemesis: " … totalKilled
if ((self.objectName == “bounce”) and (event.other.objectName == “bullet”)) then
local showObjectScore= showCounter(self)
levelCounter = levelCounter + gameLevels[nextLevel].bounceScore
self:removeSelf() – remove Bounce object from group
local bounceClearance = clearBounces(self)
event.other:removeSelf() – remove Bullet
elseif ( self.objectName ~= “spaceShip” ) and ( event.other ~= “floorObject” ) then
levelCounter = levelCounter + gameLevels[nextLevel].nemesisScore
end
if ((self.objectName == “flyingObject” ) and ( event.other.objectName == “bullet”)) then
local showObjectScore= showCounter(self)
gameTotalObjects = gameTotalObjects - 1
self:removeSelf() – remove flyingObject
event.other:removeSelf() – remove Bullet
if ( gameTotalObjects == 0 ) then – Completed Level
local completeLevel = completeGameLevel()
end
elseif ( (self.objectName == “spaceShip” ) and ( event.other.objectName == “flyingObject”) ) then
local showObjectScore= showCounter(event.other)
event.other:removeSelf()
self:stop();self:removeSelf()
destroySpaceShip()
elseif ((self.objectName == “spaceShip”) and (event.other.objectName == “bounce”) ) then
local showObjectScore= showCounter(event.other)
self:stop();self:removeSelf()
destroySpaceShip()
levelCounter = levelCounter + gameLevels[nextLevel].bounceScore
elseif ((self.objectName == “spaceShip”) and (event.other.objectName == “nemesisBullet”) ) then
local showObjectScore= showCounter(event.other)
local myobj1 = event.other.objectName
if(myobj1==“bulletNemesis”) then
if(myobj1.myTransition ~= nil) then
myobj1.myTransition.cancel()
myobj1:removeSelf()
end
end
if event.other ~= nil then event.other:removeSelf() end
self:stop();self:removeSelf()
destroySpaceShip()
end
scoreObject.text = "Score: "…levelCounter
end
end
end
[/blockcode] [import]uid: 11038 topic_id: 3907 reply_id: 12722[/import]