Lag on collision at 60fps

I would try commenting things out and to try to figure out what is cause the lag. 

Here are the things that could be causing it

1. media.playSound(“snd/ball_pop.wav”)

2. local settings = loadsave.loadTable(“bounceSettings.json”) and loadsave.saveTable(settings, “bounceSettings.json”)

So it was the media.playSound. But this was the only way that I knew to play audio in realtime on android without a delay. Is there another solution?

File IO is slow. Depending on the sound, event.playSound() has to load in and decode the sound which takes time. For short sounds it can be more timely to play than audio.play() on Android. But it’s still a file operation going on.

Next on loadsave, you only need to load the file once at the start of the app running. After that, the table should remain in memory. You update the table and then save whenever you change it. You may have to do some techniques to get the table visible to all of your scenes.

But generally, if you’re in a tight, performance hungry part of your code, delay the save for a while. Updating the high score isn’t a critical operation.

Rob

Hi Rob,

it works fine now. I moved the loadsave table and removed the event.playSound() and replaced it with audio.play(). But the audio feels delayed on Android devices. Is this just my device or is it a known issue? So just to be clear, the game doesn’t stutter, the audio just comes with a delay.

Just a quick question, why do call it event.playSound, shouldn’t it be media.playSound

edit: audio lag has always been a problem on android devices running a version less than 4.1 (there are of course some exception to this)  

edit 2: when are you loading the audio?

You’re right it’s media.* we just call it “event sound” and my brain lapsed.

Urgh yeah. We all make mistakes I guess  :stuck_out_tongue:

It was written correctly in the code though :smiley:

I’m running the audio.play() at the same place where media.playSound() was located because I want the sound every time when the collision happens.

where is the audio.loadSound being called? It should be called when the app opens or when there little “horse power” (cpu or stuff going on).

It’s called at the top, outside of the composer functions where I declare my forwared declarations.

What android device and c version are you running this app on? You might make sure you are up to date android version wise.

I’m running it on an galaxy s7 edge

Wow, that is a new device.
Are you doing local collision or global collision?
https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html

A local collision.

Have you tried doing precollison?
https://docs.coronalabs.com/api/event/preCollision/index.html

If push comes to shove you can always try to work around it. Here are some idea you could do. Edit the physics body shape to make it little bit larger then it actually is and do a timer on the composer.gotoScene.