Collision bugs in Device build

Hi, I’m trying to make a top view runner game in which the player has to collect coins and avoid enemies. The player character, enemies and coins are physics bodies with functions being called when a collision event occurs involving a coin or an enemy.

(y_enem1 initially set =0.4*height and y_gap=0.05*height)
[lua]physics.start( )

physics.pause( )
for iter=1,num_coins do
coins[iter]=Food:new()
coins[iter]:position(math.random(0.06*width,0.92*width),0-y_gap)
physics.addBody( coins[iter], “dynamic”, {density=0, friction=1, bounce=1, isSensor=true } )
print(coins[iter].x…" "…coins[iter].y)
y_gap=y_gap+(0.1*height)
end

for enem_iter1=1,num_enemy1 do

enemies_1[enem_iter1]=Kasai1:new()
enemies_1[enem_iter1]:position(math.random(0.12*width,0.6*width),(centery-(1.5*y_enem1)))
physics.addBody( enemies_1[enem_iter1], “dynamic”, {shape=enemy1_outline, density=1, bounce=1,isSensor=true} )
print(enemies_1[enem_iter1].x…" "…enemies_1[enem_iter1].y)
y_enem1=y_enem1+(math.random(0.7,1.1)*height)
end
y_enem1=0.6*height

for enem_iter2=1,num_enemy1 do
enemies_2[enem_iter2]=Kasai2:new()
enemies_2[enem_iter2]:position(math.random(0.65*width,0.92*width),(centery-(2*y_enem1)))
physics.addBody( enemies_2[enem_iter2], “dynamic”, {shape=enemy1_outline, density=1, bounce=1,isSensor=true} )
print(enemies_2[enem_iter2].x…" "…enemies_2[enem_iter2].y)
y_enem1=y_enem1+(math.random(0.6,1.5)*height)
end[/lua]

num_coins=15
num_enemy1=8

Thus total of 15 coins and 16 enemies. The code above is used to create patterns of objects and to place them at random yet appropriate gaps.
Furthermore these are the collision handlers:
[lua]function onCoinCollision(self,event)
if(event.phase==“began”) then
–print(“Coin collided”)
local move_coin=function()

y_gap=math.random(0.7,4)*height

self.y=-1*y_gap
self.x=math.random(0.15*width,0.85*width)
end
if(event.other==player) then
num_plants=num_plants+1
text_plants.text="Lusan eaten: "…num_plants
print(num_plants)
end

timer.performWithDelay( 1, move_coin ,1 )
end
end

function onEnemyCollision(self,event)
if(event.phase==“began”) then
print(“Enemy1 collided”)
local move_enemy1=function()

self.y=0-y_enem1
y_enem1=height*(math.random(0.6,3))
self.x=math.random(0.15*width,0.85*width)
end
if (event.other==player) then
lives=lives-1
end

timer.performWithDelay( 1, move_enemy1 ,1 )

end
if(lives<1) then

prepare_end_game()
end
end

for iter=1,num_coins do
coins[iter]:setLinearVelocity( 0, 196 )
coins[iter]:addEventListener( “collision”, coins[iter] )
coins[iter].collision=onCoinCollision
end
for enem_iter1=1,num_enemy1 do
enemies_1[enem_iter1]:setLinearVelocity( 0, 196 )
enemies_1[enem_iter1]:addEventListener( “collision”, enemies_1[enem_iter1] )
enemies_1[enem_iter1].collision=onEnemyCollision
end
for enem_iter2=1,num_enemy1 do
enemies_2[enem_iter2]:setLinearVelocity( 0, 196 )
enemies_2[enem_iter2]:addEventListener( “collision”, enemies_2[enem_iter2] )
enemies_2[enem_iter2].collision=onEnemyCollision
end

physics.start( )[/lua]

In the simulator, my game runs fine, the coins and enemies are placed where I intend them to be placed. Moreover, if enemies or coins happen to be placed next to each other and collide, they get placed somewhere else due to the collision handling.

Unfortunately, when I compiled my game into android build and ran in on my phone ( Huawei G610), all of the coins and enemies continue to appear at the top of the screen, collide with each other and reappear at the top again. I have added a link to a screenshot I took of this occurrence. Look at the top of the picture.

https://app.box.com/s/74rtpb6j7lzsf7nuz6ec

​I have Corona public build 2014.2189
Is this a bug? Or am I doing something wrong?
 

Hi @alirahman9316,

Are you getting any errors from the device console when you run the game on the phone? Can you please try to build one of the sample apps like “Simple Pool” and report if it works on the phone? It’s located in your local application directory here:

CoronaSDK > SampleCode > Physics > SimplePool

Thanks,

Brent

@Brent Sorrentino

The SimplePool game runs fine on my phone. Anyways, I installed the Android SDK on my PC and used the adb tools to take a look at the device console whilst my game:

http://gyazo.com/11c3e2c761ab866f4739a5815b9f7392

The above screenshot shows the positions/coordinates of the 15 coins and 16 enemies( 8 enemies_1 and 8 enemies_2). The position is shown in the sequence of <x-coordinate  y-coordinate>.

As seen in the screenshot, the y-coordinate for consecutive objects remains the same in some cases. This results in objects overlapping each other. This doesn’t happen in the simulator:
http://gyazo.com/85e8da17dd43f86c5dbe44a5a126e219

The y_enem1 variable doesn’t seem to increment in some iterations in the device build. What’s causing this anomaly? Is it due to some bug in the math.random function?

Ali

Hi Ali,

This may be something with the random number generator. Did you “seed” the random number generator in your code? That can be done by including the following line somewhere in advance of generating the random numbers:

[lua]

math.randomseed( os.time() )

[/lua]

Take care,

Brent

I added the randomseed line but it made no difference:
http://gyazo.com/dea526b5ea300cfc3980553a5b6c3dba

Hi Ali,

Can you just run a basic random number routine on your device and see if you get better results? I’d like to narrow this down a bit.

[lua]

math.randomseed( os.time() )

for i=1,50 do

   print( math.random( -8000, 0 ) )

end

[/lua]

I found the problem. After fiddling with the code you gave and adjusting the math.random arguments to match those used in my game, I realized that math.random only returns integer values. Thus

 [lua]math.random(0.7,3)[/lua]
would have returned either 0,1,2,3. Probability of 0 (hence an overlap between objects due to my code) being returned is 0.25.

I have made changes. So:
[lua]y_enem1=y_enem1+(math.random(0.7,1.1)*height)[/lua]

becomes:
[lua]y_enem1=y_enem1+(math.random(7,11)*0.1*height)[/lua]

I compiled a new device build and checked the device console:
http://gyazo.com/01676f7fc93325d39898ed16b0f2bd20

There is no more overlap.

Thanks a lot Brent!!

Ali
 

Hi @alirahman9316,

Are you getting any errors from the device console when you run the game on the phone? Can you please try to build one of the sample apps like “Simple Pool” and report if it works on the phone? It’s located in your local application directory here:

CoronaSDK > SampleCode > Physics > SimplePool

Thanks,

Brent

@Brent Sorrentino

The SimplePool game runs fine on my phone. Anyways, I installed the Android SDK on my PC and used the adb tools to take a look at the device console whilst my game:

http://gyazo.com/11c3e2c761ab866f4739a5815b9f7392

The above screenshot shows the positions/coordinates of the 15 coins and 16 enemies( 8 enemies_1 and 8 enemies_2). The position is shown in the sequence of <x-coordinate  y-coordinate>.

As seen in the screenshot, the y-coordinate for consecutive objects remains the same in some cases. This results in objects overlapping each other. This doesn’t happen in the simulator:
http://gyazo.com/85e8da17dd43f86c5dbe44a5a126e219

The y_enem1 variable doesn’t seem to increment in some iterations in the device build. What’s causing this anomaly? Is it due to some bug in the math.random function?

Ali

Hi Ali,

This may be something with the random number generator. Did you “seed” the random number generator in your code? That can be done by including the following line somewhere in advance of generating the random numbers:

[lua]

math.randomseed( os.time() )

[/lua]

Take care,

Brent

I added the randomseed line but it made no difference:
http://gyazo.com/dea526b5ea300cfc3980553a5b6c3dba

Hi Ali,

Can you just run a basic random number routine on your device and see if you get better results? I’d like to narrow this down a bit.

[lua]

math.randomseed( os.time() )

for i=1,50 do

   print( math.random( -8000, 0 ) )

end

[/lua]

I found the problem. After fiddling with the code you gave and adjusting the math.random arguments to match those used in my game, I realized that math.random only returns integer values. Thus

 [lua]math.random(0.7,3)[/lua]
would have returned either 0,1,2,3. Probability of 0 (hence an overlap between objects due to my code) being returned is 0.25.

I have made changes. So:
[lua]y_enem1=y_enem1+(math.random(0.7,1.1)*height)[/lua]

becomes:
[lua]y_enem1=y_enem1+(math.random(7,11)*0.1*height)[/lua]

I compiled a new device build and checked the device console:
http://gyazo.com/01676f7fc93325d39898ed16b0f2bd20

There is no more overlap.

Thanks a lot Brent!!

Ali