Random collisions occuring

Hello, I’ve been having a problem using the physics engine where I have a moving object (dynamic, transition) and a draggable object (kinematic, responds to touch).
 
When the collision occurs, I want it to cause the draggable object to fade(I managed this with transition.dissolve), remove the dynamic object from view and display an image in it’s place.
 
I have uploaded a video to show the problem:

[media]http://www.youtube.com/watch?v=pAldp55q4jo&feature=youtu.be[/media]

Basically, the error is where the purple(kinematic) object fades without appearing to have collided with the transitioning (orange) object.

It correctly fades when the orange and purple collide also.

Code:

[lua]system.setIdleTimer(false)

– allow user’s background music to play
–audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode)

– Hide Status Bar
display.setStatusBar(display.HiddenStatusBar)


– FORWARD REFERENCES --Call functions locally but with a global scope

local centerX = display.contentCenterX
local centerY = display.contentCenterY

–Variables

local score = 0
local timer = 0
local dodgeCount = 0d

local bg
local bg2
local spaceship
local enemy
enemyHit = false
–Functions
local easyGame
local easyStart

–Filters
–local enemyCollisionFilter = { categoryBits = 1, maskBits = 1}


–DEFINE LOCAL VARIABLES {AND TABLES}

–local sounds
–local sounds={}

– LIBRARIES – load global libraries that will be used throughout the app

physics = require(‘physics’)
physics.setDrawMode(‘hybrid’)
physics.start()
physics.setGravity(0,0)

– GRAPHICS

– Load Graphics

– AUDIO

local hit = audio.loadSound(‘hit.wav’)


– Main Functions

–Menu Interface
–(Note: X=Left/Right, Y=Up/Down | - = Left, + = Right , | - = Up, + = Down) (>> = + | << = - | V = + | ^ = -)

function onLocalCollision( event )
enemyHit=true
transition.dissolve(spaceship, spaceship, 5000, 0)
end

easyStart = function()
enemy = display.newImageRect(‘images/spaceship.png’, 50, 100)
enemy.x = 400
enemy.y = math.random(20, display.contentHeight-20)
enemy.trans = transition.to (enemy, { x=-40, time=math.random(1600, 2000), delay=1000, onComplete=easyStart})
physics.addBody(enemy, ‘dynamic’)
enemy.collision = onLocalCollision
enemy:addEventListener(‘collision’, enemy)
end

Runtime:addEventListener(‘collision’, onLocalCollision)

function easyScoring()
timer = timer + 1
scoreTxt.text = ‘Score: ‘…score…’ Time:’… timer
scoreTxt.x = centerX
scoreTxt.y = 450
score = score + 2
if (enemyHit == true) then
enemyHit = false
Hit = display.newImage(‘images/hit.png’, enemy.x, enemy.y)

–display.remove(Hit)
display.remove(enemy)
end
end

Runtime:addEventListener(‘enterFrame’, easyScoring)

–Easy Mode
function easyGame()
bg = display.newImage(‘images/gameBg.png’)
bg:setReferencePoint(display.BottomLeftReferencePoint)
bg.alpha = 0
bg.speed = 4
bg.x = -300

bg2 = display.newImage(‘images/gameBg.png’)
bg2:setReferencePoint(display.BottomLeftReferencePoint)
bg2.x = 0
bg2.speed = 4
bg2.alpha = 0

spaceship = display.newImageRect( “images/spaceship.png”, 50, 100 )
spaceship.x = display.contentCenterX
spaceship.y = display.contentCenterY
spaceship.alpha = 0
spaceship.xScale= 2
spaceship.yScale= 2
physics.addBody(spaceship, ‘kinematic’)
scoreTxt = display.newText(('score: ’ … score), 0, 0, “Helvetica”, 15)
----dodge = display.newText(('Dodge Count: ’ … dodgeCount), 0, 0, “Helvetica”, 15)
–transition effect
transition.to( bg, { time=2000, alpha=1} )
transition.to( bg2, { time=1000, alpha=1} )
transition.to( spaceship, { time=2000, alpha=1, xScale=1, yScale=1, x=200, onComplete=easyStart } )
–interface
spaceship.collision = onLocalCollision
spaceship:addEventListener(‘collision’, onLocalCollision)
end
–change this after interface is done.
–Call easyGame --This must be written after the function or it will cause errors. --Write before spaceship:touch or errors will occur.
easyGame()

function scrollbg(self, event)
–self.x = self.x + 4
if self.x > 300 then
self.x = -300
else
self.x = self.x + self.speed
end
end

bg.enterFrame = scrollbg
Runtime:addEventListener(‘enterFrame’, bg)

bg2.enterFrame = scrollbg
Runtime:addEventListener(‘enterFrame’, bg2)[/lua]

I have reviewed and twiddled with my code about 20+ times but can’t seem to sort it.

Any help is much appreciated.

What happens if you remove line 105?

  1. spaceship.collision = onLocalCollision

It doesn’t change anything visibly.

I have noticed that it occurs mostly when the orange object hits the side of the screen…

(Upon entry and exit)

Right, it seems like when the orange ship enters the screen it (often) triggers the collision event.  Perhaps off screen image culling triggers collision events when physics objects enter the screen??  Simplest solution in this case might be to put the collision event on the purple ship instead.  You can get the id of the colliding ship from the event and then destroy it that way.

In your onLocalCollision function, try putting a print statement to dump the contents of the event table.  I suspect that the collisions are firing normally, and the issue is actually a funny behavior of transition.dissolve (which few developers use).  You’ve set a dissolve time of 5 seconds, and all of the “false” collisions in your video occurred within 5 seconds, which leads me to believe it’s not a problem with the collisions, but with how dissolve is working.

  • Andrew

By this do you mean when two ships collide or when it leaves/enters the screen?

I have managed to correct the original error using your suggestion, thanks but now this happens-

d9162f9f17667d5b06a8b4290103f0a8.png

Hi, I had tried disabling transition.dissolve before but the ships still seem to ‘collide’ with the edge of the screen which I think is the cause of the problem.

This happens-

bf7d6dbc0b7fe372c4ee8ff10f55f2bd.png

What code would you suggest I use to print/dump the event table?

To print a table quickly, you could do this:

[lua]

for k,v in pairs(event) do

   print(k,v)

end

[/lua]

Or you could use the function in the code exchange here: http://developer.coronalabs.com/code/printr-implementation-dumps-everything-human-readable-text.

  • Andrew

Hard to tell from this screenshot what is going on, although I’m guessing what’s happening is the orange ships aren’t being destroyed after the collision is detected (as you desire?) and instead they are piling up and colliding with each other on the left side of the screen.  Basically, though, if the collision listener is attached to the purple ship, then when an orange ship collides with it in your listener can be designed to destroy “event.other” which will remove the colliding orange ship.   It looks like the way you have it now nothing is being destroyed. 

Also, destroying physics objects after a collision is a bit trickier than removing regular display objects because you need to wait a fraction of a second for the physics calculations of the collision to play out before destroying a physical object.  I suggest reading this section of the Corona docs to try to get a better handle on what you want to have happen and some of the pitfalls.  There is a good example near the middle of the page, called Local Collision Listeners, that might help.

http://developer.coronalabs.com/content/game-edition-collision-detection

Hm yes, I do need to destroy the ships after their transitions have ended, though I need to make sure they can be reused where I think I had errors previously when I set them to nil.
 
I adjusted the code to include

[lua]function destroyEnemy()

    enemy:removeSelf()

    –

    easyStart()

end     [/lua]

and it works as I want it to :smiley: …at least for a while until it crashes :confused:

Now I want to use this code

[lua]timer.performWithDelay( 1000, destroyEnemy )[/lua]

but where would you suggest I put it, as it’s causing this error:

56883bf919d64e34b93cafe47a512138.png

EDIT: Never mind, seems I had another variable called ‘timer’ which was causing the errors. Thank you both for helping me :slight_smile:

What happens if you remove line 105?

  1. spaceship.collision = onLocalCollision

It doesn’t change anything visibly.

I have noticed that it occurs mostly when the orange object hits the side of the screen…

(Upon entry and exit)

Right, it seems like when the orange ship enters the screen it (often) triggers the collision event.  Perhaps off screen image culling triggers collision events when physics objects enter the screen??  Simplest solution in this case might be to put the collision event on the purple ship instead.  You can get the id of the colliding ship from the event and then destroy it that way.

In your onLocalCollision function, try putting a print statement to dump the contents of the event table.  I suspect that the collisions are firing normally, and the issue is actually a funny behavior of transition.dissolve (which few developers use).  You’ve set a dissolve time of 5 seconds, and all of the “false” collisions in your video occurred within 5 seconds, which leads me to believe it’s not a problem with the collisions, but with how dissolve is working.

  • Andrew

By this do you mean when two ships collide or when it leaves/enters the screen?

I have managed to correct the original error using your suggestion, thanks but now this happens-

d9162f9f17667d5b06a8b4290103f0a8.png

Hi, I had tried disabling transition.dissolve before but the ships still seem to ‘collide’ with the edge of the screen which I think is the cause of the problem.

This happens-

bf7d6dbc0b7fe372c4ee8ff10f55f2bd.png

What code would you suggest I use to print/dump the event table?

To print a table quickly, you could do this:

[lua]

for k,v in pairs(event) do

   print(k,v)

end

[/lua]

Or you could use the function in the code exchange here: http://developer.coronalabs.com/code/printr-implementation-dumps-everything-human-readable-text.

  • Andrew

Hard to tell from this screenshot what is going on, although I’m guessing what’s happening is the orange ships aren’t being destroyed after the collision is detected (as you desire?) and instead they are piling up and colliding with each other on the left side of the screen.  Basically, though, if the collision listener is attached to the purple ship, then when an orange ship collides with it in your listener can be designed to destroy “event.other” which will remove the colliding orange ship.   It looks like the way you have it now nothing is being destroyed. 

Also, destroying physics objects after a collision is a bit trickier than removing regular display objects because you need to wait a fraction of a second for the physics calculations of the collision to play out before destroying a physical object.  I suggest reading this section of the Corona docs to try to get a better handle on what you want to have happen and some of the pitfalls.  There is a good example near the middle of the page, called Local Collision Listeners, that might help.

http://developer.coronalabs.com/content/game-edition-collision-detection