Some things I noticed while taking a look at the code:
Also, it doesn't harm to localize functions out of the math table if you need to use them a lot.
- You should local more global variables.
- Is there a reason you extensively use document variables? Debugging? You shouldn't use them if you don't really need them (they are less optimised than Lua data types)
- You are using image.copy and image.rotate in your on.paint method. You should not do this, they are very expensive routines. Rather rotate your canon 360 times (if you really need one degree precision) while loading your game and store the resulting images in a table that you can easily access.
Also, it doesn't harm to localize functions out of the math table if you need to use them a lot.