Whilst working on a game, I was unexpectedly led me down a long, but productive, detour of a road in trying to implement a buoyancy effect that makes physics objects float realistically when dropped into a body of liquid.
A search on YouTube and Google showed some interesting implementations on various platforms (Flash and Java), but I couldn’t find anything related to Corona SDK or Lua that I could dissect, learn from and implement myself.
So, I decided to have a go at creating the buoyancy myself. After all, this best way to learn is by diving in (no pun intended) and doing it yourself.
The maths may not be perfect, but I think that the effect looks good and, more importantly, pretty realistic.
In this example, I’ve added a ‘Settings’ section that allows simple changing of:
- Number Of Boxes
- Size Of Boxes
- Liquid Density
The liquid density allows the user to control how viscous the liquid is. The default setting of 1.0 simulates water, and the higher the number, the more viscous the fluid becomes. A setting of 2.5 here simulates something similar to oil etc.
In a future update, I will be modularising the code to make it more flexible, and support various display objects etc, and I’ll also be tightening up the physics calculations, but hopefully it’s something useful that you may be able to use in your own projects.