Simple Collision test error

I want my objects to collide and disappear. They’re 2 balls with planet images , coming at eachother and they just pass by over eachother.
Thanx in advance.

require "physics" physics.start() local planet = display.newImage("Planet.png") planet.x = 900 physics.addBody (planet, "static" ) transition.to(planet,{time = 10000, x = -650}) local planet2 = display.newImage("Planet.png") planet2.x = 5 physics.addBody (planet2, "static") transition.to(planet2,{time = 10000, x = 900}) local function onLocalCollision( self, event ) if ( event.phase == "began" ) then event.target:removeSelf() event.other:removeSelf() end end planet:addEventListener( "collision", planet ) planet2:addEventListener( "collision", planet2 ) [import]uid: 176335 topic_id: 32419 reply_id: 332419[/import]

HI there,

First, see the warnings here (http://developer.coronalabs.com/content/game-edition-collision-detection) and here (http://docs.coronalabs.com/api/event/collision/index.html) – you should avoid trying to remove a physics object in the middle of a collision listener. Instead, enclose the code to remove the physics objects in a short timer.performWithDelay.

Second, you have created two static objects and are manipulating them using transitions. Instead, try making them dynamic physics objects and setting their velocities so that they will collide with each other. That way, the physics engine “owns” the movement of the objects and can properly detect the collision.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 32419 reply_id: 128939[/import]

Thanx for the input Andrew. I got it to work but as i upgraded my game i ran into another problem. I have a sprite sheet of my bullet (blaster). It shoots fine but with that collision factor ,its transition gets a problem and the game gets stuck.

1- How do i cancel the transition when the object already gets removed by the collision.
2- Also, how do i put some delay between the fire action coz the bullets hit each other when you click/tap on fire button fast enough (timer.performWithDelay doesnt seem add-able anywhere in it)

[code]
local remover = function (obj)
obj:removeSelf()
end
local function weapon ()
blaster = sprite.newSprite (blasterset)
blaster:prepare(“fireA”)
blaster:play(“fireA”)
end

local function projectile ()
physics.addBody( blaster, “dynamic”, {bounce = 0} )
local function onLocalCollision( self, event )
event.target:removeSelf()
end
blaster.collision = onLocalCollision
blaster:addEventListener( “collision”, blaster )

transition.to(blaster, {delay = 250, time = 500, x = blaster.x + 400, onComplete = remover})
end

function touchfire ()
weapon()
projectile()
end
firebutt:addEventListener(“touch”, touchfire)
[/code] [import]uid: 176335 topic_id: 32419 reply_id: 128941[/import]

On your question #2, you should look at the info here (http://www.coronalabs.com/blog/2012/04/11/corona-touch-events-tutorial/), and in particular the section on touch event phases. In your case, you may want to fire a weapon and projectile only after the “ended” phase.

  • Andrew [import]uid: 109711 topic_id: 32419 reply_id: 128942[/import]

Hi

I got to fix my first problem by using the below mentioned code.
I still need help with my second question, which was “2- Also, how do i put some delay between the fire action coz the bullets hit each other when you click/tap on fire button fast enough (timer.performWithDelay doesnt seem add-able anywhere in it)”
Thanx in advance!

[code]
local function projectile ()

physics.addBody( blaster, “dynamic”, {bounce = 0} )
local sendblaster = transition.to(blaster, { time = 500, x = blaster.x + 400, onComplete = remover})
local function onLocalCollision( self, event )
transition.cancel(sendblaster)
event.target:removeSelf()
end
blaster.collision = onLocalCollision
blaster:addEventListener( “collision”, blaster )

end
[/code] [import]uid: 176335 topic_id: 32419 reply_id: 128943[/import]

HI there,

First, see the warnings here (http://developer.coronalabs.com/content/game-edition-collision-detection) and here (http://docs.coronalabs.com/api/event/collision/index.html) – you should avoid trying to remove a physics object in the middle of a collision listener. Instead, enclose the code to remove the physics objects in a short timer.performWithDelay.

Second, you have created two static objects and are manipulating them using transitions. Instead, try making them dynamic physics objects and setting their velocities so that they will collide with each other. That way, the physics engine “owns” the movement of the objects and can properly detect the collision.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 32419 reply_id: 128939[/import]

Thanx for the input Andrew. I got it to work but as i upgraded my game i ran into another problem. I have a sprite sheet of my bullet (blaster). It shoots fine but with that collision factor ,its transition gets a problem and the game gets stuck.

1- How do i cancel the transition when the object already gets removed by the collision.
2- Also, how do i put some delay between the fire action coz the bullets hit each other when you click/tap on fire button fast enough (timer.performWithDelay doesnt seem add-able anywhere in it)

[code]
local remover = function (obj)
obj:removeSelf()
end
local function weapon ()
blaster = sprite.newSprite (blasterset)
blaster:prepare(“fireA”)
blaster:play(“fireA”)
end

local function projectile ()
physics.addBody( blaster, “dynamic”, {bounce = 0} )
local function onLocalCollision( self, event )
event.target:removeSelf()
end
blaster.collision = onLocalCollision
blaster:addEventListener( “collision”, blaster )

transition.to(blaster, {delay = 250, time = 500, x = blaster.x + 400, onComplete = remover})
end

function touchfire ()
weapon()
projectile()
end
firebutt:addEventListener(“touch”, touchfire)
[/code] [import]uid: 176335 topic_id: 32419 reply_id: 128941[/import]

On your question #2, you should look at the info here (http://www.coronalabs.com/blog/2012/04/11/corona-touch-events-tutorial/), and in particular the section on touch event phases. In your case, you may want to fire a weapon and projectile only after the “ended” phase.

  • Andrew [import]uid: 109711 topic_id: 32419 reply_id: 128942[/import]

Hi

I got to fix my first problem by using the below mentioned code.
I still need help with my second question, which was “2- Also, how do i put some delay between the fire action coz the bullets hit each other when you click/tap on fire button fast enough (timer.performWithDelay doesnt seem add-able anywhere in it)”
Thanx in advance!

[code]
local function projectile ()

physics.addBody( blaster, “dynamic”, {bounce = 0} )
local sendblaster = transition.to(blaster, { time = 500, x = blaster.x + 400, onComplete = remover})
local function onLocalCollision( self, event )
transition.cancel(sendblaster)
event.target:removeSelf()
end
blaster.collision = onLocalCollision
blaster:addEventListener( “collision”, blaster )

end
[/code] [import]uid: 176335 topic_id: 32419 reply_id: 128943[/import]